In: Computer Science
C++ UNIX
car.txt
0001|Mercury|LeSabre|2005|Brown|11000|2019-09-01
0002|Chevrolet|Aveo|2013|Blue|12000|2019-09-02
0003|Datsun|240Z|1979|Orange|13000|2019-09-03
0004|Ford|Galaxie|1964|Black|14000|2019-09-04
0005|Porsche|Boxster|2014|Green|15000|2019-09-0
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
const int RECORDS = 42;
ifstream reader("car.txt");
if(!reader) {
cout << "Error: cannot open
input file" << endl;
return -1;
}
string item[RECORDS];
int i = 0;
while(!reader.eof()) {
if((i+1) % 7 == 0)
getline(reader,item[i++],'\n');
else
getline(reader,item[i++],'|');
}
i = 0;
while(i < RECORDS) {
cout << "(1) carID= "
<< item[i++] << endl;
cout << "(2) carManufacturer=
" << item[i++] << endl;
cout << "(3) carModel= "
<< item[i++] << endl;
cout << "(4) carYear= "
<< item[i++] << endl << endl;
cout << "(5) carColor= "
<< item[i++] << endl << endl;
cout << "(6) carPrice= "
<< item[i++] << endl << endl;
cout << "(7)
carInventoryDate= " << item[i++] << endl << endl;
}
reader.close();
return 0;
}
this is my code to read that text file.
and output should be
(1) carID=0001
(2) carManufacturer=Mercury
(3) carModel=LeSabre
(4) carYear=2005
(5) carColor=Brown
(6) carPrice=11000
(7) carInventoryDate=2019-09-01
and so on.
but my output is
(1) carID= 0001
(2) carManufacturer= Mercury
(3) carModel= LeSabre
(4) carYear= 2005
(5) carColor= Brown
(6) carPrice= 11000
(7) carInventoryDate= 2019-09-01
like this. I don't know why there is space between 4, 5, 6, 7
please help me to fix
Thank you
The problem is you have too many line breaks (endl) in your code. cout<<endl; prints one line break, cout<<endl<<endl; prints two line breaks. In your code (inside while method) you print endl twice in all statements, which is the reason why your output has so many blank lines. I have simply removed one endl from each statement, now it works as it should. Note that I didn’t remove the two endl in last cout statement, so that each car’s output will be separated by a blank line, remove that also if you want.
Here is the corrected code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
Note: Considering the code and input file, I think you have to create a class/structure to hold records of each car instead of simply reading and printing as a block of strings. Anyway I didn’t change anything else, just formatted your output only.
EDIT: added the record number display.
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
const int RECORDS = 42;
ifstream reader("car.txt");
if(!reader) {
cout << "Error: cannot open input file" << endl;
return -1;
}
string item[RECORDS];
int i = 0;
while(!reader.eof()) {
if((i+1) % 7 == 0)
getline(reader,item[i++],'\n');
else
getline(reader,item[i++],'|');
}
i = 0;
int recordNum=1; //record number
while(i < RECORDS) {
//printing record number
cout<<"Record#"<<recordNum<<endl;
//incrementing record number
recordNum++;
cout << "(1) carID= " << item[i++] << endl;
cout << "(2) carManufacturer= " << item[i++] << endl;
cout << "(3) carModel= " << item[i++] << endl;
cout << "(4) carYear= " << item[i++] << endl;
cout << "(5) carColor= " << item[i++] << endl;
cout << "(6) carPrice= " << item[i++] << endl;
//only printing two line breaks (endl) after the last item of each record to separate each
//object
cout << "(7) carInventoryDate= " << item[i++] << endl << endl;
}
reader.close();
return 0;
}