if, else if statements

Yet again, i encountered a problem using if, else if statements.
This time making the vending machine program in beginning excercises.
The program launches, however it will always give you a coca cola.

Here is the code;

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
#include<iostream>
using namespace std;

int main() {

	int beverage;
	
	cout << "Choose a beverage by entering its number!" << endl;
		cout << "1 - Coca Cola" << endl;
			cout << "2 - Fanta" << endl;
				cout << "3 - Mountain Dew" << endl;
					cout << "4 - Dr. Pepper" << endl;
						cout << "5 - RC Cola" << endl;
		cin >> int beverage;

	if (int beverage == 1)
		cout << "The vending machine gave you a " << "Coca Cola" << "!" << endl;
	else if (int beverage == 2)
		cout << "The vending machine gave you a " << "Fanta" << "!" << endl;
	else if (int beverage == 3)
		cout << "The vending machine gave you a " << "Mountain Dew" << "!" << endl;
	else if (int beverage == 4)
		cout << "The vending machine gave you a " << "Dr. Pepper" << "!" << endl;
	else if (int beverage == 5)
		cout << "The vending machine gave you a " << "RC Cola" << "!" << endl;

	system("pause");
	return 0;
}
Last edited on
In each if and else if statement you are declaring a new local variable beverage. Substitute the declaration for the expression

if ( beverage == 1)


By the way what compiler are you using?
Last edited on
I'm sorry, if i understood you correctly your solution did not help.
Here is what it looks like now. (Very little different)

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
#include<iostream>

using namespace std;

int main() {

	int beverage;
	
	cout << "Choose a beverage by entering its number!" << endl;
		cout << "1 - Coca Cola" << endl;
			cout << "2 - Fanta" << endl;
				cout << "3 - Mountain Dew" << endl;
					cout << "4 - Dr. Pepper" << endl;
						cout << "5 - RC Cola" << endl;
		cin >> int beverage;

	if ( beverage == 1) 
		cout << "The vending machine gave you a " << "Coca Cola" << "!" << endl;
	else if ( beverage == 2)
		cout << "The vending machine gave you a " << "Fanta" << "!" << endl;
	else if ( beverage == 3)
		cout << "The vending machine gave you a " << "Mountain Dew" << "!" << endl;
	else if ( beverage == 4)
		cout << "The vending machine gave you a " << "Dr. Pepper" << "!" << endl;
	else if ( beverage == 5)
		cout << "The vending machine gave you a " << "RC Cola" << "!" << endl;

	system("pause");
	return 0;

}


Microsoft visual c++ Express 2012
Last edited on
Remove here type specifier int

cin >> int beverage;

It is strange that MS VC++ compiles this code...
Last edited on
Cin>>beverage not int beverage
Issue still persists, even whencin >> beverage; Solution is beyond me...
Please, show the result code.
Looks like a solid structure to me.
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
#include<iostream>

using namespace std;

int main() {

	int beverage;
	
	cout << "Choose a beverage by entering its number!" << endl;
		cout << "1 - Coca Cola" << endl;
			cout << "2 - Fanta" << endl;
				cout << "3 - Mountain Dew" << endl;
					cout << "4 - Dr. Pepper" << endl;
						cout << "5 - RC Cola" << endl;
		cin >> beverage;

	if ( beverage == 1) 
		cout << "The vending machine gave you a " << "Coca Cola" << "!" << endl;
	else if ( beverage == 2)
		cout << "The vending machine gave you a " << "Fanta" << "!" << endl;
	else if ( beverage == 3)
		cout << "The vending machine gave you a " << "Mountain Dew" << "!" << endl;
	else if ( beverage == 4)
		cout << "The vending machine gave you a " << "Dr. Pepper" << "!" << endl;
	else if ( beverage == 5)
		cout << "The vending machine gave you a " << "RC Cola" << "!" << endl;

	system("pause");
	return 0;

}


Then again, i'm doing beginner excercises, haha...
The code shall work correctly. Check your project. Maybe you have other files in the project.
EDIT: Moreover I am sure that you are compiling and executing another module because this module in its original form shall not be compiled.
Last edited on
I have only my source.cpp which is the code i have shown you, and the external dependencies. Rest of it is empty.
[IMG]http://i.imgur.com/samqz.png[/IMG]
What is the error then?
It is if-statement module that is compiled not that one. Either create a new project or transfer the code from this module to if-statement module and after that delete this module.
Creating a new project and copypasting what i have posted on this forum into the new project, strangely it works.
There is nothing strange. You did not even compile this module. Instead it you compiled the main module in the project not this one.
Main module? Elaborate, please, i'm very new to this obviously...
Topic archived. No new replies allowed.