Need help with this simple C++ Console app.

Started by
9 comments, last by DividedByZero 15 years, 8 months ago
Ok, I am typing a simple console program that reads an integer from the user and tells if it is odd or even. Here is the code I got: ----------------------------------------------------- // odd or even, Kyle Conrad, 7/27/08 // reads an integer and tells if it's odd or even. #include <iostream> using std::cout; using std::cin; using std::endl; int main() { int number; int remainder; cout << "Enter an integer: "; cin >> number; remainder = number % 2; if ( remainder = 0 ) cout << number << " is even." << endl; if ( remainder != 0 ) cout << number << " is odd." << endl; system("pause"); return 0; } ---------------------------------------------------- When I run the program it starts just fine, but the problem is, when you enter an integer and hit enter it just skips my if statements, the problem is the if statements, I even made it print remainder just to see if I was getting 0 for even numbers and 1 for odd numbers, and sure enough I am. So I don't know what's wrong with my if statements but it's like they're being ignored or something. Please, any help would be appreciated.
-----------------------------------------"With the mind, anything is possible"
Advertisement
For one you have:
if( remainder = 0 )

which means you are setting remainder to 0 and therefore the if statement will always be true. Also, for something like the way you're doing, an else-if concept would be the way to go:
/* odd or even, Kyle Conrad, 7/27/08 reads an integer and tells if it's odd or even.*/#include <iostream>using std::cout;using std::cin;using std::endl;int main(){int number;int remainder;cout << "Enter an integer: ";cin >> number;remainder = number % 2;if ( remainder == 0 ){     cout << number << " is even." << endl;}else{     cout << number << " is odd." << endl;}system("pause");return 0;}


:]
~Maverick
Holy crap, you can read!
if ( remainder = 0 )
cout << number << " is even." << endl;


here your setting remainder to 0, you want to use == to check for equality.

Edit: bah, to slow, what PCN said.
@vs322: [laughs] Well it's the thought that counts. If it makes you fell any better, I'll rate you up. :]
Holy crap, you can read!
You could also use 'using namespace std;' instead of accessing each member in the C++ standard library individually. Also, you don't need to use curly braces if you only have one line in an if statement.

/* odd or even, Kyle Conrad, 7/27/08 reads an integer and tells if it's odd or even.*/#include <iostream>using namespace std;int main(){int number;cout << "Enter an integer: ";cin >> number;// you can just do comparison with number// instead of creating remainder to compareif ( number % 2 == 0 )  cout << number << " is even." << endl;else  cout << number << " is odd." << endl;system("pause");return 0;}
Thanks for the help guys, I totally forgot about == instead of =. Oh, and Moonshoe, I know about the whole 'using namespace std;' thing already, I just do it that way right now because that's how the book does it, also, this was an exercise in the book at the end of a chapter, so it was all me doing it, meaning it was a mess up on my part and not just miss typing the source code from the book.
-----------------------------------------"With the mind, anything is possible"
Quote: Oh, and Moonshoe, I know about the whole 'using namespace std;' thing already, I just do it that way right now because that's how the book does it...
Actually, the way you're doing it now (using 'using' declarations) is arguably superior to simply placing the using directive 'using namespace std' at the head of your file.

For more info on 'using' directives and declarations (and when not to use them), just type 'using namespace std' into the forum search box and check out some of the threads that come up.
It is also good practice to get keyboard input and store it it a 'char' type variable, because if your program prompts for an integer and you input some value other than an integer it will set the istream failbit and forever loop.
If you want to see it happen, type in a character when it asks for an integer.
Quote:Original post by bitshifter
It is also good practice to get keyboard input and store it it a 'char' type variable, because if your program prompts for an integer and you input some value other than an integer it will set the istream failbit and forever loop.
If you want to see it happen, type in a character when it asks for an integer.


What you actually want to do is check if the read operation succeeded, not force it to succeed. If you input into a char and the user enters "10", you're only going to get '1'. If you read it into an int, you check for success by:

if( in >> some_int )
{
//input succeeded
}
else
{
//wasn't able to read an int, fail bit on stream is set
}
Quote:Thanks for the help guys, I totally forgot about == instead of =.


A good habit to get into is to put the constant on the left as in

if(0 == remainder)

that way you if you accidentally use = instead of == the compiler will complain.
Humble people don't refer to themselves as humble (W.R.T. "IMHO".)

This topic is closed to new replies.

Advertisement