functions proble

Reply

Join Date: Dec 2008
Posts: 17
Reputation: singhraghav has a little shameless behaviour in the past 
Solved Threads: 1
singhraghav singhraghav is offline Offline
Newbie Poster

functions proble

 
0
  #1
Dec 11th, 2008
Hey guys! i Have a prblm writing a program, i hope i can find help here.... in the prgram i have to take user input on gender as 'm' or 'f' or 'male' or 'female' or 'Male' or 'FmAle'.... what i mean is i the input should not be case sensetive... can anyone please help
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: functions proble

 
0
  #2
Dec 11th, 2008
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,145
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1434
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: Functions Problem

 
0
  #3
Dec 11th, 2008
>> can any one help?
Yes -- but I can't see what you have attempted so you will just have to post your code here.

>>the input should not be case sensetive
Convert the input string to all upper or lower case. You can use the toupper() and tolower() macros for that -- they work on only one character so you will have to do it in a loop.
Last edited by Ancient Dragon; Dec 11th, 2008 at 6:04 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 17
Reputation: singhraghav has a little shameless behaviour in the past 
Solved Threads: 1
singhraghav singhraghav is offline Offline
Newbie Poster

Re: functions proble

 
0
  #4
Dec 12th, 2008
#include <cstdlib>
#include <iostream>
#include <cctype>
using namespace std;

int main(int argc, char *argv[])
{char g;
int i;
str s;
cout<<"Enter your gender :"<<flush;
cin>>g;


for(i=0;i<s.length();i++)
g=tolower(g);

if (g=='m' && 'male')
cout<<"hello Mr."<<endl;

else
if(g=='f'&&'female')
cout<<"Hello Ms."<<endl;

i am really new to C++ ...thas why cudnt strt on my own..thx for the help neways... but i have been able to do this much only.... what cpmmand do i use to change a sstring to lower case?


system("PAUSE");
return EXIT_SUCCESS;
}
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 17
Reputation: singhraghav has a little shameless behaviour in the past 
Solved Threads: 1
singhraghav singhraghav is offline Offline
Newbie Poster

need correction!!

 
0
  #5
Dec 12th, 2008
hi guys!! i am trying to write a program in which i have to input gender in a non case sensetive manner.... and out mr or mrs...on the bases of the input.... i have to take input in 'm' 'f' or 'male' 'female' in any case. i ave tried this program but it doesnt wrkd... can ne1 hlp me out...


  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <cctype>
  4. #include <iomanip>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. string toLowerCase (string s)
  10.  
  11. {
  12. string g=s;
  13. toLowerCase(g);
  14. return g;
  15. }
  16. int main(int argc, char *argv[])
  17. {
  18. string s;
  19. cin>>s;
  20. cout<<toLowerCase(s)<<endl;
  21. int i;
  22.  
  23.  
  24.  
  25. cout<<"Enter your gender :"<<flush;
  26. cin>>s;
  27. cout<<s<<endl;
  28.  
  29.  
  30. {if (i=='m' && 'male')
  31. cout<<"hello Mr."<<endl;
  32.  
  33. else
  34. if(i=='f'&&'female')
  35. cout<<"Hello Ms."<<endl;
  36. }
  37.  
  38.  
  39. system("PAUSE");
  40. return EXIT_SUCCESS;
  41. }
Last edited by Ancient Dragon; Dec 12th, 2008 at 9:40 pm. Reason: correct code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,145
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1434
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: need correction!!

 
0
  #6
Dec 12th, 2008
  1. string toLowerCase (string s)
  2.  
  3. {
  4. string g=s;
  5. toLowerCase(g);
  6. return g;
  7. }

That's not how to convert a string to lower case. Use the tolower() macro in a loop and convert each character
  1. string toLowerCase (string s)
  2. {
  3. string g = s;
  4. for(int i = 0; i < g.length(); i++)
  5. g[i] = tolower[g[i]);
  6. return g;
  7. }

or you could do it this way in c++
  1. string toLowerCase (string s)
  2.  
  3. {
  4. string g=s;
  5. transform(g.begin, g.end(), g.begin(), tolower);
  6. return g;
  7. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: functions proble

 
0
  #7
Dec 12th, 2008
Also: why do you check i?
Instead of if(i=='m' && 'male') try if(s=="m" || s=="male") .

First correct your toLowerCase as AncientDragon suggested, then fix "minor" things like the one I pointed out to you (and others like double s input)
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 17
Reputation: singhraghav has a little shameless behaviour in the past 
Solved Threads: 1
singhraghav singhraghav is offline Offline
Newbie Poster

Re: functions proble

 
0
  #8
Dec 13th, 2008
thanks guysguess i got it almost... now m tryin to put the if else condition for outputing MR. if it is male...or else show an error message...but this doesnt wrk... can u point out the prblm/??

<code>
#include <cstdlib>
#include <iostream>

using namespace std;
string toLowerCase (string s)
{
string g = s;
for(int i = 0; i < g.length(); i++)
g[i] = tolower(g[i]);
return g;
}
int main(int argc, char *argv[])
{
string s;

cin>>s;
cout<<toLowerCase(s)<<endl;

if( s=="m" || "male")
cout<<"hello Mr."<<endl;
else
cout<<"error"<<endl;
system("PAUSE");
return EXIT_SUCCESS;
</code>
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 17
Reputation: singhraghav has a little shameless behaviour in the past 
Solved Threads: 1
singhraghav singhraghav is offline Offline
Newbie Poster

Re: functions proble

 
0
  #9
Dec 13th, 2008
hey can i use a void function for outputtin MR. after taking in the gender???
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 53
Reputation: BeyondTheEye is an unknown quantity at this point 
Solved Threads: 9
BeyondTheEye BeyondTheEye is offline Offline
Junior Poster in Training

Re: functions proble

 
0
  #10
Dec 13th, 2008
  1. if( s=="m" || "male")
Should be
  1. if( s=="m" || s=="male")

You have to compare it to the s variable again, else it will evalute "male" as a char array, which will always return true.
Last edited by BeyondTheEye; Dec 13th, 2008 at 2:10 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC