116k views
0 votes
Description:

Create a program that converts the number of miles that you walked on a hike to the number of feet that you walked.
Console:
Hike Calculator
How many miles did you walk?: 4.5
You walked 23760 feet.
Continue? (y/n): y
How many miles did you walk?: 2.5
You walked 13200 feet.
Continue? (y/n): n
Bye!
Specifications:
The program should accept a float value for the number of miles.
Store the code that gets user input and displays output in the main function.
There are 5280 feet in a mile.
Store the code that converts miles to feet in a separate function. This function should return an int value for the number of feet.
Assume that the user will enter a valid number of miles.

User CiNN
by
4.8k points

1 Answer

1 vote

Answer:

The programming language is not stated (I'll answer using C++)

#include <iostream>

using namespace std;

int convert(float miles)

{

return miles * 5280;

}

int main() {

cout<<"Console:"<<endl;

cout<<"Hike Calculator"<<endl;

float miles;

char response;

cout<<"How many miles did you walk?. ";

cin>>miles;

cout<<"You walked "<<convert(miles)<<" feet"<<endl;

cout<<"Continue? (y/n): ";

cin>>response;

while(response == 'y')

{

cout<<"How many miles did you walk?. ";

cin>>miles;

cout<<"You walked "<<convert(miles)<<" feet"<<endl;

cout<<"Continue? (y/n): ";

cin>>response;

}

cout<<"Bye!";

return 0;

}

Step-by-step explanation:

Here, I'll explain some difficult lines (one after the other)

The italicized represents the function that returns the number of feet

int convert(float miles)

{

return miles * 5280;

}

The main method starts here

int main() {

The next two lines gives an info about the program

cout<<"Console:"<<endl;

cout<<"Hike Calculator"<<endl;

float miles;

char response;

This line prompts user for number of miles

cout<<"How many miles did you walk?. ";

cin>>miles;

This line calls the function that converts miles to feet and prints the feet equivalent of miles

cout<<"You walked "<<convert(miles)<<" feet"<<endl;

This line prompts user for another conversion

cout<<"Continue? (y/n): ";

cin>>response;

This is an iteration that repeats its execution as long as user continue input y as response

while(response == 'y')

{

cout<<"How many miles did you walk?. ";

cin>>miles;

cout<<"You walked "<<convert(miles)<<" feet"<<endl;

cout<<"Continue? (y/n): ";

cin>>response;

}

cout<<"Bye!";

User WrightsCS
by
4.3k points