My program would have 3 food options in 3 different functions but put into 1 function to check for what the variable x is and then output the cout in one of the 3 functions but for some reason if I put any of the number all the functions are output into the command line and i dont know how to fix so if someone can help please
#include <iostream>
#include <string>
usingnamespace std;
void Burger_fries(int x){
if (int x = 1) {
cout << "Thanks for choosing burger with fries" << endl;
}
}
void Pizza_with_hotdogs(int x)
{
if (int x = 2) {
cout << "Thanks for choosing pizza with hotdogs" << endl;
}
}
void Fried_Chicken(int x)
{
if (int x = 3) {
cout << "Thanks for choosing fried chicken" << endl;
}
}
void food (int x)
{
cin >> x;
Burger_fries(x);
Pizza_with_hotdogs(x);
Fried_Chicken(x);
}
int main()
{
int x;
cout << "The food options as listed are what we currently have to eat\n";
cout << "1. Cheese Burger with fries\n";
cout << "2. Pizza with a hot dog\n";
cout << "3. Fried chicken\n";
food(x);
}
By putting that int there you are defining another variable that hides the x that you use as parameter
To compare values use ==. = means assignment.
All the strings are printed because x = 1 (or 2 or 3) is translated to if(x). Since x = 1 that becomes if(1). Any number different from 0 is converted to TRUE so all the conditions are true.