(Help Please) Formatting input....

D

da Vinci

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
 
J

Josh Sebastian

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
 
A

Alf P. Steinbach

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

Alf P. Steinbach

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

da Vinci

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

Frank Schmitt

da Vinci said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top