Connecting Tech Pros Worldwide Help | Site Map

Trouble with while loop

Member
 
Join Date: Jan 2007
Posts: 32
#1: Feb 14 '07
I'm having a problem trying to write a loop that tells the user to input a positive number. And if number is zero the program stops. The numbers can be no greater than 1000.

Expand|Select|Wrap|Line Numbers
  1. # include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     int i;
  7.     int num;
  8.  
  9.     while (num >0)
  10.     {
  11.  
  12.     cout<< "Enter a positive number:"<<endl;
  13.     cin>> num;
  14.     if (num=0)
  15.     cout<<"END OF PROGRAM";
  16.     else
  17.     { cout<< num;
  18.     }
  19.     }
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.     return 0;
  27. }
sicarie's Avatar
Moderator
 
Join Date: Nov 2006
Location: USA
Posts: 3,929
#2: Feb 14 '07

re: Trouble with while loop


Quote:

Originally Posted by compsci

I'm having a problem trying to write a loop that tells the user to input a positive number. And if number is zero the program stops. The numbers can be no greater than 1000.

Expand|Select|Wrap|Line Numbers
  1. # include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     int i;
  7.     int num;
  8.  
  9.     while (num >0)
  10.     {
  11.  
  12.     cout<< "Enter a positive number:"<<endl;
  13.     cin>> num;
  14.     if (num=0)
  15.     cout<<"END OF PROGRAM";
  16.     else
  17.     { cout<< num;
  18.     }
  19.     }
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.     return 0;
  27. }

Right now your program will loop until the user inputs any number less than 1, but if they put in 0, it will display 'END OF PROGRAM.'

Some things to consider - 1) how many numbers do you want the user to put in?
2) what will you do with them if they fulfill the condition?
3) what is going to happen if they fail the condition?
4) how are they going to exit that loop?

Once you answer those, you will be better able to write your program - having a better idea of what you want it to do, and we will be better able to help you.
Lives Here
 
Join Date: Oct 2006
Posts: 1,626
#3: Feb 14 '07

re: Trouble with while loop


Quote:

Originally Posted by compsci

I'm having a problem trying to write a loop that tells the user to input a positive number. And if number is zero the program stops. The numbers can be no greater than 1000.

Expand|Select|Wrap|Line Numbers
  1. # include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     int i;
  7.     int num;
  8.  
  9.     while (num >0)
  10.     {
  11.        cout<< "Enter a positive number:"<<endl;
  12.        cin>> num;
  13.        if (num=0)
  14.             {
  15.           cout<<"END OF PROGRAM";
  16.             }
  17.        else
  18.        { 
  19.                       cout<< num;
  20.        }
  21.     }
  22.     return 0;
  23. }

Hi. So stepping through your code.
You declare an int called num and give it no initial value. This means that the compiler will arrange 4 bytes of memory space to be allocated and the value of num will be any old garbage that happens to be at that memory space at that time. Could be positive, could be negative. You just don't know. Just for now we should initialize it with a zero
Expand|Select|Wrap|Line Numbers
  1. int num = 0;
The next part of your code begins a while loop with the test expression (num>0). Well we know that that is not the case because we initialized num to zero so let's test after the user has input a value which mean we use a do while loop instead:
Expand|Select|Wrap|Line Numbers
  1. int num = 0;
  2. do {
  3. //blah blah blah
  4. }
  5. while (num>0);
The only problem with the rest of the code comes at this point:
Expand|Select|Wrap|Line Numbers
  1. if (num=0)
You are assigning the value 0 to num which is true no matter what the value of num before this assignment. To test for zero you should use double equals:
Expand|Select|Wrap|Line Numbers
  1. if (num == 0)
And now your code should work.
Reply