Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
For loop 为什么for循环的第二次迭代不等待用户输入_For Loop - Fatal编程技术网

For loop 为什么for循环的第二次迭代不等待用户输入

For loop 为什么for循环的第二次迭代不等待用户输入,for-loop,For Loop,我的for循环有问题。当我进行第二次迭代时,程序不会等待用户输入。我们感激地接受了任何帮助 #include <stdio.h> #include "dogInfo.h" main() { struct dogInfo dog[3]; //Array of three structure variable int ctr; //Get information about dog from the user printf("You will be

我的for循环有问题。当我进行第二次迭代时,程序不会等待用户输入。我们感激地接受了任何帮助

#include <stdio.h>
#include "dogInfo.h"


main()
{
    struct dogInfo dog[3];  //Array of three structure variable
    int ctr;

    //Get information about dog from the user
    printf("You will be asked to describe 3 dogs\n");
    for (ctr = 0; ctr < 3; ctr ++)
    {
       printf("What type (breed) of dog would you like to describe for dog #%d?\n", (ctr +1));
       gets(dog[ctr].type);
       puts("What colour is the dog? ");
       gets(dog[ctr].colour);
       puts("How many legs does the dog have? ");
       scanf(" %d", &dog[ctr].legNum);
       puts("How long is the snout of the dog in centimetres? ");
       scanf(" %.2f", &dog[ctr].snoutLen);
       getchar(); //clears last new line for the next loop
    }

你的问题是gets没有按照你想要的方式运行。无论如何,你都不应该使用gets,因为它是危险的,而且是安全的。 但幸运的是,scanf可以轻松读取字符串,还允许指定格式(在本例中为%24s,因为您希望最多读取24个字符,因为最后一个字符是为空终止符保留的)

代码的工作原理如下:

#包括
结构dogInfo
{
字符类型[25];
炭色[25];
int legNum;
浮鼻;
};
int main()
{
struct dogInfo dog[3];//三个结构变量的数组
国际中心;
//从用户处获取关于狗的信息
printf(“将要求您描述3只狗”\n);
对于(ctr=0;ctr<3;ctr++)
{
printf(“您想为狗描述什么类型(品种)#%d?\n”,(ctr+1));
scanf(“%24s”,dog[ctr].type);
puts(“狗是什么颜色的?”);
扫描频率(“%24s”,狗[ctr]。颜色);
puts(“狗有几条腿?”);
scanf(“%d”,&dog[ctr].legNum);
puts(“狗的鼻子有多长,以厘米为单位?”);
scanf(“%.2f”,&dog[ctr].snoutLen);
getchar();//清除下一个循环的最后一行新行
}
}

谢谢,但是你能告诉我为什么第一次迭代可以,但是第二次迭代C不会等待用户输入吗?谢谢
 struct dogInfo
{
    char type[25];
    char colour[25];
    int legNum;
    float snoutLen;
};