Question

In: Computer Science

Write in C++ Example Enter number of miles travelled 12340 Enter number of hours in trip...

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


Solutions

Expert Solution

#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


Related Solutions

C++ write a program that asks the user to enter the hours and rate then calculate...
C++ write a program that asks the user to enter the hours and rate then calculate the gross pay for an employee, the program should test if the hours are regular (40), any hour more than 40 should be paid with the overtime rate: 1.5*rate. The program should ask repeatedly the user if he/she wants to continue: y or n, if the user types y, then the program should ask for the hours and rate for another employee then display...
IN C++ Write a program that prompts the user to enter the number of students and...
IN C++ Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score (display the student’s name and score). Also calculate the average score and indicate by how much the highest score differs from the average. Use a while loop. Sample Output Please enter the number of students: 4 Enter the student name: Ben Simmons Enter the score: 70 Enter the student name:...
Write a C++ program that keeps asking user to enter a positive integer number until a...
Write a C++ program that keeps asking user to enter a positive integer number until a sentinel value (999) is entered. Then for each positive number entered by the user, find out how many digits it consists. (Hint: divide a number by 10 will remove one digit from the number. You can count how many divisions it needs to bring the number to 0.) An example of executing such a program is shown below. Note that the user input is...
In C#, write a console-based program that asks a user to enter a number between 1...
In C#, write a console-based program that asks a user to enter a number between 1 and 10. Check if the number is between the range and if not ask the user to enter again or quit. Store the values entered in an array. Write two methods. Method1 called displayByVal should have a parameter defined where you pass the value of an array element into the method and display it. Method2 called displayByRef should have a parameter defined where you...
Use this data for Questions 20-24 The distance travelled (in hundreds of miles) and sales (in...
Use this data for Questions 20-24 The distance travelled (in hundreds of miles) and sales (in thousands of dollars) for ACME Company are reflected in table below: Miles Travelled Sales 5 6 4 2 8 7 1 3 14 6 19 11 What is the Coefficient of Determination? 0.7334 0.4053 0.2267 0.1437 What is the Test Statistic for the Correlation Coefficient? 3.32 16.15 1.88 Cannot be computed What is the regression slope? 1.98 0.40 0.71 0.32 What is the regression...
1/Write a C program that asks the user to enter 5 nums and adds each number...
1/Write a C program that asks the user to enter 5 nums and adds each number to a total. The program should then display the total. To add to the total, the program must call the function void sum(int value, int *ptotal) passing the value the user entered and the address of the total (a local variable in main). The function should add the value to the total. Note that the function is void and does not return anything. Don't...
C++ Write the C++ code for a void function that prompts the user to enter a...
C++ Write the C++ code for a void function that prompts the user to enter a name, and then stores the user's response in the string variable whose address is passed to the function. Name the function getName.
IN C This assignment is to write a program that will prompt the user to enter...
IN C This assignment is to write a program that will prompt the user to enter a character, e.g., a percent sign (%), and then the number of percent signs (%) they want on a line. Your program should first read a character from the keyboard, excluding whitespaces; and then print a message indicating that the number must be in the range 1 to 79 (including both ends) if the user enters a number outside of that range. Your program...
Write a program that runs on SPIM that allows the user to enter the number of...
Write a program that runs on SPIM that allows the user to enter the number of hours, minutes and seconds and then prints out the total time in seconds. Name the source code file “seconds.asm
Write a program that runs on SPIM that allows the user to enter the number of...
Write a program that runs on SPIM that allows the user to enter the number of hours, minutes and seconds and then prints out the total time in seconds. Name the source code file “seconds.asm Explain step by step
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT