# trouble with while statements...what am i missing?

Show 50 post(s) from this thread on one page
Page 1 of 2 12 Last
• March 9th, 2013, 04:51 PM
psfign
trouble with while statements...what am i missing?
Im trying to create a program that has the user input a 5 digit number. If it's between 10000 & 99999, it will do one thing..(just saying 'yes' for now. Outside those numbers will prompt the user to input again. However, if the user inputs the exact digits 76087, it should display 'term'.

I think my error is the braces. If someone could help me find my error without 'doing it for me'.

This current code is displaying 'term' whenever the user inputs the 5 digits.

Code:

```//Author: Kenneth Watkins #include <iostream> using namespace std; int main() { int pin;     cout << "Welcome to Movie Food\nEnter your 5-digit pin code: " ;     cin >> pin;     cout << endl;         while (pin != 76087)     {           if (pin >= 10000 && pin <= 76086 || pin >= 76088 && pin <= 99999)           {           cout << "yes" << endl;           break;           }           else           {           cout << "Welcome to Movie Food\nEnter your 5-digit pin code: " ;           cin >> pin;           cout << endl;               break;               } }           if (pin = 76087)           cout << "term" << endl;                                     system ("pause");       return 0; }```
• March 9th, 2013, 06:12 PM
GCDEF
Re: trouble with while statements...what am i missing?
Do you know the difference between = and ==?

Not sure what this is trying to accomplish?
if (pin >= 10000 && pin <= 76086 || pin >= 76088 && pin <= 99999)
Seems like that could be simplified. You may want to use parens to group your conditions too.
• March 9th, 2013, 06:15 PM
psfign
Re: trouble with while statements...what am i missing?
UGH, i didnt realize i had a = instead of ==. but yes, == is equal to. tbh, im not sure when i would use = instead
• March 9th, 2013, 07:38 PM
GCDEF
Re: trouble with while statements...what am i missing?
Quote:

Originally Posted by psfign
UGH, i didnt realize i had a = instead of ==. but yes, == is equal to. tbh, im not sure when i would use = instead

= assigns a value to a variable.
• March 9th, 2013, 08:16 PM
psfign
Re: trouble with while statements...what am i missing?
so it seems like iif im trying to do if statements within a while statement, it only works if i have two ifs.

for example on this code, i have 5 letter choices the user can input, but if i add more than two if statements than the program doesnt function properly.

Code:

```          while (letter != Q)           {                 if (letter = B)                 {                 cout << "You selected Beverage\n";                 cout << "Input 5.00\n";                 cin >> amount;                 change = amount - BEVERAGE_COST;                 cout << fixed << showpoint << setprecision(2);                 cout << "Change returned: " << change << "\n" << endl;                 break;                 }                 else if (letter = C)                 {                 cout << "You selected Candy\n";                 cout << "Input 2.25\n";                 cin >> amount;                 change = amount - CANDY_COST;                 cout << fixed << showpoint << setprecision(2);                 cout << "Change returned: " << change << "\n" << endl;                 break;                 }```
That code functions fine, but if i add
Code:

```          while (letter != Q)           {                 if (letter = B)                 {                 cout << "You selected Beverage\n";                 cout << "Input 5.00\n";                 cin >> amount;                 change = amount - BEVERAGE_COST;                 cout << fixed << showpoint << setprecision(2);                 cout << "Change returned: " << change << "\n" << endl;                 break;                 }                 else if (letter = C)                 {                 cout << "You selected Candy\n";                 cout << "Input 2.25\n";                 cin >> amount;                 change = amount - CANDY_COST;                 cout << fixed << showpoint << setprecision(2);                 cout << "Change returned: " << change << "\n" << endl;                 break;                 }                 if (letter = H)                 {                 cout << "You selected Hot Dog\n";                 cout << "Input 7.00\n";                 cin >> amount;                 change = amount - HOTDOG_COST;                 cout << fixed << showpoint << setprecision(2);                 cout << "Change returned: " << change << "\n" << endl;                 break;                 }                 }```
it no longer works right
• March 9th, 2013, 09:35 PM
psfign
Re: trouble with while statements...what am i missing?
ok, i figured it out. I have to do cases...
• March 9th, 2013, 10:36 PM
Coder Dave
Re: trouble with while statements...what am i missing?
Quote:

Originally Posted by psfign
Code:

```...         while (pin != 76087)         {                 if (pin >= 10000 && pin <= 76086 || pin >= 76088 && pin <= 99999)                 {                         cout << "yes" << endl;                         break;                 }                 else                 {                         cout << "Welcome to Movie Food\nEnter your 5-digit pin code: " ;                         cin >> pin;                         cout << endl;                         break;                 }     }  ...```

Your while loop will be executed only one time at most, since you are breaking out of it under both conditions of the if statement. Breaking out of loops is also considered to be sloppy coding because it makes the code harder to understand.

Code:

`if (pin = 76087)`
Code:

`if (letter = B)`
Code:

`else if (letter = C)`
Code:

`if (letter = H)`
You keep using the assignment operator = instead of the comparison operator == which is why your code is not working properly. Now I would agree that allowing assignments inside of an if statement is probably one of the dumbest things ever put into a programming language, but it is something we have to live with unless they ever change the language to disallow it.

Also, unless Q, B, C, and H are defined as chars and set to appropriate values, your code will not work properly either (or even compile). Character constants must be enclosed in single quotes.
• March 10th, 2013, 12:43 PM
psfign
Re: trouble with while statements...what am i missing?
Thanks Dave, I did have them set to char. But yea, i keep using = wrong, i switched them to == and it worked.
• March 10th, 2013, 02:29 PM
2kaud
Re: trouble with while statements...what am i missing?
Looking at your pin input, this would usually be coded using a do loop rather than a while so that the output/input statements need only be stated once.

Code:

```#include <iostream> using namespace std; int main() { int pin;       do {                 cout << "Welcome to Movie Food\nEnter your 5-digit pin code: " ;                 cin >> pin;                 cin.ignore(1000, '\n');                 cout << endl;         } while (pin < 10000 || pin > 99999);         if (pin == 76087)             cout << "term" << endl;         system ("pause");         return 0; }```
• March 10th, 2013, 03:26 PM
psfign
Re: trouble with while statements...what am i missing?
that makes sense. Can I also mix while statements with for statements? Im doing a number classifying project now that is requiring me to display:
1. A count of the number of integers entered
2. The sum of the integers
3. The average of the integers
4. The number of odd integers
5. The number of even integers
6. The number of zeros.

I have the number of integers and average done, but when I try to do the evens ( that's as far as ive gotten) it always displays there's zero evens.
• March 10th, 2013, 05:25 PM
psfign
Re: trouble with while statements...what am i missing?
well, i think i found the conflict, but i cant figure out how to get the while statement and the for statement to co-exist. if i remove the while statement, the program does count the evens. I am required to do the SENTINEL statement, so would i leave the while statement out?

Code:

```#include <iostream> #include <iomanip> using namespace std; const int SENTINEL = -999; const int N = 5; int main () {     int number;     int sum = 0;     int count = 0;     int odds = 0;     int evens = 0;     int zeros = 0;     int counter;     int num;     cout << "Enter integers ending with " << SENTINEL << endl;     cin >> number;         while (number != SENTINEL)     {           sum = sum +  number;           count++;           cin >> number;           }                       cout << "The number of integers is: " << count << endl;         if(count !=0)     {             cout << "The average is " << sum / count << endl; }                 for (counter = 1; counter <= number; counter++)         {             cin >> number;             cout << number << " ";                         switch (number % 2)             {             case 0:             evens++;             if (number == 0)             zeros++;             break;             case 1:             case -1:                   odds++;                   }                   }                   cout << endl;                   cout << "There are " << evens << " evens, " << endl;                 system ("pause");       return 0; }```
• March 10th, 2013, 05:27 PM
2kaud
Re: trouble with while statements...what am i missing?
Quote:

Can I also mix while statements with for statements?
Not sure I understand what you mean. A for loop can contain a while loop and a while loop can contain a for loop. Post your code and we'll see what you are trying to do.
• March 10th, 2013, 05:30 PM
psfign
Re: trouble with while statements...what am i missing?
i just posted it right as you were replying
• March 10th, 2013, 05:39 PM
2kaud
Re: trouble with while statements...what am i missing?
You need to do the odd, even and zero tests within the while loop. You don't need a seperate for loop. So your switch will go in the while loop.

Code:

```while (number != SENTINEL)     {           sum = sum +  number; //YOU NEED TO DO THE TEST FOR ODD, EVEN AND ZERO HERE           count++;           cin >> number;           }```
• March 10th, 2013, 06:03 PM
psfign
Re: trouble with while statements...what am i missing?
boom!

Code:

``` #include <iostream> #include <iomanip> using namespace std; const int SENTINEL = -999; int main () {     int number;     int sum = 0;     int count = 0;     int odds = 0;     int evens = 0;     int zeros = 0;     int counter;     int num;     cout << "Enter integers ending with " << SENTINEL << endl;     cin >> number;         while (number != SENTINEL)     {           sum = sum +  number;                   {           switch (number % 2)             {             case 0:             evens++;             if (number == 0)             zeros++;             break;             case 1:             case -1:                   odds++;                   }                   }           count++;           cin >> number;     }     system("cls");                  cout << "Count: " << count << endl;     cout << "Sum: " << sum << endl;     if(count !=0)     {             cout << "Average " << sum / count << endl;             cout << "Even: " << evens << endl;             cout << "Odds: " << odds << endl;             cout << "Zeros: " << zeros << endl;             }                 system ("pause");       return 0; }```
Show 50 post(s) from this thread on one page
Page 1 of 2 12 Last