In: Computer Science
This is my coded "multipath adventure" that I created. Now I have to modify it to include loops but I am really struggling to do so. Below are the instructions and my current code that needs to be modified. Thank you in advance!
#include<iostream>
#include<string>
using namespace std;
string getChoice1()
{
string choice1;
cout << "You wake up on the weekend and you don't know
what to do. Do you..." << endl;
cout << "a. Be responsible and get some homework done"
<< endl;
cout << "b. Start watching some TV" << endl;
cin >> choice1;
return choice1;
}
string getChoice4()
{
string choice4;
cout << "Good job! Now you must choose which homework to
do! Do you want..." << endl;
cout << "a. Computer Science" << endl;
cout << "b. Work on another subject" << endl;
cin >> choice4;
return choice4;
}
void getChoice5(string choice4, string &choice5)
{
if (choice4 == "b")
{
cout << "That's alright. But what other subject do you
want to do?" << endl;
cout << "a) Research design and analysis" <<
endl;
cout << "b) IT&C" << endl;
cin >> choice5;
}
}
void getChoice2(string &choice2)
{
cout << "That's okay! What should you watch?"; cout
<< "a) Reality TV" << endl;
cout << "b) a movie" << endl;
cin >> choice2;
}
void getChoice3(string &choice3)
{
cout << "Cool. Now you must choose which show to watch. Do
you want to watch..." << endl;
cout << "a) The Real Housewives of Salt Lake City" <<
endl;
cout << "b) Criminal Minds" << endl;
cin >> choice3;
}
int main ()
{
string choice1,choice2,choice3,choice4,choice5;
choice1 = getChoice1();
if (choice1 == "a")
{
choice4 = getChoice4();
}
if (choice4 == "a")
{
cout << "Perfect. You sit down and begin your main lab 4 for CS 142. THE END" << endl;
return 0;
}
getChoice5(choice4,choice5);
if (choice5 == "a")
{
cout << "Sounds good. You sit down and begin module 3. THE END." << endl;
return 0;
}
else if(choice5=="b")
{
cout << "Ok. You begin your quiz for IT&C. THE END." << endl;
return 0;
}
if (choice1 == "b")
{
getChoice2(choice2);
}
if (choice2 == "b")
{
cout << "Great choice! You decide to stay in bed and watch a movie. THE END" << endl;
return 0;
}
else
{
getChoice3(choice3);
}
if (choice3 == "b")
{
cout << "Great! You end up watching Criminal Minds! THE END" << endl;
}
else
{
cout << "You end up trying to watch RHSL but find out it has not come out yet so you end up going with option b anyways. THE END" << endl;
}
}
#include<iostream>
#include<string>
#include <bits/stdc++.h> //this header is used for the rand()
function......
using namespace std;
const int MAX = 2; // MAX is a global variable used while
implementing rand()
char arr[MAX] = {'a','b'};
string getChoice1()
{
string choice1;
cout << "You wake up on the weekend and you don't know
what to do. Do you..." << endl;
cout << "a. Be responsible and get some homework done"
<< endl;
cout << "b. Start watching some TV" << endl;
choice1=arr[rand() % MAX]; // the function in RHS generates a
random character either 'a' or 'b' and gets stored in the LHS
variable.
cout<<"Random choice: " + choice1 <<endl;
return choice1;
}
string getChoice4()
{
string choice4;
cout << "Good job! Now you must choose which homework to
do! Do you want..." << endl;
cout << "a. Computer Science" << endl;
cout << "b. Work on another subject" << endl;
choice4=arr[rand() % MAX];
cout<<"Random choice: " + choice4 <<endl;
return choice4;
}
void getChoice5(string choice4, string &choice5)
{
if (choice4 == "b")
{
cout << "That's alright. But what other subject do you
want to do?" << endl;
cout << "a) Research design and analysis" <<
endl;
cout << "b) IT&C" << endl;
choice5=arr[rand() % MAX];
cout<<"Random choice: " + choice5 <<endl;
}
}
void getChoice2(string &choice2)
{
cout << "That's okay! What should you watch?" <<
endl;
cout << "a) Reality TV" << endl;
cout << "b) a movie" << endl;
choice2=arr[rand() % MAX];
cout<<"Random choice: " + choice2 <<endl;
}
void getChoice3(string &choice3)
{
cout << "Cool. Now you must choose which show to watch. Do
you want to watch..." << endl;
cout << "a) The Real Housewives of Salt Lake City" <<
endl;
cout << "b) Criminal Minds" << endl;
choice3=arr[rand() % MAX];
cout<<"Random choice: " + choice3 <<endl;
}
int main ()
{
string again;
string choice1,choice2,choice3,choice4,choice5;
while(1) //while loop is used to play the game as many times you
can.press 'y' to play again, else 'n'
{
choice1 = getChoice1();
if (choice1 == "a")
{
choice4 = getChoice4();
}
if (choice4 == "a")
{
cout << "Perfect. You sit down and begin your main lab 4
for CS 142. THE END" << endl;
goto play_again;
}
getChoice5(choice4,choice5);
if (choice5 == "a")
{
cout << "Sounds good. You sit down and begin module 3. THE
END." << endl;
goto play_again;
}
else if(choice5=="b")
{
cout << "Ok. You begin your quiz for IT&C. THE END."
<< endl;
goto play_again;
}
if (choice1 == "b")
{
getChoice2(choice2);
}
if (choice2 == "b")
{
cout << "Great choice! You decide to stay in bed and watch
a movie. THE END" << endl;
goto play_again;
}
else
{
getChoice3(choice3);
}
if (choice3 == "b")
{
cout << "Great! You end up watching Criminal Minds! THE
END" << endl;
goto play_again;
}
else
{
cout << "You end up trying to watch RHSL but find out it
has not come out yet so you end up going with option b anyways. THE
END" << endl;
goto play_again;
}
play_again:
cout<<"Want to play again? y or n"<<endl;
cin>> again;
if (again == "n")
break;
}
return 0;
}
----------------------------------------------------------------------------------------------------------------------------------------------
Here I am attaching a screenshot of result for your reference.
All the choices were chosen randomly. You just need to make a choice after each iteration i.e whether you want to play again or not.
changes made to your code:
-> use of return(0) statement in every conditional block terminated the program there itself.Hence, while loop failed to work.
-> use of rand() function to make random choices.
-> use of goto statements according to requirement.
This code makes the game completely automatic. If you want to make a specific part as automatic then make necessary changes. If you need any help, Please make me know in the comment box.
Thank you.