Tag: while loop

从scanf函数接受任意数量的输入

我试图使用scanf函数读取未知数量的输入。 int a[100]; int i = 0; while((scanf(“%d”, &a[i])) != ‘\n’) i++; // Next part of the code 但是这个函数不会进入下一部分代码,似乎有一个无限的while循环。 我该如何解决这个逻辑错误? 是否还有像sscanf scanf一样的其他替代方法可以将整数读入数组?

为什么{} while(condition); 在它结束时需要分号但是(条件){}不是吗?

我总是有放置的问题; 在结束while或不将它放在do while循环的末尾。 那是什么原因? 为什么 int numItemsToProcess = 3; while(numItemsToProcess > 0) { // process an item numItemsToProcess–; } 不需要; 最后但是 do { numItemsToProcess –; } while (numItemsToProcess > 0); 呢? 也许原因并不太重要。 但是当你知道你记得放在哪里的原因时; 。

为什么程序不执行最终的printf语句?

我无法弄清楚为什么程序控制没有到达第三个printf,就在for循环之后。 为什么不打印第三张printf ? 如果我将for循环更改为while循环,它仍然不会打印。 这是程序和输出: main() { double nc; printf (“Why does this work, nc = %f\n”, nc); for (nc = 0; getchar() != EOF; ++nc) { printf (“%.0f\n”, nc); } printf (“Why does this work, nc = %f”, nc); } 输出是: Why does this work, nc = 0.000000 test 0 1 2 3 4

在while循环中使用scanf

这个非常简单的问题可能是一个非常简单的答案: 我正在阅读Pratta的“C Primer Plus”,他一直在使用这个例子 while (scanf(“%d”, &num) == 1)… == 1真的有必要吗? 似乎有人可以写: while (scanf(“%d”, &num)) 似乎相等测试是不必要的,因为scanf返回读取的对象数,1将使while循环成为真。 是确保读取的元素数量正好为1还是完全多余的原因?

为什么这个C代码有问题?

在另一个问题上 , Jerry Coffin指出了以下内容: 它(可能)与你的问题并不真正相关,但是while (!feof(fileptr)){几乎是一个有保障的bug。 我想我会开始一个单独的问题,因为这个评论有些偏离主题。 有人可以向我解释一下吗? 这是我以前用C编写的第一个程序。

在while循环中使用scanf函数

我正在尝试为编程分配格式化以空格分隔的用户输入。 本质上,输入由任意数量的表达式组成 L integer integer integer integer和C integer integer integer 。 例如: L 1 1 5 7 C 4 5 3 。 到目前为止,我已经设法根据初始字符提取整数,并可以使用scanf函数迭代字符串: char a; while(scanf(“%c”, &a) == 1){ if(a == ‘C’){ int inputX, inputY, inputR; scanf(“%d %d %d”, &inputX, &inputY, &inputR); printf(“%d %d %d\n”, inputX, inputY, inputR); } else if(a == ‘L’){ int x1, […]

等待在循环中按C键进入?

我正在写一个C程序,我需要等待用户按任意键才能继续。 当我使用getchar(); 它等待按下Enter键。 但是当我在while循环中使用它while ,它不起作用。 如何使我的代码等待按任何键继续循环? 这是我的代码示例。 我正在使用GNU / Linux。 #include #include int main() { int choice; while(1) { printf(“1.Create Train\n”); printf(“2.Display Train\n”); printf(“3.Insert Bogie into Train\n”); printf(“4.Remove Bogie from Train\n”); printf(“5.Search Bogie into Train\n”); printf(“6.Reverse the Train\n”); printf(“7.Exit”); printf(“\nEnter Your choice : “); fflush(stdin); scanf(“%d”,&choice); switch(choice) { case 1: printf(“Train Created.”); break; case 2: […]

比较两个文本文件 – C中的拼写检查程序

我正在编写一个拼写检查程序,它将用户的文本文件与字典进行比较,以查看他们输入的单词是否在字典中。 如果不是,则打印错误消息以告知用户特定单词是错误的。 我已尝试过以下代码的多种变体,但未获得所需的结果。 它是嵌套while循环中的东西,它将它抛出。 这段代码处于草案阶段我必须使其更具内存效率等并整理它。 我只是想让它先工作。 谢谢! 编辑:根据下面的提示稍微修改了代码。 它现在读取第一个单词并说它在字典中。 然后显示第二个单词,但字典扫描循环不运行,程序挂起。 我知道它嵌套的while循环导致问题我无法理解它! /*Spellcheck program*/ /*Author: */ #include #include #include int main(void) { /*Open files and test that they open*/ FILE *fp1; FILE *fp2; char fname[20]; char wordcheck[45];/*The longest word in the English Language is 45 letters long*/ char worddict[45]; char dummy; int i; int dictcount = […]

C中是/否循环

我只是不明白为什么这个Yes / No循环不起作用。 有什么建议? 鉴于输入是“Y”。 我只是想让它运行循环,然后再次询问Y或N. 如果是Y,则打印成功,如果是N,则打印出良好的再见声明。 什么原因? int main(){ char answer; printf(“\nWould you like to play? Enter Y or N: \n”, answer); scanf(“%c”, &answer); printf(“\n answer is %c”); while (answer == ‘Y’){ printf(“Success!”); printf(“\nDo you want to play again? Y or N: \n”); scanf(“%c”, &answer); } printf(“GoodBye!”); return 0; }

使用C中的scanf在EOF退出while循环

我正在为我的入门C课程写一些非常小的课程。 其中一个要求我读取双倍值,每行一个数字,然后在EOF后打印出基本统计数据。 这是我的代码段,它给了我一些问题: double sample[1000000]; int result; double number; int i = 0; int count = 0; double sum = 0; double harmosum = 0; result = scanf(” %lf \n”, &number); double min = number; double max = number; while(result != EOF){ sample[i] = number; if(number max){ max = number; } sum += number; if(number […]