Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » File I/O

This thread is locked; no one can reply to it. rss feed Print
 1   2 
File I/O
Aikei_c
Member #14,871
January 2013
avatar

No, it can't be that, since 1) it happens after printf, 2) it doesn't change gender in any way.
If gender causes the problem then the gender pointer doesn't point to valid text data. Where do you assign value to gender? Problem is there somewhere.
To compare c strings you do this:

if (strcmp(gender,"Male") == 0) //if gender is "Male"
   //do something

ma3stro
Member #15,329
October 2013

#SelectExpand
1void ChooseGender(ALLEGRO_MOUSE_STATE state) 2{ 3 if(mouse[B1]) 4 if(state.x >= 100 && state.x <= 113 && state.y >= 112 && state.y <= 125 ) 5 { 6 al_draw_filled_rectangle(100, 112, 113, 125, al_map_rgb(0,0,0)); 7 gender = "Male"; 8 } 9 else if(state.x >= 200 && state.x <= 213 && state.y >= 112 && state.y <= 125 ) 10 { 11 al_draw_filled_rectangle(200, 112, 213, 125, al_map_rgb(0,0,0)); 12 gender = "Female"; 13 } 14 15}

I am using this function to get user gender.

I tried something else:

I changed "char *gender" to "char gender", "Male" to 'm' , "Female" to 'f' and %d to %c. It worked but 'รข' was written in the file not 'f' or 'm'.

EDIT: strcmp() didn't work either.

Edgar Reynaldo
Member #8,592
May 2007
avatar

ma3stro
Member #15,329
October 2013

You're not listening. You can't compare const char* by using the equivalence operator. That compares the addresses they store, not the data at those addresses. That's why you have to use strcmp.

Why wouldn't I listen while seeking for help? I wrote in my last message, I used strcmp() but it didn't work.

I used this:

#SelectExpand
1int value; 2. 3. 4. 5void ChooseGender(ALLEGRO_MOUSE_STATE state) 6{ 7 if(mouse[B1]) 8 if(state.x >= 100 && state.x <= 113 && state.y >= 112 && state.y <= 125 ) 9 { 10 al_draw_filled_rectangle(100, 112, 113, 125, al_map_rgb(0,0,0)); 11 value = 0; // Male 12 } 13 else if(state.x >= 200 && state.x <= 213 && state.y >= 112 && state.y <= 125 ) 14 { 15 al_draw_filled_rectangle(200, 112, 213, 125, al_map_rgb(0,0,0)); 16 value = 1; //Female 17 } 18 19} 20. 21. 22. 23if (value == 0) 24 cinsiyet = "Male"; 25 else 26 cinsiyet = "Female";

and it worked! ;D Not the efficient way though.

Thomas Fjellstrom
Member #476
June 2000
avatar

That's actually more efficient than comparing strings.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Edgar Reynaldo
Member #8,592
May 2007
avatar

ma3stro said:

Why wouldn't I listen while seeking for help? I wrote in my last message, I used strcmp() but it didn't work.

Sorry, I didn't see your edit. What do you mean, strcmp didn't work? How were you using it? It returns a negative number, 0, or a positive number based on the ascii collating sequence. Negative if the first string comes before, 0 if they are equivalent, and positive if the first string comes after the second.

I think your problem is that you are thinking of const char* as if they are strings. They really aren't. They are pointers to an array of char. I used to think that way when I first started using C++. You would think you could just compare them with the equivalence operator, and you can, but that only compares the addresses they store. And curiously enough, if ("string" == "string"); is valid code, but it is comparing two static addresses. Literal strings are usually allocated statically by the compiler, meaning there is usually only one instance of "string" even if you reference it more than once.

ma3stro
Member #15,329
October 2013

Well, I thought it worked but it was not. Program always saves passengers as "Female" because the if statement doesn't provide '0' then it goes for else. If I change 'else' to 'else if' error will be back.

I think the problem is in 'ChooseGender()'. Mouse[B1] becomes true just before program reaches to function line and 'mouse.state' cannot catch the coordinates and it always returns false.

Edgar Reynaldo
Member #8,592
May 2007
avatar

If you don't mind, I'd like to see your latest code.

Here's how I would check for gender equivalence.

bool male = true;

male = (expression_with_condition_for_being_male);

const char* gender = (male?"Male":"Female");
// this is the same as if you said
// if (male) {gender = "Male";} else {gender = "Female";}

al_draw_textf(font , allegro_color , x , y , "Gender = %s" , gender);

ma3stro
Member #15,329
October 2013

If you don't mind, I'd like to see your latest code.

Here's how I would check for gender equivalence.

bool male = true;

male = (expression_with_condition_for_being_male);

const char* gender = (male?"Male":"Female");
// this is the same as if you said
// if (male) {gender = "Male";} else {gender = "Female";}

al_draw_textf(font , allegro_color , x , y , "Gender = %s" , gender);

Sorry for not responding. I handed in the project on Tuesday. I used a boolean type variable(male) and deleted the string comparison part(which was no longer necessary).

Thank you all for your help, I appreciate it!

 1   2 


Go to: