Need help with this C++ program. Write a program that will input miles traveled
ID: 3725344 • Letter: N
Question
Need help with this C++ program.
Write a program that will input miles traveled and hours spent in
travel. The program will determine miles per hour. This calculation must
be done in a function other than main; however, main will print the
calculation. The function will thus have 3 parameters: miles, hours, and
milesPerHour. Which parameter(s) are pass by value and which are
passed by reference? Output is fixed with 2 decimal point precision.
Sample Run:
Please input the miles traveled
475
Please input the hours traveled
8
Your speed is 59.38 miles per hour
Here is what I have so far:
//CPP program for calculate miles per hour
//Autumn Montgomery
#include<iostream>
using namespace std;
void calculation(double, double, double);
int main()
{
double miles;
double hours;
double mph;
cout << "Please input the miles traveled";
cin >> miles;
cout << "Please input the hours traveled";
cin >> hours;
calculation (miles, hours, mph);
system("pause");
}
void calculation(double m, double h, double mh)
{
double mph = (m / h);
cout << "You speed is " << mph << "miles per hour";
}
I keep getting an error that the uninitialized local variable 'mph' is being used.
What am I doing wrong?
Explanation / Answer
Hi.. I have written cpp for the above.
Main.cpp
#include<iostream>
#include <iomanip>
using namespace std;
void calculation(double, double, double*);
int main()
{
double miles;
double hours;
double mph=0;
cout << "Please input the miles traveled:";
cin >> miles;
cout << "Please input the hours traveled:";
cin >> hours;
calculation (miles, hours, &mph);
cout << "You speed is " << setprecision(4) << mph << " miles per hour";
//system("pause");
return 0;
}
void calculation(double m, double h, double* mh)
{
*mh = (m / h);
}
Output:
Please input the miles traveled:475
Please input the hours traveled:8
You speed is 59.38 miles per hour
Please test the code and let me know any issues. Thank you. All the best.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.