Sign in to follow this  
Kaycon11

Need help with this simple C++ Console app.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 compare
if ( number % 2 == 0 )
cout << number << " is even." << endl;

else
cout << number << " is odd." << endl;

system("pause");
return 0;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by px
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.

Nice one!
I didn't realize this. You learn something every day! :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this