How to handle invalid inputs while displaying Game menus? - c++

#include <iostream>
#include <string>
using namespace std;
int main() { //Program starts
cout << "-------------------------------" << endl;
cout << "Welcome to Ninjas vs. Samurais!" << endl; //The intro
cout << "-------------------------------" << endl;
string newAdventure;
string chosenKind;
cout << "Hello, new solder! Are you ready for your adventure to begin, yes or no?\n"; //Asks you if you are ready
cin >> newAdventure;//Takes in if you are ready or not
if (newAdventure == "yes" || newAdventure == "Yes" || newAdventure == "Yes!") { //Asks if they are ready
cout << "Great!\n" << endl;
}
else if (newAdventure == "no" || newAdventure == "No" || newAdventure == "No!") { //Asks if they are ready
cout << "Too bad!\n" << endl;
}
else {
cout << "Please type a yes or no answer!\n";
}
system("PAUSE");
return 0;
}
If the user didn't input a valid answer, how could I make them restart the question? Would I have to use a loop? If so, how would I do that?

Yes you would have to use a loop. Something like:
while(cin >> newAdventure)//Takes in if you are ready or not
{
if (newAdventure == "yes" || newAdventure == "Yes" || newAdventure == "Yes!") { //Asks if they are ready
cout << "Great!\n" << endl;
break;
}
else if (newAdventure == "no" || newAdventure == "No" || newAdventure == "No!") { //Asks if they are ready
cout << "Too bad!\n" << endl;
break;
}
else {
cout << "Please type a yes or no answer!\n";
}
}
The break keyword will exit the loop once your answer is valid, otherwise, it keeps going.

You can add a condition like this:
while(true)
{
cout << "Hello, new solder! Are you ready for your adventure to begin, yes or no?\n"; //Asks you if you are ready
cin >> newAdventure;//Takes in if you are ready or not
if (newAdventure == "yes" || newAdventure == "Yes" || newAdventure == "Yes!") { //Asks if they are ready
cout << "Great!\n" << endl;
break;
}
else if (newAdventure == "no" || newAdventure == "No" || newAdventure == "No!") { //Asks if they are ready
cout << "Too bad!\n" << endl;
break;
}
else {
cout << "Please type a yes or no answer!\n";
}
}

Related

Why does my program terminate when it should continue?

#include <iostream>
#include <ctime>
using namespace std;
int main() {
int answer;
string question;
string play = "";
srand(time(NULL));
cout << "What question would you like to ask the Magic 8 ball?: \n";
cout << "\n";
cin >> question;
answer = rand () % 8 + 1;
if (answer == 1) {
cout << "The answer is: It is certain\n";
} else if (answer == 2) {
cout << "The answer is: It is decidely so\n";
} else if (answer == 3) {
cout << "The answer is: Most likely\n";
} else if (answer == 4) {
cout << "The answer is: Signs point to yes\n";
} else if (answer == 5) {
cout << "The answer is: Ask again later\n";
} else if (answer == 6) {
cout << "The answer is: Don't count on it\n";
} else if (answer == 7) {
cout << "The answer is: My sources say no\n";
} else {
cout << "The answer is: Reply hazy, try again\n";
}
cout << "Would you like to play again?(y/n): ";
cin >> play;
if (play == "yes" || play == "y") {
cin >> question;
} else if (play == "no" || play == "n") {
cout << "Thank you for playing with the Magic 8 Ball";
}
return 0;
}
It stops the program after it gives my answer, not letting the user answer if they want to play again or not. Please help me, I've been stuck on this for a while now and don't know what to do.
You have to add
getline(cin, question);
If you don’t,
cin >> question;
won’t be able to read more than one word.
you did most of the work you just have to make a loob like so
#include <iostream>
#include <ctime>
using namespace std;
int main() {
int answer;
string question;
string play = "";
srand(time(NULL));
while(play!="no"&&play!="n")
{
play = "";
cout << "What question would you like to ask the Magic 8 ball?: \n";
cout << "\n";
cin >> question;
answer = rand () % 8 + 1;
if (answer == 1) {
cout << "The answer is: It is certain\n";
} else if (answer == 2) {
cout << "The answer is: It is decidely so\n";
} else if (answer == 3) {
cout << "The answer is: Most likely\n";
} else if (answer == 4) {
cout << "The answer is: Signs point to yes\n";
} else if (answer == 5) {
cout << "The answer is: Ask again later\n";
} else if (answer == 6) {
cout << "The answer is: Don't count on it\n";
} else if (answer == 7) {
cout << "The answer is: My sources say no\n";
} else {
cout << "The answer is: Reply hazy, try again\n";
}
cout << "Would you like to play again?(y/n): ";
while(play != "no" && play != "n"&& play != "y" && play != "yes")
{cin >> play;
if (play == "no" || play == "n") {
cout << "Thank you for playing with the Magic 8 Ball";
} else if (play != "no" && play != "n"&& play != "y" && play != "yes") {
cout<<"you enter an unknown value, try agein"<<endl;
}
}
}
return 0;
}
i declare (string play) then i reassign it so it could not save the y/n answer from round to round cuz that would break the program and i add a loob down the code that will get you y/n answer more effective

Price does not accumulate if user repeat order

First of all, sorry if this is a dumb question.
These are my 2 functions. I'm trying to make a repeated order in function listHardware as you can see, "Do you want to purchase more? Press y for yes." at the bottom of the function. But if user repeat order, only the new price will be displayed. Why does not it accumulate with the previous price of their previous order?
I thought I made it right by doing price+= in calcFunc.
void listHardware()
{
int i,j,type,m_type,c_type,r_type,s_type,g_type,quantity=0;
const int SIZE=3;
char shopmember,addorder = 'y';
double price=0;
while(addorder=='Y' || addorder=='y')
{
cout << endl << "Select which type of hardware that you want to purchase: ";
cin >> type;
if(type==1)
{
const char *monitor[SIZE][2]=
{
{"BenQ PD3200U", "(RM3000)"},
{"Acer Predator X34", "(RM4000)"},
{"Dell UltraSharp UP3218K", "(RM8000)"}
};
cout << "Monitors:" << endl;
for(i=0;i<SIZE;i++)
{
cout << "\t" << i+1 << ". ";
for(j=0;j<2;j++)
{
cout << monitor[i][j] << " ";
}
cout << endl;
}
cout << endl << "Enter which Monitor you would like to purchase: ";
cin >> m_type; //monitor
cout << endl << "How many Monitor would you like to purchase?" << endl;
cin >> quantity;
}
if(type==2)
{
const char *cpu[SIZE][2]=
{
{"AMD Ryzen 7 2700X", "(RM1200)"},
{"Intel Core i5-8600K", "(RM1200)"},
{"Intel Core i9-7980XE", "(RM8000)"}
};
cout << "CPU:" << endl;
for(i=0;i<SIZE;i++)
{
cout << "\t" << i+1 << ". ";
for(j=0;j<2;j++)
{
cout << cpu[i][j] << " ";
}
cout << endl;
}
cout << endl << "Enter which CPU would you like to purchase: ";
cin >> c_type; //cpu
cout << endl << "How many CPU would you like to purchase?" << endl;
cin >> quantity;
}
if(type==3)
{
const char *ram[SIZE][2]=
{
{"Patriot Viper Elite 8GB DDR4-2400MHz", "(RM400)"},
{"G.Skill Ripjaws V 16GB DDR4-2400MHz", "(RM1200)"},
{"Corsair Dominator Platinum 32GB DDR4-3333MHz", "(RM2000)"}
};
cout << "RAM:" << endl;
for(i=0;i<SIZE;i++)
{
cout << "\t" << i+1 << ". ";
for(j=0;j<2;j++)
{
cout << ram[i][j] << " ";
}
cout << endl;
}
cout << endl << "Enter which RAM would you like to purchase: ";
cin >> r_type; //ram
cout << endl << "How many RAM would you like to purchase?" << endl;
cin >> quantity;
}
if(type==4)
{
const char *ssd[SIZE][2]=
{
{"Samsung 860 Pro 1TB", "(RM1250)"},
{"Crucial MX500 1TB", "(RM600)"},
{"WD Blue 2TB", "(RM1600)"}
};
cout << "SSD:" << endl;
for(i=0;i<SIZE;i++)
{
cout << "\t" << i+1 << ". ";
for(j=0;j<2;j++)
{
cout << ssd[i][j] << " ";
}
cout << endl;
}
cout << endl << "Enter which SSD would you like to purchase: ";
cin >> s_type; //ssd
cout << endl << "How many SSD would you like to purchase?" << endl;
cin >> quantity;
}
if(type==5)
{
const char *gcard[SIZE][2]=
{
{"Nvidia GeForce RTX 2080 Ti", "(RM4000)"},
{"Nvidia GeForce GTX 1080 Ti", "(RM2900)"},
{"AMD Radeon RX 580 8GB", "(RM2100)"}
};
cout << "Graphic Card:" << endl;
for(i=0;i<SIZE;i++)
{
cout << "\t" << i+1 << ". ";
for(j=0;j<2;j++)
{
cout << gcard[i][j] << " ";
}
cout << endl;
}
cout << endl << "Enter which Graphic Card would you like to purchase: ";
cin >> g_type; //gpu
cout << endl << "How many Graphic Card would you like to purchase?" << endl;
cin >> quantity;
}
cout << "Membership (Y/N): ";
cin >> shopmember;
cout << "Do you want to purchase more? Press y for yes." << endl;
cin >> addorder;
cin.ignore();
price=calcFunc(m_type,c_type,r_type,s_type,g_type,quantity,price,shopmember); //function call
}
cout << setfill ('-') << setw (55) << "-" << endl;
cout << "Total Price: " << price << endl;
cout << setfill ('-') << setw (55) << "-" << endl;
}
//
double calcFunc(int m_type,int c_type,int r_type, int s_type, int g_type,int quantity,double price, char shopmember)
{
const double discount=0.1;
if(m_type==1) //monitor
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(3000-(3000*discount))*quantity;
else
price+=3000*quantity;
}
else if(m_type==2) //monitor
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(4000-(4000*discount))*quantity;
else
price+=4000*quantity;
}
else if(m_type==3) //monitor
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(8000-(8000*discount))*quantity;
else
price+=8000*quantity;
}
if(c_type==1) //cpu
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(1200-(1200*discount))*quantity;
else
price+=1200*quantity;
}
else if(c_type==2) //cpu
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(1200-(1200*discount))*quantity;
else
price+=1200*quantity;
}
else if(c_type==3) //cpu
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(8000-(8000*discount))*quantity;
else
price+=8000*quantity;
}
if(r_type==1) //ram
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(400-(400*discount))*quantity;
else
price+=400*quantity;
}
else if(r_type==2) //ram
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(1200-(1200*discount))*quantity;
else
price+=1200*quantity;
}
else if(r_type==3) //ram
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(2000-(2000*discount))*quantity;
else
price+=2000*quantity;
}
if(s_type==1) //ssd
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(1250-(1250*discount))*quantity;
else
price+=1250*quantity;
}
else if(s_type==2) //ssd
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(600-(600*discount))*quantity;
else
price+=600*quantity;
}
else if(s_type==3) //ssd
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(1600-(1600*discount))*quantity;
else
price+=1600*quantity;
}
if(g_type==1) //gpu
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(4000-(4000*discount))*quantity;
else
price+=4000*quantity;
}
else if(g_type==2) //gpu
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(2900-(2900*discount))*quantity;
else
price+=2900*quantity;
}
else if(g_type==3) //gpu
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(2100-(2100*discount))*quantity;
else
price+=2100*quantity;
}
}
double calcFunc(int m_type,int c_type,int r_type, int s_type, int g_type,int quantity,double price, char shopmember)
Is missing a return statement.
Add
return price;
at the end of the function and you should be good.

If Statement "Your Code Will Never Be Executed"

The if else at the bottom will not work; it says that pets do not really exist.
void checkIn ()
{
int roomNum = 0;
int party = 0;
char pets;
char smoke;
cout << "----------------------------" << endl;
cout << "Welcome to Shrek's Swamp Inn" << endl << endl;
cout << "How many people are In your party? " << endl;
cin >> party;
cout << "Do you have pets? (Y/N)" << endl;
cin >> pets;
switch (pets)
{
case 'Y' : case 'y':
cout << "GTLO" << endl;
break;
case 'N' : case 'n':
cout << "cool cool" << endl;
break;
default :
cout << "User error" << endl;
}
cout << "Do you want a smoke room? (Y/N) " << endl;
cin >> smoke;
switch (smoke)
{
case 'Y' : case 'y':
cout << "GTLO" << endl;
break;
case 'N' : case 'n':
cout << "cool cool" << endl;
break;
default :
cout << "User error" << endl;
}
if((pets == 'Y' || 'y') && (smoke == 'Y' || 'y')){
roomNum = rand() % 4 + 1;
cout << "Your room is " << roomNum << "S" << endl;
}
else if((pets == 'Y' || 'y') && (smoke == 'n' || 'N')){
roomNum = rand() % 8 + 5;
cout << "Your room is " << roomNum << "P" << endl;
}
else if((pets == 'n' || 'N') && (smoke == 'n' || 'N'));{
roomNum = rand() % 15 + 9;
cout << "Your room is " << roomNum << "R" << endl;
}
}
You should change the if condition
(pets == 'Y' || 'y')
to
(pets == 'Y' || pets == 'y')
Otherwise, 'y' (as a non-zero value) will always be evaluated as true for if condition, then (pets == 'Y' || 'y') will be always true too.
And it's the same for all other if conditions.
Adding to songyuanyao's answer, you have another syntax error
else if((pets == 'n' || 'N') && (smoke == 'n' || 'N'));{
Note the semicolon ; towards the end of the statement, you need to remove this

How to make input string taken as it is instead of only the first character?

I want to create a menu where there is both letter and number.
I tried to use string but now when user input 20, it will only take the first number, which is 2. How do I make so that when user put 20, it will be seen as 20 instead of 2?
#include <iostream>
using namespace std;
int main ()
{
string choice;
cout << "1. A" << endl;
cout << "2. B" << endl;
cout << "3. C" << endl;
cout << "4. D" << endl;
cout << "Q. Quit" << endl;
do
{
cout << "Please enter your choice: ";
cin >> choice;
if (choice[0] == '1')
{
cout << "1";
} else if (choice[0] == '2')
{
cout << "2";
} else if (choice[0] == '3')
{
cout << "3";
} else if (choice[0] == '4')
{
cout << "4";
} else if (choice[0] == 'q' || choice[0] == 'Q')
{
cout << "q";
} else {
cout << "Please choose one of the menu above. " << endl;
}
} while (choice[0] != 1 && choice[0] != 2 && choice[0] != 3 && choice[0] != 4 && choice[0] != 'q');
return 0;
}
You also can see my code at http://cpp.sh/7ipv
Your code treats 20 as 2 because it sees only the first character of the input.
Try this:
#include <iostream>
using namespace std;
int main ()
{
string choice;
cout << "1. A" << endl;
cout << "2. B" << endl;
cout << "3. C" << endl;
cout << "4. D" << endl;
cout << "Q. Quit" << endl;
do
{
cout << "Please enter your choice: ";
cin >> choice;
if (choice == "1")
{
cout << "1";
} else if (choice == "2")
{
cout << "2";
} else if (choice == "3")
{
cout << "3";
} else if (choice == "4")
{
cout << "4";
} else if (choice == "q" || choice == "Q")
{
cout << "q";
} else {
cout << "Please choose one of the menu above. " << endl;
}
} while (choice != "1" && choice != "2" && choice != "3" && choice != "4" && choice != "q" && choice != "Q");
return 0;
}

Converting Integers to Strings and If.. Else Boolean Functions C++

I am working on this assignment that requires me to create a game of rock, paper scissors for my programming class. I have ran into a couple issues that I am not fully educated about as I am still learning the basics of this language. My professor wants me to take in the users choice and the computers choice and then change it from an int to a string and print it out as "You chose: Rock" instead of "You chose: 1" which is what it is doing now. This part would be in the getComputerChoice() and getPlayerChoice() functions. Another issue I am having trouble with is my professor wants us to check if it was a tie or if the player won and I am trying to put these functions in an If else statement but I am not exactly sure what the proper way to declare the function in the else statement is. (This is commented out in the else part of the if statement in main all the way at the bottom)
My code is as follows:
#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>
using namespace std;
int getComputerChoice();
int getPlayerChoice();
bool isTie(int, int);
bool isPlayerWinner(int, int);
int getComputerChoice()
{
int comp;
string cpChoice;
comp = rand() % 3 + 1;
if (comp == 1)
{
cpChoice = "Rock";
}
else if (comp == 2)
{
cpChoice = "Paper";
}
else if (comp == 3)
{
cpChoice = "Scissors";
}
return comp;
}
int getPlayerChoice()
{
int userChoice;
string strChoice;
cout << "Rock, Paper, or Scissors?\n";
cout << "1) Rock\n";
cout << "2) Paper\n";
cout << "3) Scissors\n";
cout << "Please enter your choice : \n";
cin >> userChoice;
cout << '\n';
while(userChoice < 1 || userChoice > 3)
{
cout << "Invalid Selection\n";
cout << "Re-enter a number between 1 and 3\n";
cin >> userChoice;
}
if (userChoice == 1)
{
strChoice = "Rock";
}
else if (userChoice == 2)
{
strChoice = "Paper";
}
else if (userChoice == 3)
{
strChoice = "Scissors";
}
return userChoice;
}
bool isTie(string userChoice, string comp)
{
if (userChoice != comp)
return false;
else
return true;
}
bool isPlayerWinner(int userChoice, int comp)
{
if ((comp == 1 && userChoice == 2) || (comp == 3 && userChoice == 1) || (comp == 2 && userChoice == 3))
return true;
else
return false;
}
int main()
{
char selection;
int computerChoice;
int userChoice1;
string Rock;
string Paper;
string Scissors;
srand ((unsigned int)time(NULL));
do
{
cout << '\n';
cout << "ROCK PAPER SCISSORS MENU\n";
cout << "-------------------------\n";
cout << "p) Play Game\n";
cout << "q) Quit\n";
cout << "Please enter your choice : \n";
cin >> selection;
cout << '\n';
cout << '\n';
// cin >> selection;
if (selection == 'p' || selection == 'P')
{
computerChoice = getComputerChoice();
//string computerChoice = to_string(comp);
userChoice1 = getPlayerChoice();
//string userChoice1 = to_string(userChoice);
cout << "You chose: " << userChoice1 << '\n';
cout << "The computer chose: " << computerChoice << '\n';
if (isTie(computerChoice, userChoice1)== true)
{
cout << "You choose: " << userChoice1;
cout << "The computer chose: " << computerChoice;
cout << "It's a TIE!";
}
else //(isPlayerWinner(computerChoice, userChoice1));
{
cout << "You choose: " << userChoice1;
cout << "The computer chose: " << computerChoice;
cout << "You WIN!";
}
}
//else if (selection != 'p' || selection != 'q')
//{
// cout << "Invalid Selection. Try Again.\n";
// cout << '\n';
// cin >> selection;
//}
else if (selection == 'q' || selection == 'Q')
{
cout << "You have chosen to quit the program. Thank you for using the program!\n";
}
else if (selection != 'p' || selection != 'q')
{
cout << "Invalid Selection. Try Again.\n";
cout << '\n';
}
}while (selection != 'q');
}
ANOTHER NOTE: my professor doesn't want any void functions and doesn't want any global variables.
She told me that my isTie function was fine but didn't mention anything about the isPlayerWinner function. I believe it is fine and has no issues, I am just not sure how to declare it in the main if else statement. Any help would be appreciated and if you guys have any questions or need more info please let me know. Thanks in advance.
You pretty much have everything right.
Your getPlayerChoice() and getComputerChoice() functions right now are both returning an int that stand for the players choice. You calculate the name for that choice in those functions, but dont do anything with the actual string representing the choice. You either need to return the choice string, or make a function that takes in an int and returns name associated with that choice:
string getChoiceName(int choice)
{
string strChoice;
if (choice== 1)
{
strChoice = "Rock";
}
else if (choice== 2)
{
strChoice = "Paper";
}
else if (choice== 3)
{
strChoice = "Scissors";
}
return strChoice;
}
I prefer the method, as it make it easier to calculate the result of the match if you have the ints. There are a lot of other routes you could take, like making an enum representing choices - even the function I gave you here isn't great, but it should get you to a working state.