【发布时间】:2015-03-04 01:37:48
【问题描述】:
我希望用户能够输入一个数字 n,然后程序将从用户那里读取 n 个字符串。但是现在程序不会停止读取字符串了。
#include <stdio.h>
#define STRING_MAX 10
int main (void)
{
char string[STRING_MAX];
int count;
int total;
int n, i;
int chr;
scanf("%d", &n);
for(i=0; i<n; i++)
{
do {
count = 0;
total = 0;
while ((chr = getchar() != EOF)&& (chr != '\n'))
{
if (count < STRING_MAX - 1)
string[count++] = chr;
total += 1;
}
string[count] = '\0';
} while (total > STRING_MAX - 1);
printf("The input string was:\n\t%s\n", string);
}
return 0;
}
【问题讨论】:
标签: c string input scanf getchar