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.
Related
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.
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
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:
This is a block of code from a book I've been studying and trying to improve upon, but I'm having trouble finding a way to give the player another chance at selecting the difficulty after entering the default choice. This is a very simple console text-based game and when the player chooses an incorrect choice, the game doesn't allow the player to re-choose.
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Difficulty Levels\n\n";
cout << "1 - Easy\n";
cout << "2 - Normal\n";
cout << "3 - Hard\n\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 << "Your choice is invalid.\n";
}
system("pause");
return 0;
}
You can refactor the switch into a function, which can be called whenever the player wants to change their difficulty choice.
int choose()
{
int choice;
cout << "Difficulty Levels\n\n";
cout << "1 - Easy\n";
cout << "2 - Normal\n";
cout << "3 - Hard\n\n";
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 << "Your choice is invalid.\n";
choice = 0; //this will signal error
}
return choice;
}
int _tmain(int argc, _TCHAR* argv[])
{
int choice = 0;
while(choice == 0){choice = choose();};
system("pause");
return 0;
}
Now whenever the player decides to change difficulty (maybe they enter a special letter) you can use choice = choose() to alter the difficulty.
Wrap the input code in a do-while.
bool valid = true;
do
{
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 << "Your choice is invalid.\n";
valid = false;
}
} while(!valid);
I created a text-based game to help improve my C++ skills since I am very new to it. I'm trying to learn the basics n' all.
Here is the program. In the switch statement, if a person were to select "3" it would broadcast an Error saying you can only select "1" or "2".
** The issue is the program continues and doesn't make the person RECHOOSE the selection. It goes right to Difficulty selecting.
What method do I use to force the program to halt until player chooses valid selection?
Thanks!
#include <iostream>
using namespace std;
int main()
{
cout << "\tWelcome to my text based game!\n";
char userName[100];
cout << "\nPlease enter your username: ";
cin >> userName;
cout << "Hello, " << userName << "!\n\n";
cout << "Please pick your race: \n";
cout << "1 - Human\n";
cout << "2 - Orc\n";
int pickRace;
cout << "Pick your race: ";
cin >> pickRace;
switch (pickRace)
{
case 1:
cout << "You picked the Human race." << endl;
break;
case 2:
cout << "You picked the Orc race." << endl;
break;
default:
cout << "Error - Invalid input; only 1 or 2 allowed!" << endl;
}
int difficulty;
cout << "\nPick your level difficulty: \n";
cout << "1 - Easy\n";
cout << "2 - Medium\n";
cout << "3 - Hard\n";
cout << "Pick your level difficulty: ";
cin >> difficulty;
switch (difficulty)
{
case 1:
cout << "You picked Easy" << endl;
break;
case 2:
cout << "You picked Medium" << endl;
break;
case 3:
cout << "You picked Hard" << endl;
break;
default:
cout << "Error - Invalid input, only 1,2 or 3 are allowed" << endl;
}
return 0;
}
You need to use loops. Wrap the input and the switch after it in a loop and break out of it when the input is valid.
Use a do ... while loop, like this
int pickRace;
do
{
cout << "Please pick your race: \n";
cout << "1 - Human\n";
cout << "2 - Orc\n";
cout << "Pick your race: ";
cin >> pickRace;
switch (pickRace)
{
case 1:
cout << "You picked the Human race." << endl;
break;
case 2:
cout << "You picked the Orc race." << endl;
break;
default:
cout << "Error - Invalid input; only 1 or 2 allowed!" << endl;
break;
}
}
while (pickRace != 1 && pickRace !=2);
This will keep on looping while the condition at the bottom is true (i.e. while they haven't picked a valid option).
One other comment. Since you are new you should really get into the habit of using string not char arrays. Char arrays will get you into all sorts of trouble in the future, so start using strings now. There's no point in learning bad habits.
#include <string>
string userName;
cout << "\nPlease enter your username: ";
cin >> userName;
cout << "Hello, " << userName << "!\n\n";
You can do - while loop with flags. The flag is by default false therefore allowing only one time input. If there is a need to enter another time, determined and controlled by the default case, then the flag is set to true, which makes the loop iterate once more. At the beginning of each iteration flag is reset to false, so each time it is assumed that this is the last one. Using flag will make the breaking operation much simple, and avoid complex while conditions.
int flag = 0;
do
{
cout << "Please pick your race: \n";
cout << "1 - Human\n";
cout << "2 - Orc\n";
cout << "Enter Choice: ";
cin >> pickRace;
flag = 0;
switch (pickRace)
{
case 1:
cout << "You picked the Human race." << endl;
break;
case 2:
cout << "You picked the Orc race." << endl;
break;
default:
cout << "Error - Invalid input; only 1 or 2 allowed!" << endl;
flag = 1;
}
} while (flag);