Question

In: Computer Science

This is my coded "multipath adventure" that I created. Now I have to modify it to...

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!

  • You must have at least 1 for loop in your code
    • The for loop must actually be used as a loop, and it must have real effect in your program (it is theoretically possible to use the for loop syntax without actually using it as a loop or creating any real effect, but if you do this you will lose points)
  • You must have at least 1 while loop in your code
    • The note on for loops above applies equally to this deduction regarding while loops
  • You must have at least 1 instance of rand() in your code
    • Your use of rand() must have real effect in your program (it is possible to use rand() to generate a random number without actually using the random number, but if you do this you will lose points
  • The above are REQUIREMENTS and below are just some SUGGESTIONS
    • Use a while loop to enable the player to play your game multiple times
    • Create an input function that utilizes a loop to prompt for and validate user input
      • Takes a prompt (string) as the argument
      • Returns the valid input (string)
    • For the requirement to use at least 1 for loop:
      • Consider using for loop(s) to draw box or stars around prompt in your input function (see above)
    • For the requirement to use at least 1 instance of rand():
      • Consider including a randomized process in your game, such as rolling a die, and using rand() to simulate the process
      • Consider including a point where the game randomly selects between multiple possible descriptions. For example, you could have the game randomly decide whether to describe the weather as "sunny" or "rainy".

#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;

}

}

Solutions

Expert Solution

#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.


Related Solutions

I have created a method in java in my menu list and I have to put...
I have created a method in java in my menu list and I have to put all the work into the method but I cannot get it to print and am not sure where I am going wrong. this is my code: public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); String fName = ""; String lName = ""; double hoursWorked; double hourlyRate; int optionOne = 1; int optionTwo = 2; int optionThree...
Needs to be coded in Python. Hello i am working on a project for my computer...
Needs to be coded in Python. Hello i am working on a project for my computer programming course and i am having trouble with one part. The code needs to be able to produce two versions of an output given inputs by the user. for instance. Here is the code i have to produce the following output. The input from the user are num1 num2 and asc which is just asking if they want the output to be ascending or...
hi, i finished paying off my mortgage of my house last year in FULL now i...
hi, i finished paying off my mortgage of my house last year in FULL now i decided i want to sell it. i had the mortage in 2001 till now 2020 of $3,000 monthly for about $600,000 how much can i sell it for? where do i go? can i sell it in my desire ($1,000,000)? is there any paperwork i must do now? do i get paid cash if i sell it? do i need a broker help or...
Could you modify my code so it meets the following requirement? (Python Flask) I want the...
Could you modify my code so it meets the following requirement? (Python Flask) I want the user to register for account using email and password, then store that data into a text file. Then I want the data to be read when logging in allowing the user to go to home page. -------------Code-------------------- routes.py from flask import Flask, render_template, redirect, url_for, request, session import json, re app = Flask(__name__) '''@app.before_request def before_request(): if 'visited' not in session: return render_template("login.html") else:...
I have a high definition antennae mounted on my house in my backyard. As I don’t...
I have a high definition antennae mounted on my house in my backyard. As I don’t exactly have a high ladder, it is mounted quite low, and we don’t receive many channels. The current height is 12 feet. My friend has offered to let me borrow a 15 foot high ladder. It is recommended to mount the lad- der at a 76 degree angle to the ground to prevent it from sliding out. I will be mounting the antennae 4...
I am doing a paper on stressors and my stressor is my job. I have had...
I am doing a paper on stressors and my stressor is my job. I have had an arguement with a superior who is know for being a bully, but i stood up to him and could end up unemployed. Having four kids, a wife, and bills to pay, this could be devastating. Using this stressor, what physiological changes occur in the brain due to the stress response? And what emotional and cognitive effects might occur due to this stressful situation?
This is the code I have. My problem is my output includes ", 0" at the...
This is the code I have. My problem is my output includes ", 0" at the end and I want to exclude that. // File: main.cpp /*---------- BEGIN - DO NOT EDIT CODE ----------*/ #include <iostream> #include <fstream> #include <sstream> #include <iomanip> using namespace std; using index_t = int; using num_count_t = int; using isConnected_t = bool; using sum_t = int; const int MAX_SIZE = 100; // Global variable to be used to count the recursive calls. int recursiveCount =...
I have a term paper that I have to write for my microeconomics class. Economics is...
I have a term paper that I have to write for my microeconomics class. Economics is super new to me, this is the first class I've ever taken related to it. My subject is the economic impacts of the opioid epidemic. My outline is due next week and I have no idea what to even do research on or which direction to go. Where do I even start?
I have a BigInt class I created that works correctly. I would like to add a...
I have a BigInt class I created that works correctly. I would like to add a constructor BigInt(string s, int base) that creates a BigInt operating in the given base. The newly extended class should work with any base between 2 and 36, using the digits 0, 1, …, 9, A, B, C, …, Z. How do I do this from what I have? (you may add new data members and such of course) #include <string> #include <vector> class BigInt...
Here is what I have so far. I have created a code where a user can...
Here is what I have so far. I have created a code where a user can enter in their information and when they click submit all of the information is shown. How can I add a required field for the phone number without using an alert? <!Doctype html> <html> <head> <meta charset="UTF-8"> <title>Login and Registeration Form Design</title> <link rel="stylesheet" type="text/css" href="signin.css"> <script> function myFunction(){ document.getElementById('demo').innerHTML = document.getElementById('fname').value + " " + document.getElementById('lname').value + " " + document.getElementById('street').value + " "...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT