In: Computer Science
Example of how it needs to be done, one at a time instead of asking all 3 at once
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
int main(){
int rnum = ((rand()%3-1)+1)+1;
string car1,car2,car3;
cout << "Enter a vehicle you like:";
getline(cin, car1);
cout << "Enter a vehicle you like:";
getline(cin, car2);
cout << "Enter a vehicle you dislike:";
getline(cin,car3);
switch(rnum){
case 1:
cout<< "You will drive "<< car1 << endl;
break;
case 2:
cout<< "You will drive "<< car2 << endl;
break;
default:
cout<< "You will drive "<< car3 << endl;
}
}
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.
INPUT
1 Names of three people (2 they like & one they
don’t like)
1 Three integer numbers between 1 and 100
2 Three locations including city & state (2 they
like & one they don’t like)
3 Three job titles (2 they like & one they don’t
like)
4 Three companies or restaurants (2 they like & one
they don’t like)
5 Three integer numbers between 10000 and 500000
6 Three types of cars (2 they like & one they don’t
like
RANDOM NUMBERS
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).
OUTPUT
1 The user’s type of house (mansion, apartment, shack,
or house)
2 The user’s spouse (based on the three people)
3 The number of children the user will have (based on
the integer between 1 and 100)
4 Where the user will live (based on locations)
5 Where the user will work, their job title, and their
salary (based on the three companies and the three integer numbers
between 10000 and 500000).
6 What the user will drive. (based on cars)
SPECIFICATIONS
• Indent and comment your code properly.
• MENU - You will have a main menu that will ask the user to either
1) Play MASH or
2) End the program.
You must have a switch statement to figure out which choice the user selected. The program should run over and over until the user selects to end the program using a do-while loop. Use a Boolean variable to help with this!
• You MUST validate user input with while loops if the input is a number to ensure the number is in the specified range. You may assume the user will enter in a number (not a character or string), but you can’t assume they enter a number within the specified range.
• You MUST allow spaces to be included in all string
input.
HOW TO PRINT OUT RESULTS
• For housing you are not asking the user for the data.
The acronym MASH stands for the different types of housing. There
are four choices (Mansion, Apartment, Shack or House). You will
need to generate a number between 1 and 4. If it is a 1, you will
print out that the user will live in a mansion. If it is a 2, you
will print out that the user will live in an apartment……and so
on.
• For all the other “categories” you have three choices, not four. So you will need to generate a number between 1 and 3. If it is a 1, you print out the first one, 2 the second one, and 3 the third one. For example, for spouse – generate a number between 1 and 3. If it is a 1, then print out the first person that the user said they like. If it is a 2, then print out the second person that the user said they like. If it is a 3, then print out the third person that the user dislikes. Before printing out the spouse, you should say “You will be happily married to “ and then print out the name.
• For the answers you should say something
like:
o You will live in ….
o You will be happily married to …
o You and your spouse will have ….. children.
o You will live in ….. (name city, state here)
o You will work at ……. (place) as a ……… (job title)
making $ ……… (salary) a year.
o You will drive a ……
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
int random(int){
srand((unsigned) time(0));
int rnum = (rand()%3+1);
return rnum;
}
int main(){
int f=0,choice;
do{
cout<<"1)Play MASH or 2) End Program"<<endl;
cin>>choice;
switch (choice) {
case 1: {
string per1,per2,per3;
cout<<"Enter the name of a person you like:";
getline(cin,per1);
cout<<"Enter the name of a person you like:";
getline(cin,per2);
cout<<"Enter the name of a person you dislike:";
getline(cin,per3);
int a,b,c;
cout<<"Enter a number between 1-100:";
cin>>a;
if(a<100&&a>1){
a=a;
}
else{
a=0;
cout<<"Enter a valid number"<<endl;};
cout<<"Enter a number between 1-100:";
cin>>b;
if(b<100&&b>1){
b=b;
}
else{
b=0;
cout<<"Enter a valid number"<<endl;};
cout<<"Enter a number between 1-100:";
cin>>c;
if(c<100&&c>1){
c=c;
}
else{
c=0;
cout<<"Enter a valid number"<<endl;};
string loc1,loc2,loc3;
cout<<"Enter the name of a city or state you like:";
cin>>loc1;
cout<<"Enter the name of a city or state you like:";
cin>>loc3;
cout<<"Enter the name of a city or state you dislike:";
cin>>loc2;
string job1,job2,job3;
cout<<"Enter a Job title you like :";
getline(cin,job1);
cout<<"Enter a Job title you like :";
getline(cin,job2);
cout<<"Enter a Job title you like :";
getline(cin,job3);
string com1,com2,com3;
cout << "Enter a company or restaurant you like:";
getline(cin, com1);
cout << "Enter a company or restaurants you like:";
getline(cin, com2);
cout << "Enter a company or restaurant you dislike:";
getline(cin,com3);
int n,l,m;
cout<<"Enter a number between 10000 and 500000";
cin>>n;
if(n<100&&n>1){
n=n;
}
else{
n=0;
cout<<"Enter a valid number"<<endl;};
cout<<"Enter a number between 10000 and 500000";
cin>>l;
if(l<100&&l>1){
l=l;
}
else{
l=0;
cout<<"Enter a valid number"<<endl;};
cout<<"Enter a number between 10000 and 500000";
cin>>m;
if(m<100&&m>1){
m=m;
}
else{
m=0;
cout<<"Enter a valid number"<<endl;};
string car1,car2,car3;
cout << "Enter a vehicle you like:";
getline(cin, car1);
cout << "Enter a vehicle you like:";
getline(cin, car2);
cout << "Enter a vehicle you dislike:";
getline(cin,car3);
//1
switch(random(0)){
case 1:
cout<< "You will live in a Mansion"<< endl;
break;
case 2:
cout<< "You will live in a Apartment"<< endl;
break;
case 3:
cout<<"You will live in a Shack"<<endl;
break;
default:
cout<< "You will live in a House"<< endl;
}
//2
switch(random(1)){
case 1:
cout<< "You will be happily married to "<< per1 << endl;
break;
case 2:
cout<< "You will be happily married to "<< per2 << endl;
break;
default:
cout<< "You will be happily married to "<< per3 << endl;
}
//3
switch(random(2)){
case 1:
cout<< "You and your spouse will have "<< a<<" children."<<endl;
break;
case 2:
cout<< "You and your spouse will have "<< b<<" children."<< endl;
break;
default:
cout<< "You and your spouse will have "<< c<<" children."<< endl;
}
//4
switch(random(3)){
case 1:
cout<< "You will live in "<< loc1 << endl;
break;
case 2:
cout<< "You will live in "<< loc2 << endl;
break;
default:
cout<< "You will live in "<< loc3 << endl;
}
//5
switch(random(5)){
case 1:
cout<< " You will work at "<< com1 ;
break;
case 2:
cout<< "Y You will work at "<< com2 ;
break;
default:
cout<< " You will work at "<< com3;
}
//6
switch(random(4)){
case 1:
cout<< "as a "<< job1;
break;
case 2:
cout<< "as a "<< job2 ;
break;
default:
cout<< "as a "<< job3;
}
//7
switch(random(6)){
case 1:
cout<< "making $ "<< n << endl;
break;
case 2:
cout<< "making $ "<< l << endl;
break;
default:
cout<< "making $ "<< m << endl;
}
//8
switch(random(7)){
case 1:
cout<< "You will drive "<< car1 << endl;
break;
case 2:
cout<< "You will drive "<< car2 << endl;
break;
default:
cout<< "You will drive "<< car3 << endl;
}
break;
}
case 2: {
f++;
cout<<"Thanks for Playing";
break;
}
default:
cout<<"Wrong Input"<<endl;
}
}
while(f==0);
}