Reading input files Error

Hey guys,

I have an input file that looks like this
Fahman 75 84 83
Saad 74 83 82
Mohsin 23 23 23
Waqas 23 92 92
ray 23 02 093


When I'm printing it out in the console, I get this
Fahman 75 84 83
The rest of the input don't appear on the console and I get this wieird error message that I'm encountering for my first time in c++ language. In a short note, lets just say it says unhandled exception.

I'm not sure what the issue is over here but I have a feeling it has to do with my improper while(firstname<=...)

here is my following code. looking forward for someone to assist me here. Thanks@@@

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
//Make a function to read the  data from input file
void readdata(string names[],int namesize,int ranMile[][3],int Ran_rowsize, ifstream &in)
{
	int firstname=1;
	int ransize=1;

	//read the file first
	in>>names[firstname];
	cout<<names[firstname]<< " ";
	//read each column
	for (int i=1;i<=3;i++)
	{
		in>>ranMile[ransize][i];
		cout<<ranMile[ransize][i];
		cout<<"\t";
	}

	while (firstname<=namesize ) 
		//increment first name plus
		firstname++;
		ransize;
		
		//Get the names
		in>>names[firstname];
		cout<<names[firstname];
	//read each column
	for (int i=1;i<=3;i++)
	{
		in>>ranMile[ransize][i];
		cout<<ranMile[ransize][i];
		cout<<"\t";
	}


}
int _tmain(int argc, _TCHAR* argv[])
{

	//Open files
	string namefile;
	ifstream in;
	openfile(namefile,in);

	cout<<"\nProcessing Data\n\n";
	


	//Declare the size for how many students will be there in a  input
	string names[5];
	int milesrun[5][3];
	readdata(names,5,milesrun,5,in);


I get this wieird error message that I'm encountering for my first time in c++ language.
But you don't tell us what it is?

well, the loop on line 18 has has no { } which means that the very first following line will be excuted within the loop only. In other words firstname will be increased, nothing else. On line 24/25 firstname is out of bounds.

Note that an array always starts with 0. The index for an array is always 0 to size_of_the_array - 1: e.g. line 27: for (int i=0;i<3;i++)
Coder. I have the program working coherently as of now. So we had the input data like
Fahman 75 84 83
Saad 74 83 82
Mohsin 23 23 23
Waqas 23 92 92
ray 23 02 093


This is the code I had that properly outputted the input data on the output console.
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
//Make a function to read the  data from input file
void readdata(string names[],int namesize,int ranMile[][3],int Ran_rowsize, ifstream &in)
{
	int firstname=0;
	int ransize=0;
	cout<<endl;
	

	while (in>>names[firstname] &&firstname<namesize) 
	{
		cout<<names[firstname];
			//increment first name plus
			firstname++;
			
		//read each column
		for (int i=0;i<3;i++)
			{
				in>>ranMile[ransize][i];
				cout<<ranMile[ransize][i];
				cout<<"\t";
			}
		ransize++;
	}

}
int _tmain(int argc, _TCHAR* argv[])
{

	//Open files
	string namefile;
	ifstream in;
	openfile(namefile,in);

	cout<<"\nProcessing Data\n\n";
	


	//Declare the size for how many students will be there in a  input
	string names[5];
	int milesrun[5][3];
	readdata(names,5,milesrun,5,in);


Comparing this to the following code below, this code here doesn't seem to work. I get an error on this code saing unhandled exception at 0X .... in marathon file.exe: 0xc00....: access violation writing location 0x00000..

1
2
3
4
5
6
void readdata(string names[],int namesize,int ranMile[][3],int Ran_rowsize, ifstream &in)
{
	int firstname=1;
	int ransize=0;
	cout<<endl;
	


I know why it doesn't work and like you said there out of bounds. From what I understand, the input data will be read until the end of file is found. I have a total of 5 elemtns of name in that file. The last one "ray" is the only one that doesn't get outputted. Can you tell me why that is so or visually show me an example whats happening when I'm using this code please :)
The problem in readdata on line 9 is that you read the name first and then you check whether firstname is out of bounds. firstname becomes invalid after you read "ray". It should be the other way round. So change it to

while (firstname<namesize && in>>names[firstname])

not that beautiful, but it should work
lol when you say not that beautiful, are you pointing out that this is not a professional way to do it? As a beginner, would this be ok? Don't want to bug you too much, but for general knowledge how can I modify this code more?
Well, I'd say that it isn't the best approach when the order of appearance/evaluation decides whether your program crashes or not.

more consistent and foolproof would be this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void readdata(string names[],int namesize,int ranMile[][3],int Ran_rowsize, ifstream &in)
// Note: if Ran_rowsize != namesize you need to check it before 'read each column'
{
	cout<<endl;
	
	// firstname and ransize are the same, so why not using a simple index?
	for(int i=0; in.good() && (i<namesize); i++) 
	{
		if(in>>names[i])
			cout<<names[i];
			
		//read each column
		for (int j=0;in.good() && (j<3);j++)
			{
				if(in>>ranMile[i][j])
				{
				cout<<ranMile[i][j];
				cout<<"\t";
				}
			}
	}

}
hey coder, what does in.good() mean again?
Read this:

http://www.cplusplus.com/reference/ios/ios/good/

in.good() returns true as long as no problem is indicated by the stream (such as eof)
Topic archived. No new replies allowed.