Debugglng Informatlon for Lose. Did not reach destinatlon withln 4 steps DEBUG INFO Your instructor has chosen to give you the following information to help you debug your code and improve your submission. COMPILER STACK TRACE None PROGRAM EXECUTION STACK TRACE None INPUT OF THE TEST CASE 4 1 6. YOUR CODE'S OUTPUT 1 You are 14 mile(s) from campus! 2 How do you wish to travel? (1 bus, 2 subway, 3 jetpack) You are 12 mile(s) from campus! 3 How do you wish to travel? (1 bus, 2 subway, 3 jetpack) You are 1e mile(s) from campus! 4 How do you wish to travel? (1 bus, 2 subway, 3 jetpack) You are 5 mile(s) from campus! 5 How do you wish to travel? (1 bus, 2 subway, 3 jetpack) You haven't reached your target!You lose! THE CORRECT OUTPUT OF THE TEST CASE 1 You are 14 mile(s) from campus! 2 How do you wish to travel? (1 bus, 2 subway, 3 jetpack) You are 12 mile(s) from campus! 3 How do you wish to travel? (1 bus, 2 subway, 3 jetpack) You are 10 mile(s) from campus! 4 How do you wish to travel? (1 bus, 2 subway, 3 jetpack) You are 5 mile(s) from campus! 5 How do you wish to travel? (1 bus, 2 subway, 3 jetpack) You are 3 mile(s) from campus! 6 You haven't reached your target! 7 You lose! "UNIX DIFF OF CORRECT OUTPUT AND YOUR OUTPUT 5c5,7 < How do you wish to travel? (1 bus, 2 subway, 3 jetpack) You haven't reached your target!You lose! | No newline at end of file > How do you wish to travel? (1 bus, 2 subway, 3 jetpack) You are 3 mile(s) from campus! > You haven't reached your target! > You lose! PRETTY DIFF This diff is colored to make it clear what parts of the output are wrong. Green indicates things in the correct output that you are missing. red indicates things in your output that shouldn't be there. The - character refers to newlines, so the green character refers a newline you are missing in your output and the red refers to a newline you need to remove from your output. 1 You are 14 mile(s) from campus!e 2 HOw do you wish to travel? (1 bus, 2 subway, 3 jetpack) You are 12 mile(s) from campus!e 3 How do you wish to travel? (1 bus, 2 subway, 3 jetpack) You are 1e mile(s) from campus!e How do you wish to travel? (1 bus, 2 subway, 3 jetpack) You are 5 mile(s) from campus!e How do you wish to travel? (1 bus, 2 subway, 3 jetpack) You are 3 mile(s) from campus!e 6 You haven't reached your target! You lose! 3 Campus Travel Game This lab will practice using loops. We will construct a short computer game. Please look at the Test Cases for exact wording. For your game, the player's goal is to reach campus exactiy. The player starts 14 miles away and has up to 4 turns to reach campus. At each tum the play can ride either use a Bus, a Subway, or a Jetpack: Ricling a Bus moves the player forward 2 miles each turn. Riding a Subway moves the player forward 5 miles each tum • Riding a Jetpack moves the player forward 10 mile each turn. Example: You are 14 mile(s) from campus! How do you wish to travel? (1 bus, 2 subway, 3 jetpack) The player chooses one. After each turn, the player is informed how much farther she must travel before reaching campus. Winning/Losing: After the last tum, if the player has reached campus exactly ("You have won!") Otherwise, explain the problem: "You have over-shot your target!" or "You haven't reached your target!" And then write "You lose!" The game will operate as follows: Report how far the user is from campus – the player starts 14 mile away • For each turn: o Ask user to select transport method (Bus, Subway, or Jetpack) o Report the user's new distance from campus olf the player has reached campus or passed campus and it is not the fourth turn, end the game early – This Is a more challenging step! Make sure the rest of your game works before working on this step. ALSO: Check that the user input is valid (1-3). If the user fails to picka valid number, the program must keep asking the user for a new selection until a valid number is entered. ALSO- you do not lose turns by making an invalid selection. Use this wording: Invalid choice, try again! Requirements: • You must use a loop (while, do-while, or for) to loop through the 4 turns. (The loop can include a condition to allow ending early, if you so choose.) You must use at least one if or switch statement Require the user enters one of three numbers to specify the transport method
Addition of Two Numbers
Adding two numbers in programming is essentially the same as adding two numbers in general arithmetic. A significant difference is that in programming, you need to pay attention to the data type of the variable that will hold the sum of two numbers.
C++
C++ is a general-purpose hybrid language, which supports both OOPs and procedural language designed and developed by Bjarne Stroustrup. It began in 1979 as “C with Classes” at Bell Labs and first appeared in the year 1985 as C++. It is the superset of C programming language, because it uses most of the C code syntax. Due to its hybrid functionality, it used to develop embedded systems, operating systems, web browser, GUI and video games.
I get an error when I run this c++ code. Below I have attached the code, prompt and the error I receive.
Code:
#include <iostream>
using namespace std;
int main()
{
cout <<"You are 14 mile(s) from campus!" << endl;
int turns = 1;
int distance = 14;
while(turns <=4){
// ask for the choice
cout << "How do you wish to travel? (1 bus, 2 subway, 3 jetpack) " ;
int choice;
cin >> choice;
// if the choice is invalid ask again
if(choice!= 1 && choice!=2 && choice!=3)
{
cout << "Invalid choice, try again!\n";
continue;
}
else
{
// decrease the required distance based in the choice the user makes
switch (choice)
{
case 1:
distance -= 2;
break;
case 2:
distance -= 5;
break;
case 3:
distance -= 10;
break;
}
if(turns == 4)
{
if(distance != 0)
{
if(distance < 0)
{
cout<<distance <<endl;
cout << "You have over-shot your target!";
//waits for user input to go to next line
cin.get();
//empties the buffer so that character can wait for new input,
//i.e., user can press enter again to end the program
cin.ignore();
cout <<"You lose!";
cin.get();
break;
}
else
{
cout << "You haven't reached your target!";
cin.get();
cin.ignore();
cout <<"You lose!";
cin.get();
break;
}
}
else
{
cout << "You are " << distance <<" mile(s) from campus!";
cin.get();
cin.ignore();
cout << "You have won!";
cin.get();
break;
}
}
else
{
if(distance > 0)
{
cout << "You are " << distance <<" mile(s) from campus!"<<endl;
}
else if(distance < 0)
{
cout << "You have over-shot your target!";
cin.get();
cin.ignore();
cout <<"You lose!";
cin.get();
break;
}
else
{
cout << "You are " << distance <<" mile(s) from campus!";
cin.get();
cin.ignore();
cout << "You have won!";
cin.get();
break;
}
}
turns++;
}
}
return 0;
}
It seems I am missing the part that says "You are 3 mile(s) from campus!↵" and You lose should be all the way at the bottom instead. How can I fix it?
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images