-3

What do I need to correct in the code so that the loop will stop when the user stops entering data?

while(scanf("%i", &num)){   
    printf ("%#o %d 0x%X\n", num, num, num);<br/>
}
new123321
  • 7
  • 3

2 Answers2

0

Your scanf("%i", &num) only returns when the user presses return/enter, if they've previously typed some non-whitespace characters into the terminal. If the user presses return on an empty line, the function will continue to wait for a non-whitespace characters.

If you type a non-numeric value, before you press return, you'll exit the while loop.

J...S
  • 5,079
  • 1
  • 20
  • 35
Rich
  • 11
  • 1
  • 7
0

You can use this one

#include<stdio.h>
int main(){
    int num;
    printf("Enter -1 for termination:");
while(scanf("%i", &num)>0 ){
        if(num<0)
        break;
  printf ("%#o %d 0x%X\n", num, num, num);

}

return 0;
}

If you type -1 or any character then termination will occur:

Enter -1 for termination:3
03 3 0x3
6
06 6 0x6
9
011 9 0x9
k

Process returned 0 (0x0)   execution time : 8.779 s
Press any key to continue.
Brahma
  • 58
  • 8