473,395 Members | 1,666 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Trouble with while loop

32
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. }
Feb 14 '07 #1
2 1794
sicarie
4,677 Expert Mod 4TB
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.
Feb 14 '07 #2
willakawill
1,646 1GB
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.
Feb 14 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Andreas Paasch | last post by:
Hi there, below a little code snippet that causes me some headaches. I had a similar one working fine, until I needed to redo my code on this one page entirely. Now my problem is, that the test...
2
by: apec | last post by:
Dear All, I'm having trouble getting my c++ code to work (below). Any suggestions as to what could be the problem? I think it has to do with the while loop, since the program ends before command...
1
by: teddarr | last post by:
I'm having trouble reading the first 2 lines of data in an external file. I am supposed to use a while loop to read the first 2 lines of an external file that contains several random integers. I...
1
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are...
10
by: Hendri Adriaens | last post by:
Hi, I'm trying to automate the creation of an excel file via COM. I copied my code below. I read many articles about how to release the COM objects that I create. The code below runs just fine...
3
by: bmerlover | last post by:
I believe my problem lies inside the while loop. When I click the play button on the gui app, it goes inside the while loop, reads the file and calls the necessary function to do what it needs to do....
0
by: bmerlover | last post by:
This code makes sense to me, I'm just having trouble trying to understand why it doesn't work correctly. This is a GUI APP. When the Play button is Clicked, the play_Click(System::Object * sender,...
4
by: Villanmac | last post by:
First up thanks to the guys who pointed out my error last week. Now I was wondering If I could get some help with this problem because again I have fallen into trouble. Basically im using the...
2
by: cmb3587 | last post by:
I am having a problem with the validation of the account number and password. The beginning of the program asks for users account # then pwd. The program is then supposed to go to a checkID...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.