Skip to main content
GameDev.net gamedev.net
🔒 Locked

Really confusing problem...

Started by Xero-X2 Nov 29, 2002 at 1:36 AM 5 replies 250+ views
Original Post
Xero-X2
Xero-X2
Ok I have this...
struct TempBone
{
int link;
int name;
char* mapper;
};


void CreateStuff(char* Filename)
{
char* Name;

ifstream input;
input.open(Filename);

if (input.is_open())
{
string temp="SOF";
input >> temp;
Name=(char*)temp.c_str();//Correctly set

vector List;
TempBone tempar;

while (temp != "EOF") //This works
{
input >> temp;
tempar.mapper=(char*)temp.c_str();//Correctly set
input >> tempar.name;
input >> tempar.link;

if (temp!="EOF") //This always returns true for some reason... I have also tried this as, if (tempar.mapper!="EOF")   but it had the same responce.
List.push_back(tempar);
}

... Processing. I learned that no matter what I have done there is still a item in the List Array that has EOF as its .mapper var.

}

  
why in the world would this not work correctly, do char*'s and string's not support the != operator or something?? is any one else as confused as I am. here is what I have tried 1. changeing to if (tempar.mapper!="EOF") 2. rebuilding the Project from scratch 3. I also tried to remove it after it is added but that has no effect on the problem either!?!?!? any ideas or solutions out there? [edited by - Xero-X2 on November 29, 2002 3:07:21 AM]
"I seek knowledge and to help those who also seek it"
silvermace
silvermace
i think this might work

    
struct TempBone{
int link;
int name;
char* mapper;
};

void CreateStuff(char* Filename)
{
char* Name;
ifstream input;
input.open(Filename);

if (input.is_open()){
string temp="SOF";

input >> temp;
Name=(char*)temp.c_str();//Correctly set


vector List;
TempBone tempar;

while (temp != "EOF") //This works

{
input >> temp;
tempar.mapper=(char*)temp.c_str();//Correctly set

input >> tempar.name;
input >> tempar.link;
List.push_back(tempar);
}

List.pop_back();

// ... Processing. I learned that no matter what I have done there is still a item in the List Array that has EOF as its .mapper var.

}



or, you could just push the push_back at the top

eg.
GetNextValue();
List.push_back(...);
while( condition )
{
List.push_back();
GetNextValue();
}


hope it helps

[edited by - silvermace on November 29, 2002 6:01:46 AM]
Xero-X2
Xero-X2
After a little bit of reseasuch into this by posting MessageBox(...)''s to reveal the inter workings of my program i discovered that the values in the varible Tempar are not really transfered over into the vector List. the first few are correct but then it stops being correct and it starts making up stuff like "EOF", "NECK!" and other things.. While when I set Tempar the values are correct but after they are pushed into the List The Value of List[0] starts going berserk and it starts becoming EOF and other nonsence, Any Idea whats up with that.
"I seek knowledge and to help those who also seek it"
DrPizza
DrPizza
quote:
tempar.mapper=(char*)temp.c_str();//Correctly set

It''s marked as const for a reason.

After that assignment, the const char* array no longer exists. Mapper is pointing at something random.

Just stick strings in TempBone.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;} /*** drpizza@battleaxe.net ***/
SabreMan
SabreMan
quote:
Original post by DrPizza
After that assignment, the const char* array no longer exists. Mapper is pointing at something random.

The return value of c_str() is guaranteed to be good until the next non-const operation on the string. The reason for the OP''s problem is that he is comparing pointers, not strings.
SabreMan
SabreMan
quote:
Original post by Xero-X2
while (temp != "EOF") //This works

It doesn''t work in the sense you think it does. It returns the result you are expecting, but not from performing the operation you think. It is actually comparing pointers, and it "works" because the address of temp is not equal to the address where "EOF" is being held.
quote:

why in the world would this not work correctly, do char*''s and string''s not support the != operator or something??

They both support the != operator, but the char* version compares pointers, *not* the strings pointed to. Do yourself a favour and use std::string throughout.
Xero-X2
Xero-X2
Ok Its all fixed now Thanks for all that help It was that the Pointer to the Char''s were changing I fixed it by copying the chars to their own arrays and then doing things with them. it all works now.
"I seek knowledge and to help those who also seek it"

Topic Locked

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

Sign in to reply to this topic.