2 problems as of now: 1. when asked to input what game user wants to play, if 1
ID: 3661187 • Letter: 2
Question
2 problems as of now:
1. when asked to input what game user wants to play, if 1 or 3 is entered, it still shows as invalid.
2. in main function, I am having a hard time displaying the game the user selected to play. It always plays just pick 1. When I comment out the pick 1 section, pick 3 works just fine.
what should I do to make pick 3 show up?
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int showMenu(); //displays ALL menu choices
int takeChoice(); //validates and returns user choice selected from the menu choices from showMenu
//-------------------------------------------------------------------------------
int main()
{
static int choice;
//_____________________
int Rpick1=1+rand()%; //assigns random number for pick 1 with range 0-25
int pick1; //assigns user's pick 1
//_____________________
int Rpick3n1= 1 + rand(); //assigns random number for pick 3 first number, range 1 through 10
int Rpick3n2= 1 + rand(); //assigns random number for pick 3 second number, range 1 through 10
int Rpick3Special; //sets up the random special number
srand(time(NULL)); //seeds the random number
do
{
Rpick3Special = 1+ rand()%; //assigns random number for pick 3 special number, range 1 though 25
}
while(Rpick3Special == 7 && Rpick3Special == 13); //finds random number again if it is 7 or 13
int pick3n1; //assigns user's pick 3 regular number 1
int pick3n2; //assigns user's pick 3 regular number 2
int pick3Special; //assigns user's pick 3 special number
//_____________________
showMenu();
takeChoice();
//****************************************
while (1)
{
cout << "Enter your number for pick 1: ";
cin >> pick1;
if(Rpick1 == pick1)
{
cout<<"----------------------------- ";
cout<<" YOU WON $2! ";
cout<<"----------------------------- ";
static int winnings = 2;
return winnings;
}
else
{
cout << "Sorry, your guess was not correct." << endl;
cout << "The number was "<< Rpick1 << endl;
return 0;
}
}
//****************************************
while (3)
{
cout << " Time for pick 3! ";
cout << "Pick your first regular number: ";
cin >> pick3n1 ;
cout << "Pick the second regular number: ";
cin >> pick3n2 ;
cout << "Now pick the special number: ";
cin >> pick3Special;
cout << endl;
cout << "Here are the numbers: "<<endl;
cout << "The 1st number was "<< Rpick3n1 << endl;
cout << "The 2nd number was "<< Rpick3n2 << endl;
cout << "The special number was "<< Rpick3Special << endl;
if((Rpick3n1 == pick3n1)&&(Rpick3n2 == pick3n2)&&( Rpick3Special == pick3Special))
{
cout << "---------------------------- ";
cout << " YOU WON $100!";
cout << "---------------------------- ";
return 100;
}
else if((Rpick3n1 == pick3n1)&&( Rpick3Special == pick3Special))
{
cout << "---------------------------- ";
cout << " YOU WON $50! ";
cout << "---------------------------- ";
return 50;
}
else if((Rpick3n2 == pick3n2)&&( Rpick3Special == pick3Special))
{
cout << "---------------------------- ";
cout << " YOU WON $50! ";
cout << "---------------------------- ";
return 50;
}
else if((Rpick3n1 == pick3n1)&&(Rpick3n2 == pick3n2))
{
cout << "----------------------------- ";
cout << " YOU WON $10! ";
cout << "----------------------------- ";
return 10;
}
else if((Rpick3n1 == pick3n1)||(Rpick3n2 ==pick3n2))
{
cout << "------------------------------------------- ";
cout << " Congrats, you matched a regular number! ";
cout << " But it does not qualify as a win. ";
cout << "------------------------------------------- ";
return 0;
}
else if( Rpick3Special == pick3Special )
{
cout << "------------------------------ ";
cout << " YOU WON $5! ";
cout << "------------------------------ ";
return 5;
}
else
{
cout << " Sorry no matching numbers. ";
return 0;
}
}
return 0;
}
//-------------------------------------------------------------------------------
int showMenu()
{
static int choice;
cout << "Welcome to your daily Lucky Pick! ";
cout << "Where you can walk out with $100 daily! ";
cout << "When prompted you can decide if you want to play again. ";
cout << "--------------------------------------- ";
cout << " OFFICIAL RULES ";
cout << "--------------------------------------- ";
cout << " Pick 1: ";
cout << " Guess any number from 0 though 25, then press ENTER ";
cout << " A correct guess results in a winning of $2 ";
cout << "Pick 3: ";
cout << " Pick 1 special number from 1 through 25, but not 7 or 13 ";
cout << " Pick any 2 regular numbers from 1 through 10 ";
cout << " A correct special number guess results in a winning of $5 ";
cout << " Two correct regular number guesses result in a winning of $10 ";
cout << " A correct special number guess AND one regular number guess results in a winning of $50 ";
cout << " A correct guess of all 3 numbers results in a winning of $100 ";
cout << "To play PICK 1, enter 1, but to play PICK 3, enter 3 now: ";
cin >> choice;
return choice;
}
//-------------------------------------------------------------------------------
int takeChoice()
{
static int choice;
while ( (choice != 1) || (choice != 3))
{
cout << "Please enter a valid choice: ";
cin >> choice;
if ( (choice ==1) || (choice == 3))
{
break;
}
}
if ( choice == 1)
{
cout << " Pick 1 ";
cout << "-------- ";
return 1;
}
if ( choice == 3 )
{
cout << " Pick 3 ";
cout << "-------- ";
return 3;
}
return choice;
}
Explanation / Answer
I'll note the problem areas that you see in red with your code that I've noticed. I'll run it in a compiler and see what else I spot and add those into comment area. But I do notice that you have a lot of return statements which might mess up what you are trying to do or might not be needed.
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int showMenu(); //displays ALL menu choices
int takeChoice(); //validates and returns user choice selected from the menu choices from showMenu
//-------------------------------------------------------------------------------
int main()
{
static int choice;
//_____________________
int Rpick1=1+rand()%; //assigns random number for pick 1 with range 0-25
int pick1; //assigns user's pick 1
//_____________________
int Rpick3n1= 1 + rand(); //assigns random number for pick 3 first number, range 1 through 10
int Rpick3n2= 1 + rand(); //assigns random number for pick 3 second number, range 1 through 10
int Rpick3Special; //sets up the random special number
srand(time(NULL)); //seeds the random number
do
{
Rpick3Special = 1+ rand()%; //assigns random number for pick 3 special number, range 1 though 25
}
while(Rpick3Special == 7 && Rpick3Special == 13); //finds random number again if it is 7 or 13
int pick3n1; //assigns user's pick 3 regular number 1
int pick3n2; //assigns user's pick 3 regular number 2
int pick3Special; //assigns user's pick 3 special number
//_____________________
showMenu();
takeChoice();
//****************************************
Your while loop conditions will always be true unless its a 0. So that is why your while(1) always shows, and when you commented it out it'll always show while(3). What you might want to do instead is use
if(choice == 1)
do the following
if(choice==3)
do the following
while (1)
{
cout << "Enter your number for pick 1: ";
cin >> pick1;
if(Rpick1 == pick1)
{
cout<<"----------------------------- ";
cout<<" YOU WON $2! ";
cout<<"----------------------------- ";
static int winnings = 2;
return winnings;
}
else
{
cout << "Sorry, your guess was not correct." << endl;
cout << "The number was "<< Rpick1 << endl;
return 0;
^^^^^^^ This will end the program since its a return in main, my suggestion is to make use that winnings variable to total up all scores. Have winnings = 0 at start. and at every condition where gain an amount you can just add it onto the winnings instead of returning a number.
For example:
player wins pick 1
winnings += 2; //this is equivalent to saying winning = winning + 2
then player wins all of pick 3
winnings += 100; //winning will be updated again and another 100 is added on
do this with all your return statements or else your main will end from those returns, for return 0 just delete them since they do not affect your score.
}
}
//****************************************
while (3)
{
cout << " Time for pick 3! ";
cout << "Pick your first regular number: ";
cin >> pick3n1 ;
cout << "Pick the second regular number: ";
cin >> pick3n2 ;
cout << "Now pick the special number: ";
cin >> pick3Special;
cout << endl;
cout << "Here are the numbers: "<<endl;
cout << "The 1st number was "<< Rpick3n1 << endl;
cout << "The 2nd number was "<< Rpick3n2 << endl;
cout << "The special number was "<< Rpick3Special << endl;
if((Rpick3n1 == pick3n1)&&(Rpick3n2 == pick3n2)&&( Rpick3Special == pick3Special))
{
cout << "---------------------------- ";
cout << " YOU WON $100!";
cout << "---------------------------- ";
return 100;
}
else if((Rpick3n1 == pick3n1)&&( Rpick3Special == pick3Special))
{
cout << "---------------------------- ";
cout << " YOU WON $50! ";
cout << "---------------------------- ";
return 50;
}
else if((Rpick3n2 == pick3n2)&&( Rpick3Special == pick3Special))
{
cout << "---------------------------- ";
cout << " YOU WON $50! ";
cout << "---------------------------- ";
return 50;
}
else if((Rpick3n1 == pick3n1)&&(Rpick3n2 == pick3n2))
{
cout << "----------------------------- ";
cout << " YOU WON $10! ";
cout << "----------------------------- ";
return 10;
}
else if((Rpick3n1 == pick3n1)||(Rpick3n2 ==pick3n2))
{
cout << "------------------------------------------- ";
cout << " Congrats, you matched a regular number! ";
cout << " But it does not qualify as a win. ";
cout << "------------------------------------------- ";
return 0;
}
else if( Rpick3Special == pick3Special )
{
cout << "------------------------------ ";
cout << " YOU WON $5! ";
cout << "------------------------------ ";
return 5;
}
else
{
cout << " Sorry no matching numbers. ";
return 0;
}
}
return 0;
}
//-------------------------------------------------------------------------------
int showMenu()
{
static int choice;
cout << "Welcome to your daily Lucky Pick! ";
cout << "Where you can walk out with $100 daily! ";
cout << "When prompted you can decide if you want to play again. ";
cout << "--------------------------------------- ";
cout << " OFFICIAL RULES ";
cout << "--------------------------------------- ";
cout << " Pick 1: ";
cout << " Guess any number from 0 though 25, then press ENTER ";
cout << " A correct guess results in a winning of $2 ";
cout << "Pick 3: ";
cout << " Pick 1 special number from 1 through 25, but not 7 or 13 ";
cout << " Pick any 2 regular numbers from 1 through 10 ";
cout << " A correct special number guess results in a winning of $5 ";
cout << " Two correct regular number guesses result in a winning of $10 ";
cout << " A correct special number guess AND one regular number guess results in a winning of $50 ";
cout << " A correct guess of all 3 numbers results in a winning of $100 ";
cout << "To play PICK 1, enter 1, but to play PICK 3, enter 3 now: ";
cin >> choice;
return choice;
}
//-------------------------------------------------------------------------------
int takeChoice()
{
static int choice;
while ( (choice != 1) || (choice != 3))
{
cout << "Please enter a valid choice: ";
cin >> choice;
if ( (choice ==1) || (choice == 3))
{
break;
}
}
if ( choice == 1)
{
cout << " Pick 1 ";
cout << "-------- ";
return 1;
}
if ( choice == 3 )
{
cout << " Pick 3 ";
cout << "-------- ";
return 3;
}
return choice;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.