THE 0?

Hi;
My input file is this

1
2
3
4
5
6
7
Andy   15
Bob    17
Cathy  16
David  17
Edward 16
Freda  14


I keep getting an extra 0 in my array which is really strange, here is the main.
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
#include "functions2.h"




int main()
{
	int hitL[PLAYERCOUNT]= {0}, cardsT[PLAYERCOUNT] = {0}, 
	index = 0, i = 0;
	string playersname[PLAYERCOUNT];
	char limit = ' ' ;
	ifstream fin("input.txt");

	srand((unsigned)(1234));
	
	//Shuffle cards
	shuffleCards(index);
	
	
	
	//Input
	while(!fin.eof())
	{   
		i++;
		fin >> playersname[i] >> hitL[i];
	}
	
	for(int j = 0; j < PLAYERCOUNT; j++) //PLAYERSCOUNT = 7
	{	//This appears to be ok
		//cout << playersname[j] << endl;
		//this has the extra 0
		cout << hitL[j] << endl;
		
		//cout << playerStat[j] << endl;
	}


	//Game function
	game(playersname,hitL,i,cardsT);

system("pause");


return 0;
}

When I was looking at the contents of my array I have
1
2
3
4
5
6
7
   0
   15
   17
   16
   17
   16
   14

I don't understand this. Why is this happening?
At line 24 you're incrementing i before reading into playersname[i], therefore, the first read will go into playersname[1], not playersname[0].

Thanks that helped. I noticed that when I did that though my count was off so I put a statement before loop to correct it now I get this box that appears.
Unhandled exception at 0x5cb8cac8 (msvcr100d.dll) in mp7.exe: 0xC0000005: Access violation writing location 0xcccccccc.

Everything is the same as above expect this

1
2
3
4
5
6
7
8
9
10
11
12
13
fin >> playersname[i] >> hitL[i];
	
	//Input
	while(!fin.eof())
	{   
		i++;
		fin >> playersname[i] >> hitL[i];
	     
	}
	
	
	//Game function
	game(playersname,hitL,i,cardsT);


I was getting a buffer error before this. That I was getting to in my list of things so after I changed this then I get the error I posted above. The compiler doesn't take me to a line so I don't know what ups.
1
2
3
4
fin >> playersname[i] >> hitL[i];   

        while ( i < PLAYERCOUNT && (fin >> playersname[i] >> hitL[i]) )
            ++i ;


Looping on eof is almost always the wrong thing to do, and the sanity check on the value of i can't hurt.
Hi Cire,
Why is looping on eof bad? I tried to change it and I still get the same error. I'm not sure that looping on eof is the problem
You know how when you get an unhandled exception it takes you to another tab in visual studio? Well mine did so along with the error I posed above it takes me to that tab and there is a yellow arrow pointing at this.

mov [edi+ecx*4-4],eax ;U - put dword into destination
Dude, I have not a slight idea what this is.


Why is looping on eof bad?


Because you've no guarantee that the previous input operation was successful. That often leads to people trying to read in one more record than there actually was in the file, and treating the failed extraction as if it were successful (resulting in 2 copies of the last record in the file.)

You know how when you get an unhandled exception it takes you to another tab in visual studio?


That's the point at which you check the call stack and see what the last function of yours was that was called. In the Debug menu, choose the windows option and then Call Stack.
All I got is Debug => windows => break points, output , immediate ...
Is it one of these? I never even knew you could this.
You have to start debugging before you get more options.
OK so the ones that appear in my list from bottom to top are the order in which they are called? Is this just the list of function calls? I get a bunch of crap for instance

1 stuff I don't know in other tabs
2 stuff I don't know in other tabs
3 stuff I don't know in other tabs
4 stuff I don't know in other tabs
5 pointing to a line in my program
6 pointing to a line in my program
7 pointing to a line in my program
8 pointing to a line in my program

One being the newest.

This is the last function before the crap which is 5 in my example
> mp7.exe!Dealer(std::basic_string<char,std::char_traits<char>,std::allocator<char> > * dName, int * dHit, int i) Line 18 C++


This is the function
1
2
3
4
5
6
//Functions
void Dealer(string dName[], int dHit[], int i)
{    
	dName[i+1] = "Dealer";
	dHit[i+1] = DEALER;    // arrow points to this line
}


That's pretty cool you can do that would of saved me a lot of time to know that 5 weeks earlier. :)
Topic archived. No new replies allowed.