【发布时间】:2020-07-17 18:07:43
【问题描述】:
这是我编写的程序的第一步。
目标:用户需要写一个句子,我的程序需要将它存储在一个数组中。这可以是他们喜欢的短或长。
我的程序:运行没有技术错误
问题:当我运行程序时,我无法提交超过 1 个单词的内容。 'Hello' 提交并且程序执行。除此之外的任何东西 - 新单词、空格、标点符号和数字,当我按 Enter 键时 - 我只是不断跳到终端中的新行,程序不会进入下一步。
这是我正在使用的代码
- 提示用户输入文本和
- 存储答案。
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
int main(void)
{
string z = get_string("Text: \n");
// int n = strlen(z);
int a = 0;
int d = 1;
int e = 0;
while (z[a] != '\0')
{
while (isalpha(z[a]))
{
a++;
}
while (z[a] ==' ')
{
d++;
}
//Count the sentences
while (z[a] == '.' || z[a] == '!' || z[a] == '?' || z[a] == '/')
{
e++;
}
}
// printf("%i letter(s)\n", a);
// printf("%i word(s)\n", d);
// printf("%i sentence(s)\n", e);
//Coleman-Liau index formula
int m = (0.0588 * (100 * a / d) - 0.206 * (100 * e / d) - 15.8);
if (m <= 1)
{
printf("Before Grade 1\n");
}
if (m >= 16)
{
printf("Grade 16+\n");
}
if (m < 16)
{
printf("Grade %i\n", m);
}
}
非常新的编码和任何帮助将不胜感激!
我的终端截图
【问题讨论】:
-
那是 CS50
get_string()还是你写的函数? -
什么是不起作用的“下一步”?
-
我不确定你的意思。当您按 Enter 时,它应该会转到终端中的新行。将
printf("%s\n", z);放在该行之后,您应该会看到您输入的行。 -
欢迎来到 SO。您可以轻松地将终端中的纯文本复制并粘贴到您的问题中。无需制作屏幕截图并将输出添加为图形。
-
while (z[a] ==' ') { d++;如果 z[a] == ' ',} 将永远不会停止