In: Computer Science
The purpose of the program is to play the game of M.A.S.H. (Mansion, Apartment, Shack, House). This program asks the user multiple questions and then randomly generates answers based on the answers to predict the user’s future. By the time you finish this program, you will have learned how to make a menu based program, use switch statements, validate user input with loops, allow a program to run multiple times until user wants to quit, and mix cin>> and getline() intermittently in a program.
You will be predicting the user’s future by selecting one of their three choices randomly. You will need to create a random number for each “category” – this means there should be in total 7 numbers randomly generated between 1 and 3. So for example, you will generate a random number between 1 and 3 for the names of people.
You will also generate one more random number (this would make 8 total) between 1 and 4 which will indicate if the user will live in a mansion (1), apartment (2), shack (3), or house(4).
code given below:
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include<string>
#include<conio.h>
using namespace std;
void enter()
{
char name1[100],name2[100],name3[100];
string city1,city2,city3,job1,job2,job3,comp1,comp2,comp3,car1,car2,car3;
int c1,c2,c3,c4,c5,c6;
int r1,r2,r3,r4,r5,r6,r7,mash;
cin.get();
cout << "\nEnter Names of three people (2 you like & one you don’t like)\n";
cin.getline (name1, 100);
cin.getline (name2, 100);
cin.getline (name3, 100);
r1 = 1 + (rand() % 3);
cout << "\nEnter Three integer numbers between 1 and 100\n"; cin>>c1;cin>>c2;cin>>c3;
r2 = 1 + (rand() % 3);
cin.get();
cout << "\nEnter Three locations including city & state (2 you like & one you don’t like)\n"; getline(cin,city1);getline(cin,city2);getline(cin,city3);
r3 = 1 + (rand() % 3);
cout << "\nThree job titles (2 you like & one you don’t like)\n"; getline(cin,job1);getline(cin,job2);getline(cin,job3);
r4 = 1 + (rand() % 3);
cout << "\nThree companies or restaurants (2 you like & one you don’t like)\n"; getline(cin,comp1);getline(cin,comp2);getline(cin,comp3);
cout << "\nEnter Three integer numbers between 10000 and 500000\n"; cin>>c4;cin>>c5;cin>>c6; cin.get();
cout << "\nEnter Three types of cars (2 you like & one you don’t like)\n"; getline(cin,car1);getline(cin,car2);getline(cin,car3);
r5 = 1 + (rand() % 3);
mash = 1 + (rand() % 4);
if (r1==1)
cout<<"\nYou will be happily married to … "<<name1;
else if(r1==2)
cout<<"\nYou will be happily married to … "<<name2;
else
cout<<"\n\nYou will be married to … "<<name3;
if (r2==1)
cout<<"\nYou and your spouse will have"<<c1<<"children";
else if (r2==2)
cout<<"\nYou and your spouse will have "<<c2<<"children";
else
cout<<"\n\nYou and your spouse will have"<<c3<<"children";
if (r3==1)
cout<<"\nYou will live in ….. "<<city1;
else if(r3==2)
cout<<"\nYou will live in ….. "<<city2;
else
cout<<"\n\nYou will live in ….. "<<city3;
if (r4==1)
cout<<"\nYou will work at "<<city1<<"as a "<<job1<<" making "<<c4<<"a year.";
else if(r4==2)
cout<<"\nYou will work at "<<city2<<"as a "<<job2<<" making "<<c5<<"a year.";
else
cout<<"\n\nYou will work at "<<city3<<"as a "<<job3<<" making "<<c6<<"a year.";
if (r5==1)
cout<<"\nYou will drive a …… "<<car1;
else if(r5==2)
cout<<"\nYou will drive a …… "<<car2;
else
cout<<"\n\nYou will drive a …… "<<car3;
if (mash==1)
cout<<"\nYou will live in…… "<<"Mansion";
else if(mash==2)
cout<<"\nYou will live in…… "<<"Apartment";
else if(mash==3)
cout<<"\nYou will live in…… "<<"Shack";
else
cout<<"\n\nYou will live in…… "<<"Home";
}
int main()
{
int n;
cout<<"press 1. Play Mash\n";
cout<<"press 2. End proggram\n";
cin>>n;
switch (n)
{
case 1:
enter();
break;
case 2:
exit(0);
break;
default:
cout << "Choice other than 1, 2 ";
break;
}
return 0;
}
output: