[英]I have two while loops next to each other, but how do I run the second while loop with the original input when the first while loop changed the input?
[英]How do I escape while loop when I have N amount of inputs?
using namespace std;
int main() {
int input;
int i=0;
while (1){
scanf("%d", &input);
printf("%d input:%d\n", i, input);
i++;
}
}
標准輸入:10 65 100 30 95.. .
有沒有辦法在點擊最后一個輸入后停止代碼並退出 while 循環? 輸入的數量可以是 N。
版)有沒有辦法計算標准輸入的數量? 這是我的主要問題。
using namespace std;
int main() {
int input;
int i=0;
while (1){
scanf("%d", &input);
printf("%d input:%d\n", i, input);
i++;
if (i >= 5) break; //change 5 to your desired amount of inputs
}
}
也許您正在閱讀具有 EOF(文件結尾)的輸入? 在這種情況下,您應該在收到 EOF 時停止
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int input;
int i = 0;
while (scanf("%d", &input) != EOF){
printf("%d input:%d\n", i, input);
i++;
}
printf("End\n");
return 0;
}
否則你可以做一個先讀取 N 然后迭代 N 次的程序
int main(void)
{
int input;
int i = 0;
int n = 0;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &input);
printf("%d input:%d\n", i, input);
}
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.