Reading numbers into an array of structs

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.

Driving me crazy. Please helpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  #include <iostream>
#include <cmath>
#include <fstream>
#include <string>


using namespace 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
Last edited on
Are you sure it opens the file correctly? Place something like this on line 58 to find out.
1
2
3
4
if (!inData.is_open())
{
	std::cout << "File not open!" << std::endl;
}
Last edited on
I mean i considered it, but i already know its opening the file because i can correctly manually cout any of the values in the 0 element of the array.

The printlist function isnt working because numcars never gets past 0 and so the for loop is never entered.
Last edited on
anyone? :(
bump
Lines 95-96
1
2
	in >> onecar.mileage;
	getline (in, onecar.make);
Woops, you are mixing stream extraction and getline.
if there is a newline symbol between mileage and make, you are in trouble.
http://www.cplusplus.com/forum/articles/6046/
http://stackoverflow.com/questions/9336209/mixing-ifstream-getline-and
That's.... would that affect numcars from incrementing? D:

aw hell

I doubled checked it and i have the char getting the newline character in the right place.
Last edited on
Give your current code and file example
Code hasnt changed since last night, i put up the input file.
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...
Last edited on
In that case, you should not have actionNeeded as your struct member and remove it from getcars function.
Read this character and make a switch like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
char actionNeeded;
std::cin >> actionNeeded;
switch(actionNeeded) {
  case 'A':
    Vehicles car;
    getcars(car, inData);
    //Add car into array.
    break;
  case 'R':
    Vehicles car;
    getcars(car, inData);
    //Remove car
    break;
  case 'L'
    //list cars
}
Last edited on
I see. thank you. I had some idea of it i swear! i think the getline just screwed up my plans.

Appreciate it!
Topic archived. No new replies allowed.