Jump to content

Help with if statement

- - - - -

  • Please log in to reply
5 replies to this topic

#1
flhenderson

flhenderson

    Newbie

  • Members
  • Pip
  • 2 posts
Hello everyone, I'm having trouble with what should be a simple little program to tell weather a number is odd or even based on user input. The part I'm having trouble with is having an error message print out if the user enters anything other then a whole number. I'm sorry if this is a redundant post but Ive looked around for a while and can find nothing relevant to my problem. Any help that anyone could give is appreciated. Thank you

/****************************
* Is the number even or odd *
* By Frank Henderson *
* 12.29.11 2:29 am *
****************************/

#include <iostream>

using namespace std;


int main()

{


	int n, remainder;


	// Get a number from the user, if the user enters anything but a whole number, print error message


	cout << "\nEnter a number and press ENTER: ";

	cin >> n;


	if (n != (n / 2))

		cout << "You must enter whole numbers only, try again";


	// Get remainder after dividing by 2


	remainder = n % 2;


	// If the remainder is 0, the number input is even


	if (remainder == 0)

		cout << "The number is even" << endl << endl;

	else

		cout << "The number is odd" << endl << endl;

	return 0;

}




#2
zapdude1234

zapdude1234

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
why would you use n != (n/2)? imagine the number entered in 4. this would say if 4 != 2 which is true. try using if(0 != (n%2)). this says if(there is any remainder after dividing by 2) however, this also doesnt ensure that it is a unsigned int. i am no expert so please correct if im wrong but you could always use #include <math.h> and say something like if(0 != n - (math.floor(n))) <---probably syntactically wrong. also you probably need a while loop around the if statement so they can keep entering it. hope this helps

#3
flhenderson

flhenderson

    Newbie

  • Members
  • Pip
  • 2 posts
The original program is from a book called C++ without fear, and doesnt have that first if statement. When i copied and ran it it worked fine but the more i looked at it and thought about it i concluded that it would be better if there was a way to let the user no that they entered the wrong value but apparently my knowledge on the subject is still to limited. Thanks for your response

#4
AKMafia001

AKMafia001

    Programmer

  • Members
  • PipPipPipPip
  • 119 posts
In your program everything is gonna be a whole number. Because the variable n is of type int. So, even if the user enters a float value with fractional part the integer part will be stored and the fractional part will be droped...
I think i'm able to write a code for printing "Hello, World!". Proud of that!

#5
notes

notes

    Learning Programmer

  • Members
  • PipPipPip
  • 48 posts
Also you can check if cin was successfull, so that you will know if user didn't type character instead of integer.
 if(cin.fail()) {

//...

}

One more tip. You dont have to initialize reminder variable. You can do all in one statement :
if (n%2)

...

if(!n%2)

...

Remebre about KISS & DRY

#6
zapdude1234

zapdude1234

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
wow i completely forgot int values and whole numbers!! :p wayyy over thinking things! but good luck with the C++. probably the most beautiful language! also you can also checkout a video series "Wibit.net". it takes some time but the stuff they go over will really help!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users