Reputation: 49
I have written this C++ code in an attempt to simulate a vending machine. Whenever the user's selection is entered, a number 1-5 each assigned a case in the switch statement, none of the cases execute: the program does not recognize the valid input, and will only print out the default case statement. Why is this happening?
#include <iostream>
using namespace std;
int main()
{
int choice;
double dollars;
cout << "==============================================================\n";
cout << "* *\n";
cout << "* VENDING MACHINE SIMULATOR *\n";
cout << "* *\n";
cout << "==============================================================\n";
cout << "Your Choices Are:\n";
cout << "1) Coke - $1.75\n";
cout << "2) Pepsi - $2.25\n";
cout << "3) Sprite - $1.25\n";
cout << "4) 7-up - $1.15\n";
cout << "5) Water - $2.20\n";
// Ask for user's selection.
cout << "What would you like to purchase today?\n";
cin >> choice;
switch (choice)
{
case '1':
{
cout << "How many dollar bills will you use?\n";
cin >> dollars;
{
if (dollars > 1.75)
{
cout << "You purchased: Coke\n";
cout << "Your change is: $"<< (dollars - 1.75);
}
else
{
cout << "You do not have enough money for this purchase. Goodbye!\n";
}
}
}
break;
case '2':
{
cout << "How many dollar bills will you use?\n";
cin >> dollars;
{
if (dollars > 2.25)
{
cout << "You purchased: Pepsi\n";
cout << "Your change is: $"<< (dollars - 1.75);
}
else
{
cout << "You do not have enough money for this purchase. Goodbye!\n";
}
}
}
case '3':
{
cout << "How many dollar bills will you use?\n";
cin >> dollars;
{
if (dollars > 1.25)
{
cout << "You purchased: Sprite\n";
cout << "Your change is: $"<< (dollars - 1.25);
}
else
{
cout << "You do not have enough money for this purchase. Goodbye!\n";
}
}
}
break;
case '4':
{
cout << "How many dollar bills will you use?\n";
cin >> dollars;
{
if (dollars > 1.15)
{
cout << "You purchased: 7-up\n";
cout << "Your change is: $"<< (dollars - 1.15);
}
else
{
cout << "You do not have enough money for this purchase. Goodbye!\n";
}
}
}
break;
case '5':
{
cout << "How many dollar bills will you use?\n";
cin >> dollars;
{
if (dollars > 2.20)
{
cout << "You purchased: Water\n";
cout << "Your change is: $"<< (dollars - 2.20);
}
else
{
cout << "You do not have enough money for this purchase. Goodbye!\n";
}
}
}
break;
default: cout << "Your choice is INVALID. Goodbye!\n";
}
return 0;
}
Upvotes: 0
Views: 1836
Reputation: 144
'break' statement is missing for Case 2 and change case '1': to case 1.
Upvotes: 1
Reputation: 180835
'1'
is a character. When you input 1
into an integer it is stored as 1
not '1'
. You need to change all the cases of 'x'
to x
.
The reason you do not get any errors about this is that '1'
can be implicitly converted to an int
and it has a value of the implementations character encoding. If it was ASCII the it would be 49
Upvotes: 3
Reputation: 206667
Drop the single quotes used in the case
statements.
Change
case '1':
to
case 1:
Similarly for others.
'1'
is a character constant while 1
is the integer constant. When ASCII encoding is used, '1'
evaluates to the number 49
, which clearly is not equal to the number 1
. Hence, that case:
branch is not executed when you enter 1
as your input.
Upvotes: 8
Reputation: 2544
Two solutions:
Change int choice
to char choice
Or
Change case '1':
to case 1:
.
Upvotes: 2