Jack8246 Jack8246 - 1 year ago 93
C Question

Using a while loop to iterate through user input until specific number

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

The user input is

20 -4 84 8

How could i use a while loop to print these out as


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:

#include <stdio.h>

int main(void)
int i = 0;
int x = 0;
scanf("%d", &x);
while (x != 84) {
print("%d\n", x[i]);

Answer Source

Push the scanf into the while condition. Something like

while (scanf("%d", &x) != EOF && x != 84)
    print("%d\n", x);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download