| | |
capital letter
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2008
Posts: 17
Reputation:
Solved Threads: 1
i am trying to take input for name and return it with only the first letters of the name as capital....i am able to it the first name but m unable to move on to the second and last name.... please help..
C++ Syntax (Toggle Plain Text)
string ToUpper(string n) { for(int i=0;i<n.length();i++) { string m = n; for(int i = 1; i < m.length(); i++) m[i] = tolower(m[i]); if (n[i] >='a' && n[i] <='z') n[i]=n[i]-32; return m; return n; } }
Last edited by Narue; Dec 14th, 2008 at 11:12 am. Reason: fixed code tags
•
•
Join Date: Sep 2008
Posts: 90
Reputation:
Solved Threads: 12
This thread may help you:
http://www.daniweb.com/forums/thread161801.html
http://www.daniweb.com/forums/thread161801.html
•
•
Join Date: Sep 2008
Posts: 55
Reputation:
Solved Threads: 9
I'm supposing you're obtaining user input by using:
If you are, I suggest you read this thread. The problem with cin is it only takes input till a whitespace is found, so you're only using the first word.
C++ Syntax (Toggle Plain Text)
cin >> name;
If you are, I suggest you read this thread. The problem with cin is it only takes input till a whitespace is found, so you're only using the first word.
•
•
Join Date: Dec 2008
Posts: 17
Reputation:
Solved Threads: 1
hey thanks guys...with ur help i have got it... but i want to make a do while loop of it...can u tell me how to that...because if the name is having an integer or any other sign i have to output an error message and then again take input
C++ Syntax (Toggle Plain Text)
{ string n; string s; cout<<"Please enter your name :"<<flush; getline(cin, n); for(int i=1; i<n.length(); i++) { if (n[i]<='A'|| n[i]>='z' ||n[i]!=' ') cout<<"Error code 01: Name contains a character that is neither an alphabet nor a space"<<endl; } if(n[0]>='a' && n[0]<='z') n[0]-='a'-'A'; for(int x=1; x<n.length(); x++) { if(n[x]>='a' && n[x]<='z' && n[x-1]==' ') n[x]-='a'-'A'; else if(n[x]>='A' && n[x]<='Z') n[x]+='a'-'A'; } cout<<n<<endl;
Last edited by Narue; Dec 14th, 2008 at 11:12 am. Reason: fixed code tags
•
•
Join Date: Sep 2008
Posts: 55
Reputation:
Solved Threads: 9
Version with loop:
cpp Syntax (Toggle Plain Text)
bool Error = true; while(Error) { { string n; string s; cout<<"Please enter your name :"<<flush; getline(cin, n); for(int i=1; i<n.length(); i++) { if (n[i]<='A'|| n[i]>='z' ||n[i]!=' ') { Error = false; cout<<"Error code 01: Name contains a character that is neither an alphabet nor a space"<<endl; } } if(n[0]>='a' && n[0]<='z') n[0]-='a'-'A'; for(int x=1; x<n.length(); x++) { if(n[x]>='a' && n[x]<='z' && n[x-1]==' ') n[x]-='a'-'A'; else if(n[x]>='A' && n[x]<='Z') n[x]+='a'-'A'; } } cout<<n<<endl;
•
•
Join Date: Dec 2008
Posts: 17
Reputation:
Solved Threads: 1
hey..i have tries the while loop...but it doesnt works...can ne1 spot the prblm
C++ Syntax (Toggle Plain Text)
int main(int argc, char *argv[]) { string n; string s; { cout<<"Please enter your name :"<<flush; getline(cin, n); for(int i=1; i<n.length(); i++) while(n[i]<='A'|| n[i]>='z'); cout<<"Error code 01: Name contains a character that is neither an alphabet nor a space"<<endl; cout<<"Please enter your name :"<<flush; getline(cin, n); if(n[0]>='a' && n[0]<='z') n[0]-='a'-'A'; for(int x=1; x<n.length(); x++) { if(n[x]>='a' && n[x]<='z' && n[x-1]==' ') n[x]-='a'-'A'; else if(n[x]>='A' && n[x]<='Z') n[x]+='a'-'A'; } cout<<n<<endl; }
Last edited by Narue; Dec 14th, 2008 at 11:13 am. Reason: fixed code tags
•
•
Join Date: Sep 2008
Posts: 55
Reputation:
Solved Threads: 9
Sorry, I didn't test it before. This does work:
I changed/added a few things(Placed comments). Though I suggest you try structuring your code a bit better.
I'm not sure this is what you wanted, but it appears to be..
cpp Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; int main(int argc, char *argv[]) { string n; //Removed string s; it wasn't being used...? bool Error = true; //Main loop added while(Error) { Error = false; cout<<"Please enter your name: "<<flush; getline(cin, n); for(int i=0; i<n.length(); i++) //Checking for whitespace added, your version gave an error on space if(n[i] != ' ' && (n[i] <= 'A' || n[i] >= 'z')) { cout<<"Error code 01: Name contains a character that is neither an alphabet nor a space"<<endl; Error = true; break; } //Only convert if the input is correct if(!Error) { if(n[0]>='a' && n[0]<='z') n[0]-='a'-'A'; for(int x=1; x<n.length(); x++) { if(n[x]>='a' && n[x]<='z' && n[x-1]==' ') n[x]-='a'-'A'; else if(n[x]>='A' && n[x]<='Z') n[x]+='a'-'A'; } cout<<n<<endl; } } return 0; }
I changed/added a few things(Placed comments). Though I suggest you try structuring your code a bit better.
I'm not sure this is what you wanted, but it appears to be..
![]() |
Similar Threads
- Website Login (ASP.NET)
- Capitalizing first letter of word in an array? (C)
- Convert first character from lowercase to uppercase? (C)
- help in a very hard C question (C)
- float declaration problem (Java)
- Need help with Quiz code (Java)
- keybord problems (OS X)
Other Threads in the C++ Forum
- Previous Thread: Short pseudocode..Driving a car ...please correct
- Next Thread: Help with read file program to round numbers pt. 2
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





