I am trying to print out each integer on a new line, given integers separated by whitespace from a user input. It stops printing after a specific number, lets say 84. For example
我试图打印出一个新行上的每个整数,给定由用户输入的空格分隔的整数。它会在特定数字后停止打印,比方说84.例如
The user input is
用户输入是
20 -4 84 8
How could i use a while loop to print these out as
我怎么能用while循环打印出来
20
-4
84
i know about scanf("%d %d %d %d", a, b, c, d), However the input size would be unknown, such that there could be only 3 numbers or 7 numbers. So far i have:
我知道scanf(“%d%d%d%d”,a,b,c,d),但是输入大小是未知的,因此可能只有3个数字或7个数字。到目前为止,我有:
#include <stdio.h>
int main(void)
{
int i = 0;
int x = 0;
scanf("%d", &x);
while (x != 84) {
print("%d\n", x[i]);
i++;
}
}
4 个解决方案
#1
4
Push the scanf
into the while
condition. Something like
将scanf推入while条件。就像是
while (scanf("%d", &x) != EOF && x != 84)
print("%d\n", x);
#2
2
The basic concept should be of two-steps:
基本概念应分为两步:
- Read the number
- Check and decide whether to print /continue.
阅读电话号码
检查并决定是否打印/继续。
A psuedo-code would look like
伪代码看起来像
while ((ret =scanf(x)) && ret != EOF ){
if (x == VAL) break;
printf(x);
}
#3
0
You have a few errors:
你有一些错误:
- the array
num
does not exist; - the
scanf
must be repeated inside the while loop.
数组num不存在;
必须在while循环内重复scanf。
The corrected code is thw following:
修正后的代码如下:
#include <stdio.h>
int main(void)
{
int x;
while( 1 ){
scanf( "%d", &x );
print( "%d\n", x );
if( x==84 ) break;
}
}
#4
-1
You need to put scanf()
inside the while
loop in order to update every new input. No need for arrays if you want to print only the inputted values because you can print it after new input. Better do it with do-while
loop.
您需要将scanf()放在while循环中以更新每个新输入。如果您只想打印输入的值,则无需数组,因为您可以在新输入后打印它。最好用do-while循环来做。
#include <stdio.h>
int main(void)
{
int i = 0;
int x = 0;
do {
if ( scanf("%d", &x) < 0 )
break;
printf("%d\n", x);
} while (x != 84);
}
#1
4
Push the scanf
into the while
condition. Something like
将scanf推入while条件。就像是
while (scanf("%d", &x) != EOF && x != 84)
print("%d\n", x);
#2
2
The basic concept should be of two-steps:
基本概念应分为两步:
- Read the number
- Check and decide whether to print /continue.
阅读电话号码
检查并决定是否打印/继续。
A psuedo-code would look like
伪代码看起来像
while ((ret =scanf(x)) && ret != EOF ){
if (x == VAL) break;
printf(x);
}
#3
0
You have a few errors:
你有一些错误:
- the array
num
does not exist; - the
scanf
must be repeated inside the while loop.
数组num不存在;
必须在while循环内重复scanf。
The corrected code is thw following:
修正后的代码如下:
#include <stdio.h>
int main(void)
{
int x;
while( 1 ){
scanf( "%d", &x );
print( "%d\n", x );
if( x==84 ) break;
}
}
#4
-1
You need to put scanf()
inside the while
loop in order to update every new input. No need for arrays if you want to print only the inputted values because you can print it after new input. Better do it with do-while
loop.
您需要将scanf()放在while循环中以更新每个新输入。如果您只想打印输入的值,则无需数组,因为您可以在新输入后打印它。最好用do-while循环来做。
#include <stdio.h>
int main(void)
{
int i = 0;
int x = 0;
do {
if ( scanf("%d", &x) < 0 )
break;
printf("%d\n", x);
} while (x != 84);
}