作为我课程的一部分,我必须使用 Turbo C 来学习 C(不幸的是)。
我们的老师要求我们编写一段代码来计算段落中的字符、单词和句子的数量(仅使用 printf、getch() 和 while 循环.. 他不希望我们使用任何其他命令) . 这是我写的代码:
#include <stdio.h>
#include <conio.h>
void main(void)
{
clrscr();
int count = 0;
int words = 0;
int sentences = 0;
char ch;
while ((ch = getch()) != '\n')
{
printf("%c", ch);
while ((ch = getch()) != '.')
{
printf("%c", ch);
while ((ch = getch()) != ' ')
{
printf("%c", ch);
count++;
}
printf("%c", ch);
words++;
}
sentences++;
}
printf("The number of characters are %d", count);
printf("\nThe number of words are %d", words);
printf("\nThe number of sentences are %d", sentences);
getch();
}
它确实有效(至少计算字符和单词的数量)。但是,当我编译代码并在控制台窗口中检查它时,我无法让程序停止运行。它应该在我输入回车键后立即结束。这是为什么?