Thread: string to upper

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    10

    string to upper

    I must be staring at this for too long because Ive been able to do this simple loop in the past...not today. Im just trying to take a string including spaces or characters and convert it to uppercase letters. Where am I going wrong please?

    Code:
         cout <<"\nPlease enter your full name: ";
         getline(cin,ch);
         cout << endl;
    
    
         for (int x = 0; x < ch.length(); x++) 
         {            //takes each character and converts it to uppercase letters
         cout << (char)(toupper(ch.at(x)));
         }

  2. #2
    Registered User
    Join Date
    Oct 2012
    Posts
    12
    For a string, use the transform function to uppercase all letters, including those separated by spaces:

    Code:
    #include<iostream>
    #include<conio.h>
    #include<algorithm>
    #include<string>
    using namespace std;
    
    void main()
    {
    string ch;
    cout<<"\nPlease enter your full name: ";
    getline(cin,ch);
    cout<<endl;
    transform(ch.begin(), ch.end(),ch.begin(), ::toupper) //replacement function
    cout<<ch;
    getch();
    }

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    @confused i hope you see what is the problem with the code of hss

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trying to upper case a string
    By pliang in forum C++ Programming
    Replies: 2
    Last Post: 07-17-2005, 08:57 AM
  2. Coverting a string into upper or lowercase
    By TheShaggmeister in forum C++ Programming
    Replies: 13
    Last Post: 07-13-2003, 02:21 AM
  3. Convert a string to upper case?
    By jpp1cd in forum C Programming
    Replies: 2
    Last Post: 12-12-2002, 07:49 PM
  4. string upper case
    By Shadow12345 in forum C++ Programming
    Replies: 1
    Last Post: 05-06-2002, 08:23 PM
  5. convert a string to upper case
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 03-14-2002, 08:36 PM