| | |
functions proble
![]() |
>> 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.
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.
•
•
Join Date: Dec 2008
Posts: 17
Reputation:
Solved Threads: 1
#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;
}
#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;
}
•
•
Join Date: Dec 2008
Posts: 17
Reputation:
Solved Threads: 1
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...
C++ Syntax (Toggle Plain Text)
#include <cstdlib> #include <iostream> #include <cctype> #include <iomanip> #include <string> using namespace std; string toLowerCase (string s) { string g=s; toLowerCase(g); return g; } int main(int argc, char *argv[]) { string s; cin>>s; cout<<toLowerCase(s)<<endl; int i; cout<<"Enter your gender :"<<flush; cin>>s; cout<<s<<endl; {if (i=='m' && 'male') cout<<"hello Mr."<<endl; else if(i=='f'&&'female') cout<<"Hello Ms."<<endl; } system("PAUSE"); return EXIT_SUCCESS; }
Last edited by Ancient Dragon; Dec 12th, 2008 at 9:40 pm. Reason: correct code tags
C++ Syntax (Toggle Plain Text)
string toLowerCase (string s) { string g=s; toLowerCase(g); return g; }
That's not how to convert a string to lower case. Use the tolower() macro in a loop and convert each character
C++ Syntax (Toggle Plain Text)
string toLowerCase (string s) { string g = s; for(int i = 0; i < g.length(); i++) g[i] = tolower[g[i]); return g; }
or you could do it this way in c++
C++ Syntax (Toggle Plain Text)
string toLowerCase (string s) { string g=s; transform(g.begin, g.end(), g.begin(), tolower); return g; }
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.
•
•
Join Date: Dec 2008
Posts: 17
Reputation:
Solved Threads: 1
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>
<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>
•
•
Join Date: Sep 2008
Posts: 53
Reputation:
Solved Threads: 9
C++ Syntax (Toggle Plain Text)
if( s=="m" || "male")
C++ Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: sprintf breaks memory?
- Next Thread: CAN bus, opencv and pcmcia
| Thread Tools | Search this Thread |
api array auto based beginner binary bitmap c++ c/c++ calculator challenge char class classes code coding compile console conversion count delay-loading delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption equation error file forms fstream function functions game garbage givemetehcodez graph gui hmenu homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node output parameter performance pointer primenumbersinrange problem program programming project python random read recursion reference rpg sockets string strings temperature template test text text-file tivoli tree url variable vector video win32 windows winsock wordfrequency wxwidgets






