The while loop involving numcars isnt working for some reason. it wont enter the loop and as a result numcars never increments past 0. Also my printlist isnt printing anything out though i can manually cout anything in 0 range of the array.
#include <iostream>
#include <cmath>
#include <fstream>
#include <string>
usingnamespace std;
int addCar(int);
int removeCar(int);
struct Vehicles{
char actionNeeded;
string make;
string model;
string color;
int price;
int mileage;
int year;
int distance;
};
void printList(Vehicles onecar);
void getCars (Vehicles& , ifstream& );
int main()
{
//ifstream inData;
//inData.open("Vinfo.txt");
/*cout << "Enter the input data file name:" << endl;
cin >> infileName;
inData.open(infileName.c_str());
// make it a loop error message so user doesnt have to restart program. Annoying
if(!inData)
{
cerr << "Can't open: " << infileName << ". Program is stopping" << endl;
return 1;
}
*/
Vehicles carInfo[1024]={0};
Vehicles onecar;
ifstream inData;
int numcars=0;
inData.open("Vinfo.txt");
getCars(carInfo[numcars],inData);
while(inData)
{
numcars++;
getCars(carInfo[numcars],inData);
}
for( int i=0; i<numcars; i++)
{
printList(carInfo[i]);
}
cout << carInfo[0].price<< endl;
cout << "The number of cars is:" << numcars << endl;
return 0;
}
void getCars (Vehicles& onecar, ifstream& in)
{
char fucknewline;
in >> onecar.actionNeeded;
in >> onecar.price;
in >> onecar.year;
in >> onecar.mileage;
getline (in, onecar.make);
getline (in, onecar.model);
getline (in, onecar.color);
in >> onecar.distance;
in.get(fucknewline);
}
void printList(Vehicles onecar)
{
cout << onecar.actionNeeded;
cout << onecar.price;
cout << onecar.year;
cout << onecar.mileage;
cout << onecar.make;
cout << onecar.model;
cout << onecar.color;
cout << onecar.distance;
}
Input file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
A 7989 2008 93843 Toyota Prius Gray 405
A 8993 2005 85227 Honda Civic White 377
A 8998 2006 82015 Honda Insight Red 441
R 8993 2005 85227 Honda Civic White 377
L
A 9800 2007 65434 Toyota Prius Gray 465
A 9995 2010 42000 Honda Insight Silver 464
R 8998 2006 82015 Honda Insight Red 441
R 9995 2010 42000 Honda Insight Silver 464
A 9998 2008 92653 Toyota Prius Red 200
A 10000 2008 85096 Toyota Prius Unspecified 405
A 10000 2010 100000 Honda Insight Gray 470
A 10000 2008 87183 Toyota Prius Pink 576
R 10000 2010 100000 Honda Insight Gray 470
L
R 9999 2005 74446 Honda Civic Blue 595
P
getline (in, onecar.make); will take whole line until first encountered newline, so it will be "Toyota Prius Gray 405" for first car. getline (in, onecar.model); then will put "A 8993 2005 85227 Honda Civic White 377" in model. getline (in, onecar.color); will put "A 8998 2006 82015 Honda Insight Red 441" in color. in >> onecar.distance; will then encounter "R" and fail, setting stream into failed state and failing all subsequent inputs. Then your While loop condition checked and evaluated to false, skipping loop completely.
If your model/color/maker is always one word, you can justt use in >> model, etc.
But even if you fix this, your program will still fail when it will encounter those 'L' with empty line after them.
Cant i just make a function for L ( its supposed to be the command to List the cars, A and R are add an remove respectively) and when it encounters it, call it somewhere within with loop with an if statement?
Also thank you, i completely forgot what getline did...