CFanatic

Go Back   CFanatic > Programming > C Programming

Join CFanatic Forum Now

Loop problem topic posted under C Programming which is a part of Programming category in CFanatic Forum
Reply
 
Thread Tools Display Modes
  #1  
Old 04-02-2010, 09:11 PM
Member
 
Join Date: Feb 2010
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
creative is on a distinguished road
| More
Loop problem

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.
Code:
void Gender()
{
char ***=0;
while(***!='m'||***!='f')
{
    ***=0;
    cout<<"What is your character's gender?M or F: "<<endl;
    cin>>***;
    ***=tolower(***);
}
if(***=='m')
    gender="Male";
else
    gender="Female";
}
Gender is global string.
Reply With Quote
  #2  
Old 04-02-2010, 09:12 PM
pankaj.sea's Avatar
Super Moderator
 
Join Date: Sep 2009
Location: Serampore
Posts: 132
Thanks: 0
Thanked 2 Times in 2 Posts
pankaj.sea is on a distinguished road
| More
Re: Loop problem

take out ***=0; you don't need it and also the character variable *** 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
Reply With Quote
  #3  
Old 04-02-2010, 09:13 PM
Senior Member
 
Join Date: Feb 2010
Posts: 102
Thanks: 0
Thanked 0 Times in 0 Posts
meyup is on a distinguished road
| More
Re: Loop problem

i thought a char variable could only be one character, so when u say gender="male" etc., isnt that illegal? dunno.
Reply With Quote
  #4  
Old 04-02-2010, 09:13 PM
Member
 
Join Date: Feb 2010
Posts: 86
Thanks: 0
Thanked 0 Times in 0 Posts
techme is on a distinguished road
| More
Re: Loop problem

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'.
Reply With Quote
  #5  
Old 04-02-2010, 09:14 PM
Member
 
Join Date: Feb 2010
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
creative is on a distinguished road
| More
Re: Loop problem

Quote:
Originally Posted by meyup View Post
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.
Reply With Quote
  #6  
Old 04-02-2010, 09:16 PM
Senior Member
 
Join Date: Feb 2010
Posts: 102
Thanks: 0
Thanked 0 Times in 0 Posts
meyup is on a distinguished road
| More
Re: Loop problem

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

Code:
string gender;
or like this

Code:
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++.
Reply With Quote
  #7  
Old 04-02-2010, 09:17 PM
pankaj.sea's Avatar
Super Moderator
 
Join Date: Sep 2009
Location: Serampore
Posts: 132
Thanks: 0
Thanked 2 Times in 2 Posts
pankaj.sea is on a distinguished road
| More
Re: Loop problem

Two things:
1. Yes, *** 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:

Code:
*** != 'm' || *** != 'f'
If *** is 'm', then you'll get this:
false OR true -> true
If *** 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 *** is either 'm' or 'f'. Another way of saying that is you want the clause to evaluate to false when *** is not 'm' and not 'f'.
See if that helps.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Couting the attempts in a 'while' loop? HannahMar C# Programming 4 11-23-2010 10:33 AM
Do While loop syntax codeunlock C# Programming 1 04-29-2007 08:31 AM

 

Advertisement

CFanatic Search
Custom Search

Advertisement

All times are GMT -5. The time now is 04:54 AM.



Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO ©2010, Crawlability, Inc.
Cfanatic.com is a premier member of the IDG TechNetwork. For advertising opportunities contact here
Get Paid for Working on Projects Matching Your Expertise at Go4Expert's Jobs Board