Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if statements always execute

I'm having issues with my if statements running into each other. Here is my code:

std::cout << "1) Option 1\n";
std::cout << "2) Option 2\n";
std::cout << "3) Option 3\n";
std::cout << "4) Option 4\n";
std::cout << "Type your choice and hit ENTER \n";

std::cin >> Choice;

if(Choice == 1); 
{
std::cout << "Blah Blah\n";
}
if(Choice == 2);
{
std::cout << "Blah Blah\n";
}
if(Choice == 3);
{
std::cout << "Blah Blah\n";
}
if(Choice == 4);
{
std::cout << "Blah Blah\n";
}

By running into each other I mean: it will just ignore my if statements and run all of my code so it would just print out:

Blah Blah
Blah Blah
Blah Blah
Blah Blah

What is my mistake?

like image 555
THUNDERGROOVE Avatar asked Nov 27 '22 01:11

THUNDERGROOVE


1 Answers

Your semicolons need to be removed, they are terminating the if statement.

if(Choice == 1)
{
std::cout << "Blah Blah\n";
}
if(Choice == 2)
{
std::cout << "Blah Blah\n";
}
if(Choice == 3)
{
std::cout << "Blah Blah\n"
}
if(Choice == 4)
{
std::cout << "Blah Blah\n";
}

You could use else ifs as well to clean up your code.

like image 189
Mansfield Avatar answered Dec 06 '22 01:12

Mansfield