(Help Please) Formatting input....

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • da Vinci

    (Help Please) Formatting input....

    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
  • Josh Sebastian

    #2
    Re: (Help Please) Formatting input....

    On Wed, 17 Sep 2003 02:29:25 +0000, da Vinci wrote:
    [color=blue]
    > 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?[/color]

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

    another = std::toupper(an other);

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

    Josh

    Comment

    • Alf P. Steinbach

      #3
      Re: (Help Please) Formatting input....

      On Wed, 17 Sep 2003 02:29:25 GMT, da Vinci <blank@blank.co m> wrote:
      [color=blue]
      >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' )[/color]

      Oops, the above is an assignment, not an equality comparision.
      [color=blue]
      > 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?[/color]

      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.

      Comment

      • Alf P. Steinbach

        #4
        Re: (Help Please) Formatting input....

        On Tue, 16 Sep 2003 22:37:43 -0400, Josh Sebastian <curien@cox.net > wrote:
        [color=blue]
        >The std::toupper function (from <cctype>), takes a character and returns
        >the uppercase equivalent. So, you can say
        >
        > another = std::toupper(an other);
        >
        >If another is 'a', it will become 'A'. If it's 'b', it will become 'B'. If
        >it's ';', it will remain ';'.[/color]

        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'.

        Comment

        • da Vinci

          #5
          Re: (Help Please) Formatting input....

          On Wed, 17 Sep 2003 03:02:18 GMT, alfps@start.no (Alf P. Steinbach)
          wrote:
          [color=blue][color=green]
          >>if ( another = 'n' )[/color]
          >
          >Oops, the above is an assignment, not an equality comparision.[/color]

          *HA! I typed that in wrong. I meant it to be a == though. :)
          [color=blue]
          > #include <locale>
          >
          > char toUpper( char c )
          > {
          > return std::toupper( c, std::locale() );
          > }[/color]
          ...[color=blue]
          > myChar = toUpper( myChar );[/color]

          *Excellent, thanks!

          It worked perfectly.

          Comment

          • Frank Schmitt

            #6
            Re: (Help Please) Formatting input....

            da Vinci <blank@blank.co m> writes:
            [color=blue]
            > 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?[/color]

            Use std::toupper:

            include <cctype>

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

            HTH & kind regards
            frank

            --
            Frank Schmitt
            4SC AG phone: +49 89 700763-0
            e-mail: frankNO DOT SPAMschmitt AT 4sc DOT com

            Comment

            Working...