0

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

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:

#include <stdio.h>

int main(void)
{
    int i = 0;
    int x = 0;
    scanf("%d", &x);
    while (x != 84) {
        print("%d\n", x[i]);
        i++;
    }
}
2
  • 1
    what is num? Do you want to store the previous numbers? Commented Jul 22, 2015 at 5:49
  • sorry editing the question. it is the 'x' which is the user input. No not looking to store previous numbers, just needing to print them Commented Jul 22, 2015 at 5:51

4 Answers 4

4

Push the scanf into the while condition. Something like

while (scanf("%d", &x) != EOF && x != 84)
    print("%d\n", x);
Sign up to request clarification or add additional context in comments.

8 Comments

+1 for checking the return value of scanf, to avoid inf-loop when user enters something that "%d" won't scan.
Thank you this is exactly what i needed. Could you explain to me how putting the scanf inside the while condition works compared to what i was doing? I am new to C, thanks
@Jack8246 You scanned for input once and compared to 84 again and again, this scans until 84 is found. That's the difference. This also checks if scanf correctly reads what it is asked to, int here.
@Shreevardhan I see, so by putting the scanf into the while loop, does the while loop automatically iterate through each integer from the user input? This is the main thing I'm not sure about, hence why i had the 'i++' in my previous code
@Jack8246 Yes. scanf("%d", &x); scans an integer in x. Putting this inside a loop condition will execute it in every loop iteration until the loop breaks. So, it scans integers until it finds 84. You can read a good book on C programming to get better idea.
|
2

The basic concept should be of two-steps:

  1. Read the number
  2. 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);
 }

Comments

0

You have a few errors:

  • the array num does not exist;
  • the scanf must be repeated inside the while loop.

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;
    }
}

2 Comments

the array num was supposed to be 'x', sorry about that. Thanks for the answer
this answer is almost right; however, the OP indicated the 84 was to be printed. so the 'if()' statement and the printf() statement need to be swapped. BTW: it is 'printf' not 'print' and the returned value (not the parameter value) also needs to be checked, so something like a user input that is not digits, like a simple newline and/or a...z, etc can be caught, when the returned value is not 1, need a call to getchar() rather than a call to printf() and if returned value is EOF, then need to exit loop
-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.

#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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.