Loop Ending Problem

Loop End Prob with 2 possible ends

Page 1 of 1

6 Replies - 2882 Views - Last Post: 06 January 2002 - 08:15 PM Rate Topic: -----

#1 Kenshin Rules  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-January 02

Loop Ending Problem

Posted 04 January 2002 - 11:40 PM

First of all this is my first post. I have only been in C++ for 6 months since my school has opened up the class. anyways I have been tried what I could to get this function to work. It gets the gender in from the user in character and then if they enter in the m or f then loop ends. I tried just ending the loop if m is entered and commented out f. The code worked but when I tried them both together again it still didn't work. I would appreciate any help from anyone on how to make this easier or just what is wrong with the code. thanks for the time.
void Gender()
{
char sex=0;
while(sex!='m'||sex!='f')
{
sex=0;
cout<<"What is your character's gender?M or F: "<<endl;
cin>>sex;
sex=tolower(sex);
}
if(sex=='m')
gender="Male";
else
gender="Female";
}
Gender is global string.

(Edited by Kenshin Rules at 3:01 am on Jan. 5, 2002)


Is This A Good Question/Topic? 0
  • +

Replies To: Loop Ending Problem

#2 blutrane  Icon User is offline

  • 私もクールです

Reputation: 25
  • View blog
  • Posts: 1,934
  • Joined: 17-May 01

Re: Loop Ending Problem

Posted 05 January 2002 - 07:41 AM

take out sex=0; you don't need it and also the character variable sex should not have initialized as 0 either.

i think i see what you are doing, but you don't need to clear the array with 0. when you cin again, it will over write itself

Was This Post Helpful? 0
  • +
  • -

#3 elite eagle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 03-January 02

Re: Loop Ending Problem

Posted 05 January 2002 - 10:22 AM

i thought a char variable could only be one character, so when u say gender="male" etc., isnt that illegal? dunno.
Was This Post Helpful? 0
  • +
  • -

#4 runtime error  Icon User is offline

  • Lucky.Code
  • member icon

Reputation: 3
  • View blog
  • Posts: 629
  • Joined: 19-March 01

Re: Loop Ending Problem

Posted 05 January 2002 - 10:42 AM

The program will only store one character in the variable and will store all that other stuff somewhere in memory. so when the user enters "male" the program will only read it as if they entered 'm'.
Was This Post Helpful? 0
  • +
  • -

#5 Kenshin Rules  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-January 02

Re: Loop Ending Problem

Posted 05 January 2002 - 11:12 AM

Quote

Quote: from elite eagle on 1:22 pm on Jan. 5, 2002
i thought a char variable could only be one character, so when u say gender="male" etc., isnt that illegal? dunno.

Well gender is global string that I initalized at beginning of program(last line of my first post). I could have just passed it but it was just as fast to run it as a global.
Was This Post Helpful? 0
  • +
  • -

#6 runtime error  Icon User is offline

  • Lucky.Code
  • member icon

Reputation: 3
  • View blog
  • Posts: 629
  • Joined: 19-March 01

Re: Loop Ending Problem

Posted 06 January 2002 - 11:29 AM

oops i thought you meant the char variable well what kenshin did was created a string variable called gender.

either by declaring a string like this

string gender;


or like this
char gender[7];

thus allowing it to accept more than one character you'll learn about string variables soon if your taking a class in C++.

Was This Post Helpful? 0
  • +
  • -

#7 malkiri  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 3
  • View blog
  • Posts: 364
  • Joined: 29-March 01

Re: Loop Ending Problem

Posted 06 January 2002 - 08:15 PM

Two things:
1. Yes, sex should be initialized at some point before the loop. The line where you declare it is good enough. The reason is that unlike global variables, which are automatically initialized, local variables are not. This means there's some random junk in the variable when you first declare it. Chances are it's not the characters 'm' or 'f', but you should still initialize, just in case.
2. The reason your loop isn't terminating is the clause:
sex != 'm' || sex != 'f'


If sex is 'm', then you'll get this:
false OR true -> true
If sex is 'f', then you'll get this:
true OR false -> true
See? What you actually want is for the clause to evaluate to false when sex is either 'm' or 'f'. Another way of saying that is you want the clause to evaluate to false when sex is not 'm' and not 'f'.
See if that helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1