capital letter

Please support our C++ advertiser: Intel Parallel Studio Home
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

capital letter

 
0
  #1
Dec 13th, 2008
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..
  1. string ToUpper(string n)
  2. {
  3. for(int i=0;i<n.length();i++)
  4.  
  5.  
  6. {
  7.  
  8. string m = n;
  9. for(int i = 1; i < m.length(); i++)
  10. m[i] = tolower(m[i]);
  11.  
  12. if (n[i] >='a' && n[i] <='z')
  13.  
  14. n[i]=n[i]-32;
  15.  
  16.  
  17.  
  18. return m;
  19. return n;
  20. }
  21.  
  22. }
Last edited by Narue; Dec 14th, 2008 at 11:12 am. Reason: fixed code tags
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 15
Reputation: ninwa is an unknown quantity at this point 
Solved Threads: 2
ninwa ninwa is offline Offline
Newbie Poster

Re: capital letter

 
0
  #2
Dec 13th, 2008
Here's my solution: http://codepad.org/UlW4iy19

Cheers,
ninwa
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 90
Reputation: unbeatable0 is an unknown quantity at this point 
Solved Threads: 12
unbeatable0 unbeatable0 is offline Offline
Junior Poster in Training

Re: capital letter

 
0
  #3
Dec 13th, 2008
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 55
Reputation: BeyondTheEye is an unknown quantity at this point 
Solved Threads: 9
BeyondTheEye BeyondTheEye is offline Offline
Junior Poster in Training

Re: capital letter

 
0
  #4
Dec 13th, 2008
I'm supposing you're obtaining user input by using:
  1. 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.
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: capital letter

 
0
  #5
Dec 14th, 2008
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
  1. {
  2. string n;
  3. string s;
  4.  
  5.  
  6. cout<<"Please enter your name :"<<flush;
  7. getline(cin, n);
  8. for(int i=1; i<n.length(); i++)
  9. { if (n[i]<='A'|| n[i]>='z' ||n[i]!=' ')
  10. cout<<"Error code 01: Name contains a character that is neither an alphabet nor a space"<<endl;
  11.  
  12. } if(n[0]>='a' && n[0]<='z') n[0]-='a'-'A';
  13.  
  14.  
  15. for(int x=1; x<n.length(); x++)
  16. {
  17. if(n[x]>='a' && n[x]<='z' && n[x-1]==' ') n[x]-='a'-'A';
  18. else if(n[x]>='A' && n[x]<='Z') n[x]+='a'-'A';
  19. }
  20.  
  21. cout<<n<<endl;
Last edited by Narue; Dec 14th, 2008 at 11:12 am. Reason: fixed code tags
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 55
Reputation: BeyondTheEye is an unknown quantity at this point 
Solved Threads: 9
BeyondTheEye BeyondTheEye is offline Offline
Junior Poster in Training

Re: capital letter

 
0
  #6
Dec 14th, 2008
Version with loop:

  1. bool Error = true;
  2. while(Error)
  3. {
  4. {
  5. string n;
  6. string s;
  7.  
  8.  
  9. cout<<"Please enter your name :"<<flush;
  10. getline(cin, n);
  11. for(int i=1; i<n.length(); i++)
  12. { if (n[i]<='A'|| n[i]>='z' ||n[i]!=' ')
  13. {
  14. Error = false;
  15. cout<<"Error code 01: Name contains a character that is neither an alphabet nor a space"<<endl;
  16. }
  17.  
  18. } if(n[0]>='a' && n[0]<='z') n[0]-='a'-'A';
  19.  
  20.  
  21. for(int x=1; x<n.length(); x++)
  22. {
  23. if(n[x]>='a' && n[x]<='z' && n[x-1]==' ') n[x]-='a'-'A';
  24. else if(n[x]>='A' && n[x]<='Z') n[x]+='a'-'A';
  25. }
  26. }
  27.  
  28. cout<<n<<endl;
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: capital letter

 
0
  #7
Dec 14th, 2008
hey..i have tries the while loop...but it doesnt works...can ne1 spot the prblm
  1. int main(int argc, char *argv[])
  2. {
  3. string n;
  4. string s;
  5.  
  6. {
  7.  
  8. cout<<"Please enter your name :"<<flush;
  9. getline(cin, n);
  10.  
  11. for(int i=1; i<n.length(); i++)
  12. while(n[i]<='A'|| n[i]>='z');
  13.  
  14. cout<<"Error code 01: Name contains a character that is neither an alphabet nor a space"<<endl;
  15. cout<<"Please enter your name :"<<flush;
  16. getline(cin, n);
  17. if(n[0]>='a' && n[0]<='z') n[0]-='a'-'A';
  18.  
  19.  
  20. for(int x=1; x<n.length(); x++)
  21. {
  22. if(n[x]>='a' && n[x]<='z' && n[x-1]==' ') n[x]-='a'-'A';
  23. else if(n[x]>='A' && n[x]<='Z') n[x]+='a'-'A';
  24. }
  25.  
  26. cout<<n<<endl;
  27.  
  28. }
Last edited by Narue; Dec 14th, 2008 at 11:13 am. Reason: fixed code tags
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 55
Reputation: BeyondTheEye is an unknown quantity at this point 
Solved Threads: 9
BeyondTheEye BeyondTheEye is offline Offline
Junior Poster in Training

Re: capital letter

 
0
  #8
Dec 14th, 2008
Sorry, I didn't test it before. This does work:

  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8. string n;
  9. //Removed string s; it wasn't being used...?
  10. bool Error = true;
  11.  
  12. //Main loop added
  13. while(Error)
  14. {
  15. Error = false;
  16. cout<<"Please enter your name: "<<flush;
  17. getline(cin, n);
  18.  
  19. for(int i=0; i<n.length(); i++)
  20. //Checking for whitespace added, your version gave an error on space
  21. if(n[i] != ' ' && (n[i] <= 'A' || n[i] >= 'z'))
  22. {
  23. cout<<"Error code 01: Name contains a character that is neither an alphabet nor a space"<<endl;
  24. Error = true;
  25. break;
  26. }
  27.  
  28. //Only convert if the input is correct
  29. if(!Error)
  30. {
  31. if(n[0]>='a' && n[0]<='z')
  32. n[0]-='a'-'A';
  33.  
  34. for(int x=1; x<n.length(); x++)
  35. {
  36. if(n[x]>='a' && n[x]<='z' && n[x-1]==' ') n[x]-='a'-'A';
  37. else if(n[x]>='A' && n[x]<='Z') n[x]+='a'-'A';
  38. }
  39.  
  40. cout<<n<<endl;
  41. }
  42. }
  43.  
  44. return 0;
  45. }

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..
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC