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;
}

The code you posted does not compile because it does contain errors. I can better help if you copy and paste what you really have.

C++ is case-sensitive, so I and i are different.

You're using pre-standard (non-standard) C++ ( # include <iostream.h> ).

for (I=1; I<=5; I++){

Arrays are zero-base, so this is the usual idiom.

for ( i = 0; i < 5; ++i )

C-style strings (null terminated char arrays) are not compared like this:

if ( car[i].name=="Honda" )

Instead you use strcmp from <string.h> .

In general, all input functions should be checked for success before you simply bull forward.

You are attempting to read and write to the same file.

Instead you use strcmp from <string.h>.

Or rather, as you pointed out, <cstring>. :)

Well, I'm not sure about everything you need to do in the program but there are a few things I noticed.

As the other reply says, your loop for the array should go from 0-4, not 1-5.

After all of your if statements you have two lines of code. If you want both of those lines to be part of the if, you need to put them in a block of code.

{
  line 1; 
  line 2;
}

Otherwise, the second line will always get executed, and only the first command after the IF statement will be conditional.

Also, from what I got, you need to keep a separate count for each type of car. The way you have it coded right now is with only one count, for the total number of ALL the cars.

Another thing, you started out by opening an output file. Becuase you declared the array of car structs in the same program, the values inside them are ...unknown. When you write the output to a file, you don't know what it is going to say. I'm just assuming here that you might want to open a file and read it in before you try to dump contents to a file.

Or rather, as you pointed out, <cstring>. :)

D'oh!

Well at least <string.h> has mention in the C++ standard while <iostream.h> does not. Mumble... grumble... oops. [Darn lack of font size tags.]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.