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;
}
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.
Last edited by GCDEF; March 9th, 2013 at 06:14 PM.
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.
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.
Last edited by Coder Dave; March 9th, 2013 at 10:39 PM.
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;
}
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
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.
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;
}
Re: trouble with while statements...what am i missing?
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.
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
Bookmarks