need example of toupper()

This is a discussion on need example of toupper() within the C++ Programming forums, part of the General Programming Boards category; Here's how im using toupper(); cin>>whatever; whatever.toupper(); debug says it requires a struct/union type...so i'm guessing i can't do it ...

  1. #1
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001

    need example of toupper()

    Here's how im using toupper();
    cin>>whatever;
    whatever.toupper();
    debug says it requires a struct/union type...so i'm guessing i can't do it that way...
    first i tried this:
    cin>>whatever;
    whatever=toupper(whatever);
    then it complains about whatever not being an integer...
    can someone give me a working example of toupper to work from? thanks.
    PHP and XML
    Let's talk about SAX

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    79
    I am assuming whatever is a char type variable. To convert the character to upper case without causing any complications use type casting...

    #include<ctype.h>
    #include<iostream.h>
    #include<conio.h>
    int main()
    {
    char whatever;
    cin>>whatever;
    whatever=char(toupper(whatever));
    cout<<endl<<whatever;
    cout<<endl<<"Press any key to close this window...";
    getch();
    return 0;
    }

    Compiler:Borland C++ 5.0
    Last edited by sundeeptuteja; 06-28-2002 at 10:29 PM.

  3. #3
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Do you... Google?
    How about, MSDN?

  4. #4
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    heres what i did, essentially:
    Code:
    cin>>whatever;
    int whatever2=reinterpret_cast <int> (whatever);
    whatever2=toupper(partNumber2);
    //that works...
    but, i'm going to look for whatever2 inside another string: strstr();
    i couldn't just use whatever2 because now it's an integer variable...i need to get it back to a char[], and use it inside strster();, i tried a couple of casting methods, but none of them will work according to the compiler...what can i do?
    PHP and XML
    Let's talk about SAX

  5. #5
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    char cMyChar = 'a';
    cMyChar = toupper((int)cMyChar);

    Are you sure that won't work? It works fine for me, and has in the past on a variety of compilers... What compiler are you using?

    If all else fails... you could write your own toupper..

    Code:
    int ToUpper(int iArg)
    {
         if (iArg < 123 && iArg > 96)
         {
              iArg -= 32;
         }
         return iArg;
    }
    Last edited by Dual-Catfish; 06-28-2002 at 09:15 AM.

  6. #6
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    ok....im mad...my code just compiles and gets fatal errors when i run it...
    Code:
    char whatever;//is the problem here?
    cin>>whatever;//a string, possibly more than one letter, that's why I was suspicious of char whatever.
    whatever=toupper((int)whatever);
    //I read a string from a file into char *b
    strstr(b,(const char*)partNumber);
    //then i do some stuff like if the above returns null the string was not found...etc.
    whats going on?

    I use MS Visual C++ 6 Professional
    PHP and XML
    Let's talk about SAX

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    79
    Hi. When you say char whatever, you are declaring a char type variable. however the prototype of the toupper function is...
    int toupper(int);
    i.e. it returns an int value.

    If you want to assign the int value to a char variable, you must convert this int value to char, using type casting...
    whatever=char(toupper(whatever));

    This would do the same thing as...
    whatever=char(toupper(int(whatever)));
    The conversion of whatever to int takes place automatically.

    Also, when u say cin>>whatever, you are not taking an entire string as input, you are taking only one single character as input. In order to input a string you should declare a char array, and use gets. To convert it to upper case use strupr.

    #include<iostream.h>
    #include<conio.h>
    #include<stdio.h> //for gets
    #include<string.h> //for strupr
    int main()
    {
    char whatever[50];
    cout<<"Enter string:"; gets(whatever);
    cout<<"The string in upper case is:"<<strupr(whatever);
    cout<<endl<<"Press any key to close this window...";
    getch();
    return 0;
    }

    The toupper function works on characters only, and not strings.

    Compiler:Borland C++ 5.0
    Last edited by sundeeptuteja; 06-28-2002 at 10:49 PM.

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    559

    Example

    "Do you want to fish the lower river?"
    "No, I want to go toupper river."
    Truth is a malleable commodity - Dick Cheney

  9. #9
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    strstr(b,(const char*)partNumber);

    What's this doing? Nothing. strstr returns a character pointer, and you're not doing anything with it.

    You're finding the string partNumber in b, and assigning it to nothing. A more correct approach would be:

    char *pszStringFound = strstr(b,(const char*)partNumber);

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. Need help with toupper and tolower function
    By phoebus in forum C Programming
    Replies: 8
    Last Post: 04-27-2008, 10:18 PM
  3. toupper Function Problems!
    By Dilmerv in forum C++ Programming
    Replies: 6
    Last Post: 04-05-2006, 01:40 PM
  4. conio.h, toupper(), and a craps game
    By loki221 in forum C Programming
    Replies: 2
    Last Post: 11-28-2001, 06:40 PM
  5. implicit declatation of function 'int toupper(...)'
    By Intimd8r in forum C Programming
    Replies: 3
    Last Post: 10-01-2001, 02:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21