以下是同一程序的两个版本。第一个工作,第二个不工作。使用while (scanf()==1)
我想检查scanf。请解释一下我的错误(请简单地说 - 我是初学者:))
#include <ctype.h>
#include <stdio.h>
#include <string.h>
int main (void)
{
char string_to_cap[90];
int i, n;
// get line of text
printf ("Please, enter a word: ");
scanf ("%89s", string_to_cap);
if (string_to_cap != NULL) // if s is not NULL than go ahead and execute the code below
{
// capitalize text
for (i = 0, n = strlen(string_to_cap); i < n; i++)
{
printf("%c", toupper(string_to_cap[i]));
}
}
printf ("\n");
}
这是第二个不起作用:
#include <ctype.h>
#include <stdio.h>
#include <string.h>
int main (void)
{
char string_to_cap[90];
int i, n;
// get line of text
printf ("Please, enter a word: ");
while (scanf ("%89s", string_to_cap) == 1);
{
// capitalize text
for (i = 0, n = strlen(string_to_cap); i < n; i++)
{
printf("%c", toupper(string_to_cap[i]));
}
}
printf ("\n");
}
答案 0 :(得分:0)
while (scanf ("%89s", string_to_cap) == 1);
^
注意错误
这实际上意味着while
循环只有一个语句,即;
之后的街区
{
// capitalize text
for (i = 0, n = strlen(string_to_cap); i < n; i++)
{
printf("%c", toupper(string_to_cap[i]));
}
}
永远不会被执行。