Thread: stuck with toupper

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    9

    Unhappy stuck with toupper

    I'm trying to get my program to convert only the first character of each word typed in to caps. here's what I have, could someone give me some clues/pointers on what I am doing wrong please?


    Code:
    #include <stdio.h>
    #include <iostream>
    using namespace std;
    
    int main (int argc, char *argv[], char **env)
    {
    	char c, lastc = ' ' ;
    	c = cin.get() ;
    	do {
    		
    		if ( isspace( lastc ) )
    
    		cout.put ( toupper( c ) ) ;
    
    		lastc = c ;
    		
    		c = cin.get() ;
    	} while ( !cin.eof()) ;
    }

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    You can start with including the correct header:
    Code:
    #include <cctype>

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    You're not handling every case. Try listing, in words, what should happen for each character.

    Soma

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    9
    knock yourself out

    Code:
    string StringToLower(string strToConvert)
    {//change each element of the string to lower case
        for(unsigned int i=0;i<strToConvert.length();i++)
        {
            strToConvert[i] = toupper(strToConvert[i]);
        }
        return strToConvert;//return the converted string
    }

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Way to be useful...

    o_o

    `StringToLower' == `toupper'?

    Soma

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    9
    Ok well you get the general idea, just change the string name -_-ll

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    And what the OP wants the program to do too?

    Soma

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    9
    Oh crap ok fine I am sorry I did not read the question correctly

    here is better version

    Code:
    string StringToWhatever(string strToConvert)
    {
        bool space = false; // to see if space was encountered
        for(unsigned int i=0;i<strToConvert.length();i++) // go through the whole string
        {
            if(space) // if space was encountered change the char to uppercase
            {
                strToConvert[i] = toupper(strToConvert[i]); // to uppercase
                space = false; // space is false
           }
            if(strToConvert[i].compare(" ")==0)
                space = true;
            
        }
        return strToConvert;//return the converted string
    }
    Sorry about that

  9. #9
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    >_<

    Okay. I get that you are learning. I get that you want to show off. However, you shouldn't just hand people an answer. Feel free to guide others, but let them experiment for themselves.

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. toupper() function
    By strokebow in forum C++ Programming
    Replies: 2
    Last Post: 09-15-2008, 10:54 AM
  2. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  3. Stuck on random generating
    By Vegtro in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2003, 07:37 PM
  4. stuck ky
    By JaWiB in forum Tech Board
    Replies: 2
    Last Post: 06-15-2003, 08:28 PM
  5. need example of toupper()
    By Waldo2k2 in forum C++ Programming
    Replies: 8
    Last Post: 06-29-2002, 10:07 AM