1

So my question is the following.

int n=0;
while(n<=0)
    scanf("%d",&n);

This code enters in an infinite loop, and I don't have a clue why. When the user inputs a number > 0, the loop was supposed to stop.

And thanks:)

Jørgen R
  • 10,568
  • 7
  • 42
  • 59
user2116077
  • 13
  • 1
  • 5
  • 2
    The code may never enter the loop at all - since `n` is uninitialized. How do you know it's looping? Did you type a number and press ENTER? – Nik Bougalis Feb 27 '13 at 16:00
  • I just compiled this and when I enter the value 1 and hit enter, it exits the loop. I think your problem lies elsewhere in your program. You could try making `n` a `volatile int` just to make sure you aren't getting an optimization problem. – mrh Feb 27 '13 at 16:17
  • I *very* much doubt the issue is caused by the optimizer and `volatile` won't fix it. – Nik Bougalis Feb 27 '13 at 16:18
  • Ok i dont know what is a volatile int, but i already created a new file with only this code to try it alone, still same problem. – user2116077 Feb 27 '13 at 16:26

4 Answers4

4

Over and over and over and over...

stdin is (generally) line-buffered - one has to press <enter> to make the terminal transfer the characters to your program. So now there's a dangling newline character in the buffer, and scanf() will try to read it during the next iteration, but it's not an integer, so it fails and doesn't change the contents of the variable. To solve this, make scanf() eat the newline:

scanf("%d\n", &number);

(Oh yes, n is also used uninitialized, but it seems that your code enters the loop anyway, so that's not the issue. Do initialize it, though, else you will face other strange errors.)

  • @user2116077 Having deduced from the quality of this answer (sorry, but...) - you may have other problems/semantic errors/undefined behaviors in your code. Check for those as well. –  Feb 27 '13 at 16:03
  • @H2CO3 in c++ it's UB but is this true in case of c? – Arpit Feb 27 '13 at 16:03
  • 2
    You already tried what? Typing a number and pressing ENTER? Eating the newline? You aren't helping us help you... – Nik Bougalis Feb 27 '13 at 16:03
  • this is my first post, i am kinda confused sorry if i am not helpful xD I already tried typing and pressing enter, yes. – user2116077 Feb 27 '13 at 16:05
  • 1
    i mean is this program lead to ub as in c++. – Arpit Feb 27 '13 at 16:05
  • @Arpit Of course it does. Reading uninitialized variables is UB. –  Feb 27 '13 at 16:05
  • i have it initialized in the program, it was a typo in the question, sorry – user2116077 Feb 27 '13 at 16:06
  • @H2CO3 "scanf() will try to read it during the next iteration, but it's not an integer, so it fails and doesn't change the contents of the variable" it seems wrong to me. i just use the same code to read 10 ints and it works fine without using `\n` . http://ideone.com/3ez7mY – Arpit Feb 27 '13 at 16:31
1
while (n <= 0)
    // something

means "do something while value of n is less or equal to 0". Just make sure that n is initialized when condition n <= 0 is being evaluated. Using uninitialized variables produces undefined behavior.

You should do:

int n = 0;
while (n <= 0)
    scanf("%d\n",&n);
LihO
  • 41,190
  • 11
  • 99
  • 167
0

Since you claim to have tried things and they didn't work (although I don't see why) let's try something else. Let's use a programmer's best friend: printf. How about trying to run this code instead:

int n = 0;

while(n <= 0)
{
    printf("Please enter a number: ");
    scanf("%d\n", &n);
    printf("I see you entered: %d\n", n);
}

printf("Done with the loop. The value of n is: %d\n", n);

This will let you see what the computer is doing and what values it reads as it reads them. Try replacing your code with the above and let's see what happens.

Nik Bougalis
  • 10,495
  • 1
  • 21
  • 37
0

I think you should change your compiler because i'm getting the fine result.

You might have a problem somewhere else.

You can check here.:

http://ideone.com/C4Yobi

Code:

#include<stdio.h>
main( )
{
int n = 0;
while (n <= 0)
    scanf("%d",&n);
printf("%d",n);
}

Input:
-5
4
Output:
4
Arpit
  • 12,767
  • 3
  • 27
  • 40