Question

In: Computer Science

We had to make a "multipath adventure" for our C++ class. I chose to do one...

We had to make a "multipath adventure" for our C++ class. I chose to do one on the basic steps to go through if your car doesn't start. (I did it before I found out we would have to code it. Big mistake.) Here's the code to mine:

#include
#include
using namespace std;


int main() {
string userChoice;

cout << "You wake up one morning to go to school, and you're car won't start. Do you: " << endl;
cout << "a) Check for electrical problems, " << endl;
cout << "b) Check for fuel problems, " << endl;
cout << "c) Check for ignition problems, " << endl;
cout << " or d) Check for air flow problems? " << endl;


cin >> userChoice;
if (userChoice == "a") {
cout << "Electrical: Do you start by trying to turn the engine over? Type \"a\" for yes and \"b\" for no." << endl;
  
cin >> userChoice;
if (userChoice == "a") {
cout << "The engine does not turn over at all. There is only a clicking noise. Type \"a\" to use the multimeter" << endl;

cin >> userChoice;
if (userChoice == "a") {
cout << "Using a multimeter, you check the battery charge. It's dead. Do you \"a\" replace the battery or \"b\" try a jump start?" << endl;

cin >> userChoice;
if (userChoice == "a") {
cout << "You replace the battery. The car runs fine on the way home, but when you come back to start it again, it's dead. Type \"a\" to test the alternator" << endl;

cin >> userChoice;
if (userChoice == "a") {
cout << "You test the alternator. You see that it is most likely bad, or there is parasitic draw. Do you \"a\" replace the alternator, or \"b\" test for parasitic draw?" << endl;
  
cin >> userChoice;
if (userChoice == "a") {
cout << "You replace the alternator. Good for you! Your car starts up every time now. The end." << endl;
}
  
  
}
else if (userChoice == "b") {
cout << "You test the wiring, but there is no draw. At least you know that your wires are in good condition! Type \"a\" to replace the alternator." << endl;

cin >> userChoice;
if (userChoice == "a") {
cout << "You replace the alternator. Good for you! Your car starts up every time now. The end." << endl;
}
}

}
}
else if (userChoice == "b") {
cout << "You jump start the car, but as it runs, you see the voltage dropping again. Game Over. Try again!" << endl;
}
}
  
}
else if (userChoice == "b") {
cout << "You immediatley check the battery to see if it's dead. It seems to be that way. Maybe you should've tried starting the engine just to be sure. Game over! Try again." << endl;
}



else if (userChoice == "b") {
cout << "You turn the key and hear the fuel pump whine. That means the issue most likely isnt the fuel pump." << endl;
cout << "To check the injectors, type \"a\". To check the fuel lines, type \"b\". " << endl;

cin >> userChoice;
if (userChoice == "a") {
cout << "You check the injectors using a screwdriver. You hear them clicking, meaning they are probably functioning properly. The engine is getting fuel. This isnt the problem. Try again!" << endl;
}
  
else if (userChoice == "b") {
cout << "Well, you just added another 30 minutes disassembling and reassembling your fuel. No problems here. If you had just check the injectors you would've known that...." << endl;
cout << "Game Over! Try again!" << endl;
}
}


else if (userChoice == "c") {
cout << "You pull each spark plug one by one. Each of them are sparking. None of them are rusted. No issues here. Game over! Try Again!" << endl;
  
}



else if (userChoice == "d") {
cout << "Do you start with the intake manifold? Type \"a\" for yes and \"b\" for no." << endl;

cin >> userChoice;
if (userChoice == "a") {
cout << "You spend another 30 minutes pulling the intake manifold apart. Nothing blocking here. Try again!" << endl;
}
else if (userChoice == "b") {
cout << "You instead check the air filter. It's still looking clean. The problem isnt air flow! Try again!" << endl;
}
  
}



else {
cout << "Invalid Entry" << endl;
}

return 0;
}

Now its telling us to add other things into it:

"Modify your Multipath Adventure from the previous lab to include loops. The criteria by which you will be graded are listed under "Deductions Graded by TAs". We suggest that you fulfill the requirements by doing the following:

  • 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)
  • Use at least 1 for loop
    • Consider using for loop(s) to draw box or stars around prompt in your input function (see above)
  • Use at least 1 instance of rand()

(If your code already fulfilled these, then you are done!)

If you need to, you can adjust the decision tree as you are programming. Resubmit the final decision tree with the lab submission quiz.

Note that you are allowed to use concepts we have not covered in class if you would like to. This might also help you in preparing for future labs.

As part of grading your lab, the TAs will randomly pick 2 paths through your decision tree to check your work. They will reference your submitted decision tree to choose the paths and make sure that when they run your code it takes them through the path as specified in your decision tree.

To make your game easier to play (and thus make it easier for the TAs to grade) you should provide prompts before each input that make clear what inputs are valid.

  • You must adhere to the style guidelines. (up to 10 points)
  • Your code must meet all of the Special Requirements from the previous Multipath Adventure Lab
    • It is fine if you re-organize your code and have different paths or functions than you did in Lab 4, but make sure that you still meet the requirements as listed in Lab 4.
    • If you re-organize your code, make sure to also update your Decision Tree
    • Make sure to review the deductions listed in the previous lab and any feedback you received to avoid losing points here.
  • You must have at least 1 for loop in your code (15 points)
    • 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 (15 points)
    • 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 (5 points)
    • 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)"

Im so lost...

I made the decision tree before I had to code it. We can cut down on the number of decisions and branches if needed, just as long as it fulfills the above requierments.

Solutions

Expert Solution

While loop:

In the following program while loop is used to run the switch statement.

In the switch statement all the four cases run independently.

There is no better way than switch to make menu for a game.

This while loop takes string as an input. This while loop run until user type "no".

For loop:

For loop is used to print '******" in the output window.

Rand function:

Random function is used to predict how many times for loop will run to print "*****************".

This random function will produce number from 1 to 10.

Program:

include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace std;
int main ()
{
string yes="yes";

while(yes=="yes"){
  
int ch;
string userChoice;
int random;
srand(time(0));
for(int i=0;i<10;i++)
random=(rand() % 10) + 1;
for(int i=0; i<random; i++)
{
cout<<"*************************************************************************"<<endl;
}
cout << "You wake up one morning to go to school, and you're car won't start. Do you: "<< endl;
cout << "1) Check for electrical problems, " << endl;
cout << "2) Check for fuel problems, " << endl;
cout << "3) Check for ignition problems, " << endl;
cout << "4) Check for air flow problems? " << endl;

for(int i=0; i<2; i++)
{
cout<<"********************************"<<endl;
}
cout << " Enter your choice";
cin >> ch;
switch(ch)
{
case 1 :
  
cout <<"Electrical: Do you start by trying to turn the engine over? Type \"a\" for yes and \"b\" for no."<< endl;
cin >> userChoice;
if (userChoice == "a")
    {
   cout <<"The engine does not turn over at all. There is only a clicking noise. Type \"a\" to use the multimeter"<< endl;
   cin >> userChoice;
   if (userChoice == "a")
   {
   cout <<"Using a multimeter, you check the battery charge. It's dead. Do you \"a\" replace the battery or \"b\" try a jump start?"<< endl;
   cin >> userChoice;
   if (userChoice == "a")
      {
      cout <<"You replace the battery. The car runs fine on the way home, but when you come back to start it again, it's dead. Type \"a\" to test the alternator"<< endl;
       cin >> userChoice;
       if (userChoice == "a")
       {
       cout <<"You test the alternator. You see that it is most likely bad, or there is parasitic draw. Do you \"a\" replace the alternator, or \"b\" test for parasitic draw?"<< endl;
       cin >> userChoice;
       if (userChoice == "a")
           {
           cout <<"You replace the alternator. Good for you! Your car starts up every time now. The end."<< endl;
           }


       }
       else if (userChoice == "b")
       {
       cout <<"You test the wiring, but there is no draw. At least you know that your wires are in good condition! Type \"a\" to replace the alternator."<< endl;
       cin >> userChoice;
       if (userChoice == "a")
           {
           cout <<"You replace the alternator. Good for you! Your car starts up every time now. The end."<< endl;
           }
       }

       }
   }
   else if (userChoice == "b")
{
  
   cout <<"You jump start the car, but as it runs, you see the voltage dropping again. Game Over. Try again!"<< endl;
  
   }

  
else if (userChoice == "b")
{
cout <<"You immediatley check the battery to see if it's dead. It seems to be that way. Maybe you should've tried starting the engine just to be sure. Game over! Try again."<< endl;
}


break;
case 2 :
  
cout <<"You turn the key and hear the fuel pump whine. That means the issue most likely isnt the fuel pump."<< endl;
cout <<"To check the injectors, type \"a\". To check the fuel lines, type \"b\". "<< endl;
cin >> userChoice;
  
if (userChoice == "a")
    {
   cout << "You check the injectors using a screwdriver. You hear them clicking, meaning they are probably functioning properly. The engine is getting fuel. This isnt the problem. Try again!"<< endl;
break;
    }

else if (userChoice == "b")
    {
   cout << "Well, you just added another 30 minutes disassembling and reassembling your fuel. No problems here. If you had just check the injectors you would've known that...."<< endl;
   cout << "Game Over! Try again!" << endl;
   break;
    }
  
break;
case 3 :

cout <<"You pull each spark plug one by one. Each of them are sparking. None of them are rusted. No issues here. Game over! Try Again!"<< endl;

break;


case 4 :
cout <<"Do you start with the intake manifold? Type \"a\" for yes and \"b\" for no."<< endl;
cin >> userChoice;
  
if(userChoice == "a")
    {
   cout <<"You spend another 30 minutes pulling the intake manifold apart. Nothing blocking here. Try again!"<< endl;
break;
    }
if(userChoice == "b")
   {
   cout <<"You instead check the air filter. It's still looking clean. The problem isnt air flow! Try again!"<< endl;
break;
   }

break;


default:
cout << "Invalid Entry" << endl;
break;
}
}
cout<<"Do you want to play the game again ?yes/no:";
cin>>yes;
}

return 0;
}

Output:

Note:

There is no other way to reduce if else ladder. This task is done by only with if else ladder.

Switch statement must be used to run all the four cases as per the choice of user.

Decision tree of path:

See in the image basic stucture of the program.


Related Solutions

Why do we need security personnel, and do they contribute to our welfare? Could we make...
Why do we need security personnel, and do they contribute to our welfare? Could we make everyone at least as well-off if we would all cooperate so that we could reallocate some protective labor to more productive activity? Could this be an example of a prisoner’s dilemma? Diagram the game and explain where the US economy rests.
*this is for sociology class and i chose psychology because there were no option for sociology...
*this is for sociology class and i chose psychology because there were no option for sociology under "select a subject" List a plan of action on how you would change the american justice system for criminals. what steps would need to be taken? IF you get your solution from an online source please provide the link below the answer. IF you get your solution from an online source please provide the link below the answer. IF you get your solution...
As a class, we added several different reagents to our Winogradsky columns. We also made one...
As a class, we added several different reagents to our Winogradsky columns. We also made one control-column that had nothing added to it (aside from sediment and water). Everyone added paper towels (but at varying depths), and sulfate to the bottom of their columns. The rest of the treatments are listed below. Please indicate what types of organisms you might enrich with that type of treatment and why, or how and why that treatment might result in a different type...
In this class we make a distinction between common variation and specific variation. Why do we...
In this class we make a distinction between common variation and specific variation. Why do we make this distinction
when we performed the TLC experiment, the class chose a mixture for the mobile phase of...
when we performed the TLC experiment, the class chose a mixture for the mobile phase of 80 % ethyl acetate and 20 % hexanes. why was this composition chosen
the four things that are recommended that we can do to make our planet/environment healthier for...
the four things that are recommended that we can do to make our planet/environment healthier for all land and sea life.
Make one program in c++ Implement the Point class composed of an ordered pair (?, ?)....
Make one program in c++ Implement the Point class composed of an ordered pair (?, ?). Redefine the operators in your Point class Calculate the coefficients of a Linear Regression ? = ?? + ? with next data for x and y. x y 46 4.7 72 12.3 59 6.5 56 5.9 54 6.6 70 10.3 68 8.4 48 4
I have to do a simple linear regression project. The dependent variable that I chose is...
I have to do a simple linear regression project. The dependent variable that I chose is international tourist receipts (US$ billions) and the independent variable I chose is International tourist arrivals in (billions). Did I set up my variables correctly? I am using data from the World Bank. The main trouble I am having is why testing the correlation between these two variables is important and need some ideas on answering the few questions in the introduction of the essay,...
In C++ One of the things we rely on computers to do for us is to...
In C++ One of the things we rely on computers to do for us is to store information. We keep files, movies and music (all legally obtained of course...), and a host of data to help us keep organized. This week your challenge (the last one!) is to make a program that keeps a collection of lists. You'll manage a grocery, hardware store, and chore list all in the same program. You'll use files to store those lists so that...
In C++ In this lab we will be creating a stack class and a queue class,...
In C++ In this lab we will be creating a stack class and a queue class, both with a hybrid method combining linked list and arrays in addition to the Stack methods(push, pop, peek, isEmpty, size, print) and Queue methods (enqueue, deque, peek, isEmpty, size, print). DO NOT USE ANY LIBRARY, implement each method from scratch. Both the Stack and Queue classes should be generic classes. Don't forget to comment your code.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT