Trouble with while loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • compsci
    New Member
    • Jan 2007
    • 32

    Trouble with while loop

    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.

    Code:
    # include <iostream>
    using namespace std;
    
    int main()
    {
    	int i;
    	int num;
    	
    	while (num >0)
    	{
    
    	cout<< "Enter a positive number:"<<endl;
    	cin>> num;
    	if (num=0)
    	cout<<"END OF PROGRAM";
    	else
    	{ cout<< num;
    	}
    	}
    	
    	
    	
    	
    	
    	
    	return 0;
    }
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    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.

    Code:
    # include <iostream>
    using namespace std;
    
    int main()
    {
    	int i;
    	int num;
    	
    	while (num >0)
    	{
    
    	cout<< "Enter a positive number:"<<endl;
    	cin>> num;
    	if (num=0)
    	cout<<"END OF PROGRAM";
    	else
    	{ cout<< num;
    	}
    	}
    	
    	
    	
    	
    	
    	
    	return 0;
    }
    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.

    Comment

    • willakawill
      Top Contributor
      • Oct 2006
      • 1646

      #3
      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.

      Code:
      # include <iostream>
      using namespace std;
      
      int main()
      {
      	int i;
      	int num;
      	
      	while (num >0)
      	{
      	   cout<< "Enter a positive number:"<<endl;
      	   cin>> num;
      	   if (num=0)
                  {
      	      cout<<"END OF PROGRAM";
                  }
      	   else
      	   { 
                            cout<< num;
      	   }
      	}
      	return 0;
      }
      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
      Code:
      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:
      Code:
      int num = 0;
      do {
      //blah blah blah
      }
      while (num>0);
      The only problem with the rest of the code comes at this point:
      Code:
      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:
      Code:
      if (num == 0)
      And now your code should work.

      Comment

      Working...