Look for pointers and dynamic memory allocation. Oh and if you want us to help you find the error, maybe you should post some code (or at least up to the point where it freezes). Thanks.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
Hmm...here is what I think you should do:
1. Always keep a guard condition while making header files which does away with the problem of recursive inclusion of headers. Something like:
#ifndef TGA_H_
#define TGA_H_
// your header file here
#endif
2. Don't know why but your loop looks fishy:
while ( In == NULL )
{
cout << "open failure. Please check the file's location and try again.\n";
cout << "What is the map file's name? ";
cin >> sMap;
In.open( sMap.c_str() );
}
Try removing the loop and ducking out if the fle is not found.
3. Tada..and this I think according to me is the culprit:
while ( tempChar != '/n' ) // loop to find the row and col numbers.
Just to let you know there is no such thing as /n, it must have been a typo mistake since you should write \n if validating against a newline character.
4. Oh yes...don't use system("pause") , getchar( ) achieves the same thing without putting portability at stake....
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
if ( i > 2 )
{
tempString = tempChar;
row = atoi( tempString.c_str() );
}
else
{
tempString = tempChar;
col = atoi( tempString.c_str() );
}
The above is much too complicated. All it is doing is converting a single character to an integer. Just simply subtract '0'.
if ( i > 2 )
{
row = tempChar - '0';
}
else
{
col = tempChar - '0';
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
The while ( In == NULL ) loop actually works perfectly. That's how I've always done that.
I see your logic -- if the file was not found, then ask for a new file name.I never did understand the stuff to prevent the recursive header thing, I don't really get it yet. I'll have to ask someone to explain that for me.
Its for instances when a .h file is included two or more times, which often happens with your compiler's standard header files. This prevents symbols from being defined more than once.And why would subtracting '0' from a character convert it to an integer?
lets take the example of '1'. '1' - '0' = 49 - 48 = 1. You can use a good ascii chart to see all the decimal values of the characters. I like to use '0' in my programs instead of 48 because it makes more sense.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
while I'm at it, for the #ifndef stuff, should I be putting the class name in there instead of a TGA_H? I thought I had seen it done that way before...
Leave it as TGA_H. The #ifndef followed by the #define are a way to tag your file uniquely so that it never gets #included more than once (which would cause multiple-definition hell). If you use an IDE which autogenerates those lines (old versions of VC++ used to, but I think newer ones use the compiler-specific #pragma once ) you'd see pretty much a random string so there would never be issues with another file having the same tag as yours.
Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
while I'm at it, for the #ifndef stuff, should I be putting the class name in there instead of a TGA_H? I thought I had seen it done that way before...
I always use the header filename, but it doesn'r really matter as long as it isn't duplicated anywhere else.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343