In: Computer Science
Write in C++
Example
Enter number of miles travelled
12340
Enter number of hours in trip
460
file.txt
Miles: 12340
Hours: 460
MPH: 26.83
Question:
Write a program that does the following things:
1 ) Ask the user for number of miles travelled (should be in getData function and number of hours in trip (should be in getData function)
[Note: Use reference parameters to have access to these values in main]
2 ) Calculate the miles per hour(MPH) for the trip (should be in main)
3 ) write the miles, hours and MPH to a text file (should be in writeFile function)
[Note: Use reference parameter for writer]
Here are the functions you need
Function name | Parameters | Return Type
getData | miles, hours | void
writeFile | writer, miles, hours, MPH | void
#include<iostream>
#include <fstream>
using namespace std;
void getData(int &miles,int &hours ){
//reading the data from the user for hours and miles
cout<<"Enter number of miles travelled\n";
cin>>miles;
cout<<"Enter number of hours in trip\n";
cin>>hours;
}
void writeFile(ofstream &myfile,int miles,int hours,double mph){
//writing the miles and hours and MPH to file
myfile<<"Miles: "<<miles<< endl;
myfile<<"Hours: "<<hours<< endl;
myfile<<"MPH: "<<mph<< endl;
}
int main(){
int miles,hours;
//getting the user data
getData(miles,hours);
//calculating the mph
double mph = miles/(double)(hours);
//opening the file
ofstream myfile("file.txt");
//writing to the file
writeFile(myfile,miles,hours,mph);
myfile.close();
}
NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.
I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME