My functions are messed up and won't work as I thought it would

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  #include <iostream>
#include <string>
using namespace 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);
}
 
if(int x == anything)

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.
Last edited on
I understand what I did wrong now thanks a lot!
Topic archived. No new replies allowed.