Skip to main content
gamedev.net
🔒 Locked

Code Confusion..

Started by AtariMan 4 replies 600+ views
Original Post
AtariMan
AtariMan
Hello all i have a program that i have just finished and everything works except for the below function. It will only load the name from the first object in the array then it reads blank for the rest. However i have tried using "cout << classone.name;" after "classone[i].name[c]=namebuff[c];" and it outputs all of the letters in the file.... I am truley confused. If a more experienced coder could look at this and give me some advice i would be very greatful. If you need the namerec.dat file i can send it to you. It was created by writing a string of names seperated by "1"''s and ended with "1" "0" into a file using ofstream. so an example string would look like this. "bob1stu1john1cameron10" now heres the function. Everything is declared localy so you dont need to worry about any other vars. void loadname(); void loadname() { char namebuff[400]; infile.open("namerec.dat",ios::in); infile >> namebuff; infile.close(); int c=0; char d[]="0"; char e[]="1"; int i=0; while(namebuff[c]!= *d) { classone[i].name[c]=namebuff[c]; c++; if(namebuff[c]== *e) { c++; i++; } } } if you want to email me to get more info or the namerec.dat file my address is racerx@programmer.net Thanks Eric
mossmoss
mossmoss
I see two potential problems here.

First, unless you have previously filled the classone[ i ].name arrays with zeros, your names will not be null-terminated, which is a bad thing.

Second, you use c to index both the input buffer and each of the name strings. However, once you switch to the second name (where you do i++ ) c is no longer an appropriate index. You should maintain a separate index.

So, taking your code as provided, the fewest modifications I would make would produce this:

        
void loadname()
{
char namebuff[ 400 ];
infile.open("namerec.dat", ios::in);
infile >> namebuff;
infile.close();

int c=0;
char d[ ]="0";
char e[ ]="1";
int i=0;
int x=0;

while(namebuff[ c ] != *d)
{
classone[ i ].name[ x ] = namebuff[ c ];
classone[ i ].name[ x+1 ] = 0;
c++;
x++;
if(namebuff[ c ] == *e)
{
x=0;
c++;
i++;
}
}
return 0;
}




---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!

Edited by - mossmoss on September 14, 2000 9:33:03 AM
Myopic Rhino
Myopic Rhino
your problem is probably coming from this:

    
if(namebuff[c]== *e)
{
c++;
i++;
}


You increment i, moving to the next string in the classone array, but then you start copying into the new string somewhere in the middle of it, rather than copying into it starting at index 0. You need to track the index of the array you''re copying into seperately.
AtariMan
AtariMan
Thank you thank you thank you. I have been going nuts over that code and to have it be such a small oversite like that drives me nuts! I guess the longer you stare at a screen trying to find a bug, the less you will see... Anyway Thank you.
Eric
Nurgle
Nurgle
The best way to find bugs liek that is to use a debugger. The amount of times I''ve seen people trying to solve things like that, when it could be easilly found by just setting a breakpoint at the start of the function and finding where it goes wrong...

http://www.thisisnurgle.org.uk

"Nazrix is cool" Nazrix first, then Darkmage, then Nazrix again
After careful deliberation, I have come to the conclusion that Nazrix is not cool. I am sorry for any inconvienience my previous mistake may have caused. We now return you to the original programming
mossmoss
mossmoss
quote:
The best way to find bugs liek that is to use a debugger. The amount of times I''ve seen people trying to solve things like that, when it could be easilly found by just setting a breakpoint at the start of the function and finding where it goes wrong...



I''ll second that... In fact, that''s how I found the bug in the original code, and it me less time to cut/paste/compile/debug than to read through it.

Learning to use a debugger adds an immensely powerful tool to your arsenal.

quote:

Nazrix is cool, then Darkmage, then Nazrix again



I''m about 11,548,924-th on that list of coolness....


---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.