home study engineering computer science questions and an

home / study / engineering / computer science / questions and answers / (c++) a robot travels across a 30 foot room. every ... Your question has been answered! Rate it below. Let us know if you got a helpful answer. Question: (C++) A robot travels across a 30 foot room. Every... Save (C++) A robot travels across a 30 foot room. Every 12 inches it travels it indicates that it has traveled \'x feet\'. The user inputs how far across the room it want the robot to go. Upon arrival at the specified distance, it announces...\"I made it!\"(This will happen even if the robot moves 0 feet). The program should also indicate how many more feet the robot could travel before running into a wall. i.e. for an input of 5, the resulting output should be: How far would you like the robot to travel? 5 feet 1 feet 2 feet 3 feet 4 feet 5 feet I made it! I can only travel 25 more feet. #include using namespace std; int main() { int roomsize = 30; int distance = 0; cout << \"How far would you like the robot to travel? \"; cout << distance << \" feet\" << endl; /* Type your code here. */ return 0; }

Solution

#include <iostream>
using namespace std;
int main() {
    int roomsize = 30; //declaring room size
    int distance = 0; // declaring to be travelled
    cout << \"How far would you like the robot to travel? \";
    cin >> distance; //input distance to be travelled.
    if(distance>roomsize){ //if distance to go is greater than room size, throw an error.
        cout<<\"I cannot go that far. Please enter a number less than 30.\";
        return 0;
    }
    cout << distance << \" feet\" << endl;
    /* Type your code here. */
    int travelled = 0; //initialising travelled distance
    while(distance>travelled){ //checking if travelled is less than distance to be travelled
        travelled += 1;
        cout<<travelled<<\" feet\ \"; //outputing the distance that has been travelled.
    }
    cout<<\"I made it!\ \";
    cout<<\"I can only travel \"<<roomsize-distance<<\" more feet.\ \";
    return 0;
}

home / study / engineering / computer science / questions and answers / (c++) a robot travels across a 30 foot room. every ... Your question has been answered!

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site