If Statement Within a Switch

Hello! I am having a bit of difficulty with this. I wish for the program to output "The number zero is neither odd nor even." whenever user enters a 0. But when I run it, the program calls 0 even -- no doubt because of the condition "number_two == 0." Because "0" divided by two gives a remainder of zero, making it even, according to my rules. Where would I insert another option and cause the computer to ignore the fact that 0 % 2 yields 0?



#include <iostream>
using namespace std;
/* This program should allow user to input a number, then select a menu option, which will use
the original number for its calculations, then output the answer.*/
int main ()
{
int menu_choice;
int number;
int number_two;

cout << "Enter number: ";
cin >> number;

cout << "1) Is the number odd or even? \n";
cout << "2) Is the number positive or negative? \n";
cout << "3) What is the square root of the number?\n\n";

cout << "Enter a menu choice: ";
cin >> menu_choice;

switch(menu_choice)
{
case 1://currently just working on this first one

number_two = number % 2;
if (number = 0 && number_two == 0)//I believe that the issue is here
cout << "The number zero is neither odd nor even.";

else if (number_two == 1)
cout << "The number " << number << " is odd.";

else if (number_two == 0)
cout << "The number " << number << " is even.";

else
cout << "Please enter a valid number.";

case 2:
// Process
//Display positive, negative, or zero
case 3:
//Process
//Display square root

default:
cout << "Please enter a valid menu option.\n\n";
}
return 0;

}
Last edited on
First of all there is the assignment operator instead of the comparision operator

if (number = 0 && number_two == 0)//I believe that the issue is here

So 0 is assigned to number and the condition is always equal to false.

You could simply write for example

if ( number == 0 )
Ah. You are correct, Mister Vlad. I didn't realize that the '=' operator actually sets "number" to the value 0, even when being used inside the space used for test criteria for the if statement. I imagine that there is a useful application for that.

Many thanks!
Topic archived. No new replies allowed.