Create a file containing the following car numbers, number of miles driven, and number of gallons of gas used by each car

I am having a trouble with this question when it comes to loop

a) Create a file containing the following car numbers, number of miles driven, and number of gallons of gas used by each car (do not include the headers):

Car No. Miles Driven Gallons Used
54 250 19
62 525 38
71 123 6
85 1322 86
97 235 14

I did manage to solve it but i had to run it 5 times to enter all the numbers and when I try to apply the loop it will just show me the last line i wrote

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 #include <iostream> 
#include <fstream>
using namespace std;
int main(){

	int carNo,miles,gallons;

	ofstream out("CarNumber.txt");
	cout<<"Enter the data: ";
	

	for(int i=1; i<5; i++)
cin>>carNo>>miles>>gallons;
	out<<carNo<<"\t\t"<<miles<<"\t\t"<<gallons<<endl;

	out.close();
	cout<<"File written ";

	return 0;
}


I am really sorry if the code was wrong because this is my first programming course and I didn't absorb the idea of some stuff :), Thanks in advance
Last edited on
You need to use brackets when you want to run multiple lines in an if-statement or loop. Your code is syntactically correct, but it doesn't run the way you want it to.

1
2
3
4
for (int i = 1; i < 5; i++)
    cin>>carNo>>miles>>gallons; //this is the only line that runs (four) times)

out<<carNo<<"\t\t"<<miles<<"\t\t"<<gallons<<endl; //this runs after the loop is completed 


What you want is more along the lines of:
1
2
3
4
5
for (int i = 1; i < 5; i++)
{
    cin>>carNo>>miles>>gallons;
    out<<carNo<<"\t\t"<<miles<<"\t\t"<<gallons<<endl;
}


When in doubt, use brackets to clarify what goes with what in your code!
Oops! totally forget about the brackets just one more question there is a part (b)
b) Write a C+ program that reads the data in the file created in part (a) and displays the car number, miles driven, gallons used, and the miles per gallon for each car. The output should also display the total miles driven, total gallons used, and average miles per gallon for all the cars. These totals should be displayed at the end of the output report as shown below.

can you show me the concept of solving the code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//open file for input
ifstream file("carNumber.txt");

//make sure file is open
if ( !file.is_open() )  { /* ERROR! */ }

//read until end-of-file (eof)
while ( !file.eof() )
{
    file >> numbers[i];
    //store gallons used in its own array the same way
    //store miles driven in its own array the same way
}

//output each car's stats

//maff to determine MPG stats and overall stats 
Last edited on
I did not study Array yet so is possible to use the basic ifstream
For average miles per gallon you just divide miles/gallon.
For the totals you can have two other variables like totalGallons and totalMiles, both initialized to 0.
Each time you read in gallons and miles you can have totalGallons+=gallons and totalMiles+=miles. (Note: ex: totalGallons+=gallons is the same as saying totalGallons = totalGallons + gallons)
Last edited on
Topic archived. No new replies allowed.