Help with game program please
I had a class on C++ a few years ago and I am trying to brush up the little skills I have of this language. I am creating a program that lets you choose from 4 different games. When I choose a game to play it automatically goes to the first one no matter what game I choose. It also skips one of the programs and doesn't let me choose another after I chose the first game it just goes right to another. Can someone please help me?
#include <iostream>
#include <cstdlib>
using namespace std;
int choc;
int birth;
int sum;
int y;
int born;
int year_now, age_now, another_year, another_age;
int month, day, year, total;
int tries;
int guess;
int secret;
char again = 'y';
int games = 0;
int wins = 0;
int month_number;
int choice;
int main ()
{
cout << "Choose the game you wish to play:";
cout << endl;
cout << "1.Year " << "2.Chocolate " << "3.day born " << "4.guessing game ";
cout << endl;
cout << "Please enter your choice: "<< endl;
cout << endl;
int choice=0;
cin >> choice;
while (choice<1 && choice>=4)
if (choice == 1);
cout << "Enter the current year then press RETURN.\n";
cin >> year_now;
cout << "Enter your current age in years.\n";
cin >> age_now;
cout << "Enter the year for which you wish to know your age.\n";
cin >> another_year;
another_age = another_year - (year_now - age_now);
if (another_age >= 0) {
cout << "Your age in " << another_year << ": ";
cout << another_age << "\n";
}
else {
cout << "You weren't even born in ";
cout << another_year << "!\n";
}
cout << endl;
if (choice == 2);
cout << "Enter the year in which you were born. " << endl;
cin >> born;
cout << endl;
cout << "Pick the number of times you would like to have chocolate a week (more than one but less than ten)" << endl;
cin >> choc;
cout << endl;
sum = ((choc * 2) + 5) * 50;
cout << "Have you had your birthday this year? (y/n)" << endl;
cin >> birth;
cout << endl;
system("pause");
if (birth=='y'){
sum = sum + 1754;
}
else {
sum = sum + 1755;
}
sum = sum - born;
sum = sum +1;
cout << "The first digit is how many times you want chocolate, the last digits are your age." << endl;
cout << sum << endl;
if (choice == 3);
cout << "Enter your birth date, I will tell you\n";
cout << "the day of the week on which you were born.\n";
cout << "\nBirth date (mm dd yy): ";
cin >> month >> day >> year;
total = 0;
// divide the last two digits of the birth year by 4. Put in total.
total = total + ( year / 4 );
// add the last two digits of the birth year to total
total = total + year;
// add the last two digits of the birth date to total
total = total + day;
// find the month number
switch(month)
{
case 1:
case 10: month_number = 1;
break;
case 2:
case 3:
case 11: month_number = 4;
break;
case 4:
case 7: month_number = 0;
break;
case 5: month_number = 2;
break;
case 6: month_number = 5;
break;
case 8: month_number = 3;
break;
case 9:
case 12: month_number = 6;
break;
}
// ...and add it to total
total = total + month_number;
// if leap year and month is Jan or Feb, subtract 1 from total
if ( year % 4 == 0 && ( month == 1 || month == 2 ) )
total--;
// find the remainder when total is divided by 7
int remainder = total % 7;
// look up the remainder in the following table:
cout << "You were born on a ";
switch(remainder)
{
case 0: cout << "Saturday.\n";
break;
case 1: cout << "Sunday.\n";
break;
case 2: cout << "Monday.\n";
break;
case 3: cout << "Tuesday.\n";
break;
case 4: cout << "Wednesday.\n";
break;
case 5: cout << "Thursday.\n";
break;
case 6: cout << "Friday.\n";
break;
}
if (choice==4);
rand();
while ( again == 'y' )
{
games = games + 1;
cout << "I have picked a number between one and a hundred. ";
cout << "Try to guess it." << endl;
cout << "You lose if it takes you more than seven tries." << endl;
cout << endl;
secret = rand()% 100;
tries = 0;
while ( guess != secret )
{
tries = tries + 1;
cout << "Guess #" << tries << ": ";
cin >> guess;
if ( guess < secret )
cout << "Sorry, that's too low." << endl;
if ( guess > secret )
cout << "Sorry, that's too high." << endl;
} // while loop for each guess
cout << endl;
cout << "That's correct! You got it!" << endl;
if ( tries > 7 )
{
cout << "But it took you " << tries;
cout << " tries, so you lose." << endl;
}
else
{
cout << "And it only took you " << tries;
cout << " tries, so you win!" << endl;
wins = wins + 1;
}
cout << endl;
cout << "Play again? (y/n) ";
cin >> again;
// while loop for each game
}
cout << endl;
cout << "You played a total of " << games << " games." << endl;
cout << "You only guessed my number in seven tries or less ";
cout << wins << " times." << endl;
cout << "You are a foolish mortal." << endl;
return 0;
}
# 3 Re: Help with game program please
ok its not working great. I click on 1 and it quits, 2 works fine until I get to yes or no. then it repeats forever. 3 works but wants to repeat itself over and over again and 4 works fine unless you hit no then it repeats a bunch of garbage over and over. I think my braces { } are in the wrong place somewhere can anyone figure this out?
#include <iostream>
#include <cstdlib>
using namespace std;
// declare variables
int choc;
int birth;
int sum;
int y;
int born;
int year_now, age_now, another_year, another_age;
int month, day, year, total;
int tries;
int guess;
int secret;
char again = 'y';
int games = 0;
int wins = 0;
int month_number;
int choice;
//main program
int main ()
{
//menu choices
cout << "Choose the game you wish to play:";
cout << endl;
cout << "1.Year " << "2.Chocolate " << "3.day born " << "4.guessing game ";
cout << endl;
cout << "Please enter your choice: "<< endl;
cout << endl;
int choice=0;
cin >> choice;
while (choice<1 && choice>4)
// game 1 - Calculate year
while (choice == 1)
{
cout << "Enter the current year then press RETURN.\n";
cin >> year_now;
cout << "Enter your current age in years.\n";
cin >> age_now;
cout << "Enter the year for which you wish to know your age.\n";
cin >> another_year;
another_age = another_year - (year_now - age_now);
if (another_age >= 0) {
cout << "Your age in " << another_year << ": ";
cout << another_age << "\n";
}
else {
cout << "You weren't even born in ";
cout << another_year << "!\n";
}
cout << endl;
}
// game 2 - Calculate age
while(choice == 2){
cout << "Enter the year in which you were born. " << endl;
cin >> born;
cout << endl;
cout << "Pick the number of times you would like to have chocolate a week (more than one but less than ten)" << endl;
cin >> choc;
cout << endl;
sum = ((choc * 2) + 5) * 50;
cout << "Have you had your birthday this year? (y/n)" << endl;
cin >> birth;
cout << endl;
if (birth=='y'){
sum = sum + 1754;
}
else {
sum = sum + 1755;
}
sum = sum - born;
sum = sum +1;
cout << "The first digit is how many times you want chocolate, the last digits are your age." << endl;
cout << sum << endl;
}
// game 3 - Calculate day you were born
while (choice == 3)
{
cout << "Enter your birth date, I will tell you\n";
cout << "the day of the week on which you were born.\n";
cout << "\nBirth date (mm dd yy): ";
cin >> month >> day >> year;
total = 0;
total = total + ( year / 4 );
total = total + year;
total = total + day;
switch(month)
{
case 1:
case 10: month_number = 1;
break;
case 2:
case 3:
case 11: month_number = 4;
break;
case 4:
case 7: month_number = 0;
break;
case 5: month_number = 2;
break;
case 6: month_number = 5;
break;
case 8: month_number = 3;
break;
case 9:
case 12: month_number = 6;
break;
}
total = total + month_number;
if ( year % 4 == 0 && ( month == 1 || month == 2 ) )
total--;
int remainder = total % 7;
cout << "You were born on a ";
switch(remainder)
{
case 0: cout << "Saturday.\n";
break;
case 1: cout << "Sunday.\n";
break;
case 2: cout << "Monday.\n";
break;
case 3: cout << "Tuesday.\n";
break;
case 4: cout << "Wednesday.\n";
break;
case 5: cout << "Thursday.\n";
break;
case 6: cout << "Friday.\n";
break;
}
}
// game 4 - Guessing Game
while (choice==4)
{
rand();
while ( again == 'y' )
{
games = games + 1;
cout << "I have picked a number between one and a hundred. ";
cout << "Try to guess it." << endl;
cout << "You lose if it takes you more than seven tries." << endl;
cout << endl;
secret = rand()% 100;
tries = 0;
while ( guess != secret )
{
tries = tries + 1;
cout << "Guess #" << tries << ": ";
cin >> guess;
if ( guess < secret )
cout << "Sorry, that's too low." << endl;
if ( guess > secret )
cout << "Sorry, that's too high." << endl;
} // while loop for each guess
cout << endl;
cout << "That's correct! You got it!" << endl;
cout << "And it only took you " << tries;
cout << " tries, so you win!" << endl;
wins = wins + 1;
}
cout << endl;
cout << "Play again? (y/n) ";
cin >> again;
}
return 0;
}