Sep 14, 2012 at 9:50pm UTC
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 Sep 14, 2012 at 9:51pm UTC
Sep 14, 2012 at 9:57pm UTC
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 Sep 14, 2012 at 10:04pm UTC
Sep 14, 2012 at 10:05pm UTC
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 Sep 14, 2012 at 10:05pm UTC
Sep 14, 2012 at 10:09pm UTC
Remove here type specifier int
cin >> int beverage;
It is strange that MS VC++ compiles this code...
Last edited on Sep 14, 2012 at 10:09pm UTC
Sep 14, 2012 at 10:10pm UTC
Cin>>beverage not int beverage
Sep 14, 2012 at 10:11pm UTC
Issue still persists, even whencin >> beverage;
Solution is beyond me...
Sep 14, 2012 at 10:16pm UTC
Please, show the result code.
Sep 14, 2012 at 10:23pm UTC
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 Sep 14, 2012 at 10:26pm UTC
Sep 14, 2012 at 10:36pm UTC
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.
Sep 15, 2012 at 10:57am UTC
Creating a new project and copypasting what i have posted on this forum into the new project, strangely it works.
Sep 15, 2012 at 11:30am UTC
There is nothing strange. You did not even compile this module. Instead it you compiled the main module in the project not this one.
Sep 15, 2012 at 11:52am UTC
Main module? Elaborate, please, i'm very new to this obviously...