Hi there,
I made a program which contains car info... the car make and the mpg. Each car make/mpg is put into a structure and written into a file called cars.dat.
From there, I open the file cars.dat and go through to see if the mpg is > 30 or < 30. If it's >30 it gets put into the file Over30.dat, otherwise it gets put into Under30.dat. The program works well so far.
Here is my problem:
I'm having trouble figuring out how to print the contents from all 3 files to the screen. Essentially I want to print cars.dat, Over30.dat, and Under30.dat to the screen console so the program actually looks like it's doing something instead of doing everything "behind the scenes"
I'm having trouble towards the bottom.Code:#include <iostream> #include <fstream> #include <string> using namespace std; // put the data into a structure struct Car { string make; int mpg; }; int main() { Car c1, c2, c3, c4, c5, c6, c7; // define each car c1.make = "Prius"; c1.mpg = 45; c2.make = "Civic"; c2.mpg = 35; c3.make = "Suburban"; c3.mpg = 12; c4.make = "BMW"; c4.mpg = 18; c5.make = "Mercedes"; c5.mpg = 20; c6.make = "Siena"; c6.mpg = 22; c7.make = "Focus"; c7.mpg = 32; // put all the car info into cars.dat ofstream fout; fout.open("cars.dat", ios::binary); fout << c1.make << " " << c1.mpg << "\n"; fout << c2.make << " " << c2.mpg << "\n"; fout << c3.make << " " << c3.mpg << "\n"; fout << c4.make << " " << c4.mpg << "\n"; fout << c5.make << " " << c5.mpg << "\n"; fout << c6.make << " " << c6.mpg << "\n"; fout << c7.make << " " << c7.mpg << "\n"; // open cars.dat and go through the structure classes and put the cars in their associated new files (either over30.dat // or under30.dat) fout.open("cars.dat"); if(c1.mpg > 30) { ofstream fout; fout.open("over30.dat", ios::app); fout << c1.make << " " << c1.mpg << "\n"; } else { ofstream fout; fout.open("under30.dat", ios::app); fout << c1.make << " " << c1.mpg << "\n"; } if(c2.mpg > 30) { ofstream fout; fout.open("over30.dat", ios::app); fout << c2.make << " " << c2.mpg << "\n"; } else { ofstream fout; fout.open("under30.dat", ios::app); fout << c2.make << " " << c2.mpg << "\n"; } if(c3.mpg > 30) { ofstream fout; fout.open("over30.dat", ios::app); fout << c3.make << " " << c3.mpg << "\n"; } else { ofstream fout; fout.open("under30.dat", ios::app); fout << c3.make << " " << c3.mpg << "\n"; } if(c4.mpg > 30) { ofstream fout; fout.open("over30.dat", ios::app); fout << c4.make << " " << c4.mpg << "\n"; } else { ofstream fout; fout.open("under30.dat", ios::app); fout << c4.make << " " << c4.mpg << "\n"; } if(c5.mpg > 30) { ofstream fout; fout.open("over30.dat", ios::app); fout << c5.make << " " << c5.mpg << "\n"; } else { ofstream fout; fout.open("under30.dat", ios::app); fout << c5.make << " " << c5.mpg << "\n"; } if(c6.mpg > 30) { ofstream fout; fout.open("over30.dat", ios::app); fout << c6.make << " " << c6.mpg << "\n"; } else { ofstream fout; fout.open("under30.dat", ios::app); fout << c6.make << " " << c6.mpg << "\n"; } if(c7.mpg > 30) { ofstream fout; fout.open("over30.dat", ios::app); fout << c7.make << " " << c7.mpg << "\n"; } else { ofstream fout; fout.open("under30.dat", ios::app); fout << c7.make << " " << c7.mpg << "\n"; } // here is where i'm having trouble: char buf[80]; ifstream printCars; printCars.open ("cars.dat", ios::in); if (printCars.is_open() && printCars.eof()) for(int i=0;i<80;i++) cout << buf[i]; printCars.close(); return 0; }
If someone could help me out with this, that'd be killer.



LinkBack URL
About LinkBacks


