In: Computer Science
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:
(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.
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.
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.