Switch statement for Coke Machine code - c++

I have always used if else statements over switch statements, but I decided I wanted to try a switch out. I did the basic Coke Machine program with a switch and I cannot for the life of me figure out why it does not work how it should. When I use a number 1-5 for my input it continues to give the the switch default error message instead of the case cout statements (such as "You chose Coke"). Obviously something must be wrong that I am not seeing/
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Beverage List" << endl;
cout << "Coke = 1" << endl;
cout << "Dr. Pepper = 2" << endl;
cout << "Water = 3" << endl;
cout << "Sprite = 4" << endl;
cout << "Lemonade = 5" << endl << endl << endl;
cout << "Enter a number to choose a beverage: ";
cin >> number;
switch (number)
{
case '1':
cout << "You chose Coke";
break;
case '2':
cout << "You chose Dr. Pepper";
break;
case '3':
cout << "You chose Water";
break;
case '4':
cout << "You chose Sprite";
break;
case '5':
cout << "You chose Lemonade";
break;
default:
cout << "Error: Choice was not valid. Here is your money back.";
}
cout << "\n";
system("pause");
return 0;
}

The character '1' is not the same as the number 1.
Change
case '1':
to
case 1:

It looks like your case statements are comparing chars, not integers:
case '5':
Try this instead:
case 5:

Related

Functional Loop with user input C++

So I got an assignment to make a program that allows the user to select three favourite destinations in order. It repeats until the user decides to stop. Once the user decides to discontinue, the program then displays the total votes received for each destination according to preference by the users. One user will have three preferences and if the program repeats four times, it means four users’ preferences are recorded. Therefore a total of 12 preferences are recorded in this instance.
I have tried to limit input for the loop to work but it seems it will only work with a decision which is not necessary at the beginning of the program, which i want to remove altogether.
Also, I have tried to limit output for each of the decisions but it will only run once and then move on to the next choice. Is there any way to get a persistent entry prompt that will only continue after a valid input.
Lastly, is there any way I could improve the code by using switch/break statements instead of if/else?
Here's my code:
cout << "Do you want to go forth with this program?\nType y to confirm. The
program will exit if anything else is entered: ";
cin >> Decision;
while (Decision=="y")
{
cout << "\n\nNow please enter the code for which your destination corresponds to: " << endl; //first decision
cin >> Choice1;
if (Choice1 == 1)
{
LasVegas1++;
}
else if (Choice1 == 2)
{
Tokyo1++;
}
if (cin.fail())
{
cout << "Please enter a valid choice" << endl;
continue;
}
cout << " \nNow please enter the second code: " << endl; //second decision
cin >> Choice2;
if (Choice2 == 1)
{
LasVegas2++;
}
else if (Choice2 == 2)
{
Tokyo2++;
}
else
{
cout << "\nError! Please enter a valid code as shown above!\n";
cout << "\nNow please enter the second code: ";
cin >> Choice2;
}
cout << " \nNow please enter the third code: " << endl; //third decsion
cin >> Choice3;
if (Choice3 == 1)
{
LasVegas3++;
}
else
{
cout << "\nError! Please enter a valid code as shown above!\n";
cout << "\nNow please enter the third code: ";
cin >> Choice3;
}
cout << " \nDo you wish to select three more destinations? (Y/N): " << endl;
cin >> Decision;
}
What I would do is to put all your city variables into an array and then convert your three sets of code into a for loop. Something like:
for(int i = 0; i < 3; i++) {
if(Choice1 == 0) {
rome[i]++;
}
//etc
So that way you wouldn't need to repeat the same code three times. Also you only need one Choice variable. (You can just reset it at each iteration of the loop)
Additionally you could implement a switch statement to clean up the code a little:
switch(Choice1) {
case 1:
LasVegas1++;
break;
case 2:
Tokyo1++;
break;
case 3:
London1++;
break;
case 4:
Paris1++;
break;
case 5:
Dubai1++;
break;
case 6:
Mumbai1++;
break;
case 7:
NewYork1++;
break;
case 8:
Sydney1++;
break;
case 9:
Auckland1++;
break;
case 10:
Rome1++;
break;
case 11:
Other1++;
break;
}
It can be heavily simplified by the use of "Switch" and "Break".
Like the code that i have written. Have a look :
char input;
int choice;
void Permission() {
cout << "Do you want to go forth with this program ? (y to confirm)" << flush;
cin >> input;
cout << endl;
}
void Decision1() {
if(input == 'y') {
cout << "Now please enter the code for which your destination corresponds to : " << flush;
cin >> choice;
switch (choice) {
case 1:
cout << "LasVegas " << endl;
break;
case 2:
cout << "Tokyo " << endl;
break;
case 3:
cout << "London " << endl;
break;
case 4:
cout << "Paris " << endl;
break;
case 5:
cout << "Dubai " << endl;
break;
case 6:
cout << "Mumbai " << endl;
break;
case 7:
cout << "New York " << endl;
break;
case 8:
cout << "Sydney " << endl;
break;
case 9:
cout << "Auckland " << endl;
break;
case 10:
cout << "Rome " << endl;
break;
case 11:
cout << "Other " << endl;
break;
default:
cout << "Invalid option. Enter Again : " << flush;
cin >> choice;
}
cout << endl;
}
}
void Decision2() {
cout << "Now please enter the second code: " << flush;
cin >> choice;
switch (choice) {
case 1:
cout << "LasVegas " << endl;
break;
case 2:
cout << "Tokyo " << endl;
break;
case 3:
cout << "London " << endl;
break;
case 4:
cout << "Paris " << endl;
break;
case 5:
cout << "Dubai " << endl;
break;
case 6:
cout << "Mumbai " << endl;
break;
case 7:
cout << "New York " << endl;
break;
case 8:
cout << "Sydney " << endl;
break;
case 9:
cout << "Auckland " << endl;
break;
case 10:
cout << "Rome " << endl;
break;
case 11:
cout << "Other " << endl;
default:
cout << "Invalid option. Enter Again : " << flush;
cin >> choice;
}
cout << endl;
}
void Decision3() {
cout << "Now please enter the third code: " << flush;
cin >> choice;
switch (choice) {
case 1:
cout << "LasVegas " << endl;
break;
case 2:
cout << "Tokyo " << endl;
break;
case 3:
cout << "London " << endl;
break;
case 4:
cout << "Paris " << endl;
break;
case 5:
cout << "Dubai " << endl;
break;
case 6:
cout << "Mumbai " << endl;
break;
case 7:
cout << "New York " << endl;
case 8:
cout << "Sydney " << endl;
break;
case 9:
cout << "Auckland " << endl;
break;
case 10:
cout << "Rome " << endl;
break;
case 11:
cout << "Other " << endl;
break;
default:
cout << "Invalid option. Enter Again : " << flush;
cin >> choice;
}
cout << endl;
}
int main() {
Permission();
Decision1();
Decision2();
Decision3();
system("PAUSE");
return 0;
}
I don't know that how to get the loop at the default when the user enters the wrong option. I was able to give the "cin" once. Update this problem if you know how to.
Yes you could improve with a switch statement. Theres also something called a do while loop you should look into.

How to make a program loop forever until a certain case in a switch statement is met?

How can I make this program loop forever unless the user chooses 'C' in the switch statement? I've tried several things on my own that didn't seem to work and I am not sure how to proceed, since I am still sort of a beginner in C++. Can anyone help? Let me know if you need additional info.
#include <iostream>
using namespace std;
int main()
{
cout << "Hello user, what do you want to do: " << endl
<< "A. Display A Message" << endl
<< "B. Perform A Calculation" << endl
<< "C. Exit The Program" << endl;
int result = 80 + 10;
char answer;
cin >> answer;
switch (answer)
{
case 'A':
cout << "Welcome to C++!" << endl;
break;
case 'B':
cout << 80 << " + " << 10 << " = " << result << endl;
break;
case 'C':
cout << "Goodbye...." << endl;
break;
return 0;
}
}
(Currently your return 0; statement is unreachable.)
The simplest way is to use a for(;;) infinite loop idiom, and a return in the case 'C' case in place of the break:
for (;;){
char answer;
cin >> answer;
switch (answer){
case 'A':
cout << "Welcome to C++!" << endl;
break;
case 'B':
cout << 80 << " + " << 10 << " = " << result << endl;
break;
case 'C':
cout << "Goodbye...." << endl;
return 0;
}
}
The break statements take program control out of the switch but not the loop.
We tend to use for(;;) over alternatives such as while(true) since many compilers accept the former without a warning.
Or
bool finished=false;
while (!finished)
{
switch (something)
{
case foo:
finished = true; break;
...
}
}
Or one of a dozen ways to achieve the same thing ;)

I'm not sure how to assign the values I want exactly

I need cherries, oranges, plumes, bells, melons, or bars to be randomly picked in the case statements and in a way I can then display what was chosen so I can compare them, but I'm not sure how.
For example, I was hoping when I printed slot1, slot2, and slot3, I would get the names of which case statement inside each of the three switches were chosen.
Not their numbers. (The program isn't done yet so it's quite messy right now)
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int main()
{
int slot1;
int slot2;
int slot3;
double won;
double money;
string cherries;
string oranges;
string plums;
string bells;
string melons;
string bars;
string doAgain;
do
{
cout << "We are going to be playing a slot machine game today." << endl;
cout << "Please enter the amount of money you'd like to insert into the slot machine." << endl;
cin >> money;
cout << "You put in $" << money << endl;
srand(time(0));
slot1=rand()%6+1;
slot2=rand()%6+1;
slot3=rand()%6+1;
switch (slot1)
{
case 1:
cout << cherries << endl;
case 2:
cout << oranges << endl;
break;
case 3:
cout << plums << endl;
break;
case 4:
cout << bells << endl;
break;
case 5:
cout << melons << endl;
break;
case 6:
cout << bars << endl;
}
switch (slot2)
{
case 1:
cout << melons << endl;
break;
case 2:
cout << bells << endl;
break;
case 3:
cout << bars << endl;
break;
case 4:
cout << plums << endl;
break;
case 5:
cout << oranges << endl;
break;
case 6:
cout << cherries << endl;
}
switch (slot3)
{
case 1:
cout << bars << endl;
break;
case 2:
cout << plums << endl;
break;
case 3:
cout << melons << endl;
break;
case 4:
cout << bells << endl;
break;
case 5:
cout << oranges << endl;
break;
case 6:
cout << cherries << endl;
}
cout << "The numbers you got were " << slot1 << ", " << slot2 << ", " << slot3 << endl;
cout << "Would you like to play again?" << endl;
cin >> doAgain;
if(doAgain!= "yes")
{
cout << "The total amount of money you put in the slot machine is" << money << endl;
cout << "The total amount of money you won is $" << won << endl;
}
}
while(doAgain=="yes");
return 0;
}
enter code here
You have declared strings for all the various fruits, but you don't assign any actual string values to them. ie string cherries = "cherries"
Just printing slot1 will only print an int as you have discovered. C++ doesn't know that you also want to print the name as well. You need to include your string as part of the cout statement

My menu repeats itself too many times when I enter a string of characters

So here's my code
do
{
cout << "Welcome to our Coffee Shop! Here are the options: " << endl;
cout << "C - Coffee ($1.50)" << endl;
cout << "T - Tea ($1.00)" << endl;
cout << "S - Soda ($1.00)" << endl;
cout << "J - Juice ($1.50)" << endl;
cout << "M - Manager Special ($2.00)" << endl;
cout << "X - Finish Order" << endl;
cout << "What drink would you like? Enter C, T, S, J, M, or X." << endl;
cin >> input;
//does certain actions for a certain character
if(input)
{
switch(input)
{
case 'C':
case 'c': total += coffeePrice;
coffeeCount++;
cout << "Thanks! You have ordered coffee." << endl;
break;
case 'T':
case 't': total += teaPrice;
teaCount++;
cout << "Thanks! You have ordered tea." << endl;
break;
case 'S':
case 's': total += sodaPrice;
sodaCount++;
cout << "Thanks! You have ordered soda." << endl;
break;
case 'J':
case 'j': total += juicePrice;
juiceCount++;
cout << "Thanks! You have ordered juice." << endl;
break;
case 'M':
case 'm': total += specialPrice;
specialCount++;
cout << "Thanks! You have ordered the manager special." << endl;
break;
default:
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
break;
}
}
else
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
I've been trying to make a menu that only reads one character and does something as a result. The problem is when I enter a string that has multiple c's like "cccc" the menu repeats itself 4 times. Or if I enter a string that has some c's and other's that are not correct responses like, "ccddcc" the menu will say thanks for the coffee twice then repeat the menu twice and then say thanks for the coffee twice again. Is there a way to stop this from happening. I just want it to ignore any response other than a 'c' or a correct menu option.
What I would think the problem here is, is that input'is a char. Thus, when you input cccc, it will read in the first char and move on, come back and see that there are still more c's in the std input waiting to be processed, so it will take that next c and process again. I would recommend making your input var a string, and then if that string was greater than size 1, either leave or just use the first char.
char input[256]; // Could probably be a string, but I am used to C for this
do
{
cout << "Welcome to our Coffee Shop! Here are the options: " << endl;
cout << "C - Coffee ($1.50)" << endl;
cout << "T - Tea ($1.00)" << endl;
cout << "S - Soda ($1.00)" << endl;
cout << "J - Juice ($1.50)" << endl;
cout << "M - Manager Special ($2.00)" << endl;
cout << "X - Finish Order" << endl;
cout << "What drink would you like? Enter C, T, S, J, M, or X." << endl;
cin >> input;
switch(input[0])
{
case 'C':
case 'c': total += coffeePrice;
coffeeCount++;
cout << "Thanks! You have ordered coffee." << endl;
break;
case 'T':
case 't': total += teaPrice;
teaCount++;
cout << "Thanks! You have ordered tea." << endl;
break;
case 'S':
case 's': total += sodaPrice;
sodaCount++;
cout << "Thanks! You have ordered soda." << endl;
break;
case 'J':
case 'j': total += juicePrice;
juiceCount++;
cout << "Thanks! You have ordered juice." << endl;
break;
case 'M':
case 'm': total += specialPrice;
specialCount++;
cout << "Thanks! You have ordered the manager special." << endl;
break;
default:
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
break;
}
}
else
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}

C++ Simple input/out program + enumerators

Assignment"Rewrite the menu chooser program from chapter using an enumerator to represent difficulty levels. the variable choice will still be of type int."
The first set of code is the original menu chooser program in its original untainted form. The second set of code is what I added to it in order to complete the assignment.
The only thing I want to ask is: Did I complete my assignment correctly. If I did it wrong, can someone please explain what I did wrong. I'm very new at this.
Code Set # 1 - Original
#include <iostream>
using namespace std;
int main()
cout << "Difficulty Levels\n\n";
cout << "1 - Easy\n";
cout << "2 - Normal\n";
cout << "3 - Hard\n";
int choice;
cout << "Choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "You picked Easy.\n";
break;
case 2:
cout << "You picked Normal.\n";
break;
case 3:
cout << "You picked Hard.\n";
break;
default:
cout << "You made an illegal choice.\n";
}
return 0;
}
Code Set # 2 - Assignment
#include <iostream>
using namespace std;
int main()
{
cout << "Difficulty Levels\n\n";
cout << "0 - Novice\n";
cout << "1 - Easy\n";
cout << "2 - Normal\n";
cout << "3 - Hard\n";
cout << "4 - Unbeatable\n\n";
enum {Novice = 0, Easy = 1, Normal = 2, Hard = 3, Unbeatable = 4};
int choice;
cout << "Choice: ";
cin >> choice;
switch (choice)
{
case 0:
cout << "You have picked Novice.\n";
break;
case 1:
cout << "You picked Easy.\n";
break;
case 2:
cout << "You picked Normal.\n";
break;
case 3:
cout << "You picked Hard.\n";
break;
case 4:
cout << "You picked Unbeatable.\n";
break;
default:
cout << "You made an illegal choice.\n";
}
return 0;
}
I would do something like this:
#include <iostream>
using namespace std;
int main()
{
cout << "Difficulty Levels\n\n";
cout << "0 - Novice\n";
cout << "1 - Easy\n";
cout << "2 - Normal\n";
cout << "3 - Hard\n";
cout << "4 - Unbeatable\n\n";
enum {NOVICE = 0, EASY = 1, NORMAL = 2, HARD = 3, UNBEATABLE = 4};
int choice;
cout << "Choice: ";
cin >> choice;
switch (choice) {
case NOVICE:
cout << "You have picked Novice.\n";
break;
case EASY:
cout << "You picked Easy.\n";
break;
case NORMAL:
cout << "You picked Normal.\n";
break;
case HARD:
cout << "You picked Hard.\n";
break;
case UNBEATABLE:
cout << "You picked Unbeatable.\n";
break;
default:
cout << "You made an illegal choice.\n";
break;
}
return 0;
}
This way you're showing you're at least using your enum.