(Help Please) Formatting input....

Discussion in 'C++' started by da Vinci, Sep 17, 2003.

  1. da Vinci

    da Vinci Guest

    Hello,

    I have a question regarding how to format input. I am at the beginners
    level.

    Basically, I am reading in a single character from the keyboard using
    cin and I want to ensure that it is a capital letter. I looked into
    using the formatting flag (uppercase) but the book I have specifies it
    is only for cout. example: cout.setf(ios::uppercasse) ;

    In the program I am writing now, I only have to deal with one
    character. So I just used an if statement......

    if ( another = 'n' )
    another = 'N' ;

    But what if there were options to have more than just one letter?
    Maybe the user could enter a, b, c, d, e, or f. And you want it to be
    put into a char variable in the uppercase format.

    How would you accomplish that?

    Thanks.

    DV
    da Vinci, Sep 17, 2003
    #1
    1. Advertising

  2. On Wed, 17 Sep 2003 02:29:25 +0000, da Vinci wrote:

    > Hello,
    >
    > I have a question regarding how to format input. I am at the beginners
    > level.
    >
    > Basically, I am reading in a single character from the keyboard using
    > cin and I want to ensure that it is a capital letter. I looked into
    > using the formatting flag (uppercase) but the book I have specifies it
    > is only for cout. example: cout.setf(ios::uppercasse) ;
    >
    > In the program I am writing now, I only have to deal with one
    > character. So I just used an if statement......
    >
    > if ( another = 'n' )
    > another = 'N' ;
    >
    > But what if there were options to have more than just one letter?
    > Maybe the user could enter a, b, c, d, e, or f. And you want it to be
    > put into a char variable in the uppercase format.
    >
    > How would you accomplish that?


    The std::toupper function (from <cctype>), takes a character and returns
    the uppercase equivalent. So, you can say

    another = std::toupper(another);

    If another is 'a', it will become 'A'. If it's 'b', it will become 'B'. If
    it's ';', it will remain ';'.

    Josh
    Josh Sebastian, Sep 17, 2003
    #2
    1. Advertising

  3. On Wed, 17 Sep 2003 02:29:25 GMT, da Vinci <> wrote:

    >Hello,
    >
    >I have a question regarding how to format input. I am at the beginners
    >level.
    >
    >Basically, I am reading in a single character from the keyboard using
    >cin and I want to ensure that it is a capital letter. I looked into
    >using the formatting flag (uppercase) but the book I have specifies it
    >is only for cout. example: cout.setf(ios::uppercasse) ;
    >
    >In the program I am writing now, I only have to deal with one
    >character. So I just used an if statement......
    >
    >if ( another = 'n' )


    Oops, the above is an assignment, not an equality comparision.

    > another = 'N' ;
    >
    >But what if there were options to have more than just one letter?
    >Maybe the user could enter a, b, c, d, e, or f. And you want it to be
    >put into a char variable in the uppercase format.
    >
    >How would you accomplish that?


    That can be very simple or extremely tricky, depending on what you
    want to support of international characters, and how.

    I'm not sure, but I think the following is the simplest:


    #include <locale>

    char toUpper( char c )
    {
    return std::toupper( c, std::locale() );
    }


    ...

    myChar = toUpper( myChar );


    The C library also has a function called 'toupper', which can cause
    some confusion.
    Alf P. Steinbach, Sep 17, 2003
    #3
  4. On Tue, 16 Sep 2003 22:37:43 -0400, Josh Sebastian <> wrote:

    >The std::toupper function (from <cctype>), takes a character and returns
    >the uppercase equivalent. So, you can say
    >
    > another = std::toupper(another);
    >
    >If another is 'a', it will become 'A'. If it's 'b', it will become 'B'. If
    >it's ';', it will remain ';'.


    The above usage can lead to unexpected results with international
    characters, because the C libary 'toupper' accepts an 'int' and
    returns an 'int'. Pass it a 'char', using a compiler that has 'char'
    as an unsigned type, and suprising things can happen. So either
    use explicit 'static_cast' for both the argument (to 'unsigned char')
    and the result (back to 'char'), or use the C++ library's 'toupper'.
    Alf P. Steinbach, Sep 17, 2003
    #4
  5. da Vinci

    da Vinci Guest

    On Wed, 17 Sep 2003 03:02:18 GMT, (Alf P. Steinbach)
    wrote:

    >>if ( another = 'n' )

    >
    >Oops, the above is an assignment, not an equality comparision.


    *HA! I typed that in wrong. I meant it to be a == though. :)

    > #include <locale>
    >
    > char toUpper( char c )
    > {
    > return std::toupper( c, std::locale() );
    > }

    ...
    > myChar = toUpper( myChar );


    *Excellent, thanks!

    It worked perfectly.
    da Vinci, Sep 19, 2003
    #5
  6. da Vinci <> writes:

    > Hello,
    >
    > I have a question regarding how to format input. I am at the beginners
    > level.
    >
    > Basically, I am reading in a single character from the keyboard using
    > cin and I want to ensure that it is a capital letter. I looked into
    > using the formatting flag (uppercase) but the book I have specifies it
    > is only for cout. example: cout.setf(ios::uppercasse) ;
    >
    > In the program I am writing now, I only have to deal with one
    > character. So I just used an if statement......
    >
    > if ( another = 'n' )
    > another = 'N' ;
    >
    > But what if there were options to have more than just one letter?
    > Maybe the user could enter a, b, c, d, e, or f. And you want it to be
    > put into a char variable in the uppercase format.
    >
    > How would you accomplish that?


    Use std::toupper:

    include <cctype>

    int main() {
    // read input ..
    another = std::toupper(another);
    // ...
    }

    HTH & kind regards
    frank

    --
    Frank Schmitt
    4SC AG phone: +49 89 700763-0
    e-mail: frankNO DOT SPAMschmitt AT 4sc DOT com
    Frank Schmitt, Sep 23, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brad
    Replies:
    1
    Views:
    306
  2. Replies:
    4
    Views:
    490
    Chris Uppal
    May 5, 2005
  3. KK
    Replies:
    2
    Views:
    514
    Big Brian
    Oct 14, 2003
  4. MuZZy
    Replies:
    7
    Views:
    1,716
    Mike Hewson
    Jan 7, 2005
  5. Nex_s
    Replies:
    8
    Views:
    93
    Paul Lalli
    Aug 3, 2005
Loading...

Share This Page