I need some help with a program concerning files.
a)I have to create a file with the following information:
1. Honda 4000
2.Toyota 3000
3.Mercedes 8000
4. Toyota 3500
5. Honda 2900
create the necessary data structure to represent the above data in a program. create the necessary variables and populate it by reading the data from the file.
b) Produce the necessary code to write a second file that contains the aggregate data for the car list. These consists of the car brand, the name of these cars in the list and the total value they have
Honda 2 6900
Toyota 2 6500
Mercedes 1 8000
This is what I did but it doesn't work...
I compile the program it says it has no errors but it doesn't run
Anyone knows how to help me?
# include <iostream.h>
# include <fstream.h>
#include <stdlib.h>
struct cars{
int carID;
char name[15];
int price;
};
cars car[5];
int main(){
int I=0;
ifstream fin;
ofstream fout;
fout.open ("Cars.txt");
if (!fout){
cout<<"Error opening the file";
exit(1);
}
for (I=1; I<=5; I++){
fout<<car[i].carID;
fout<<car[i].name[15];
fout<<car[i].price;
}
fin.open("Cars.txt");
if (!fin){
cout<<"Error opening the file";
exit(1);
}
int count=0;
int price=0;
for (I=1; I<=5; I++){
fin>>car[i].name[15];
fin>>car[i].price;
if (car[i].name=="Honda")
count+=1;
price+=car[i].price;
if (car[i].name=="Toyota")
count+=1;
price+=car[i].price;
if (car[i].name=="Mercedes")
count+=1;
price+=car[i].price;
}
fin>>car[i].name[15];
fin>>count;
fin>>price;
fin.close();
fout.close();
return 0;
}