473,382 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

(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
Jul 19 '05 #1
5 2563
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
Jul 19 '05 #2
On Wed, 17 Sep 2003 02:29:25 GMT, da Vinci <bl***@blank.com> 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.

Jul 19 '05 #3
On Tue, 16 Sep 2003 22:37:43 -0400, Josh Sebastian <cu****@cox.net> 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'.

Jul 19 '05 #4
On Wed, 17 Sep 2003 03:02:18 GMT, al***@start.no (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.
Jul 19 '05 #5
da Vinci <bl***@blank.com> 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
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Adrienne | last post by:
I am the first to admit that I know bupkis about javascript, except that sometimes I need it to do something client side that I can't do server side. Anyway, here's my problem: <input...
7
by: ilona | last post by:
Hi all, I store phone numbers in the database as 123447775665554(input mask is used for input, and some numbers have extensions), and I also know from db if the number is Canadian, US, or some...
5
by: msammart | last post by:
Hey, i have a payroll system and i'm tyring to have it so the user can select an option from the menu and then be able to change one of the employee's salaries based on the user ID input. ( data is...
83
by: deppy_3 | last post by:
Hi.I am started learning Programm language C before some time.I am trying to make a programm about a very simple "sell shop".This programm hasn't got any compile problem but when i run it i face...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
5
by: shanti | last post by:
hai, i have developed a c-code to push integers into a stack (an array). in the program there are three inputs which has to be given through keyboard. the inputs are "choice", "option","S_R_NO"...
2
by: sorobor | last post by:
dear sir .. i am using cakephp freamwork ..By the way i m begener in php and javascript .. My probs r bellow I made a javascript calender ..there is a close button ..when i press close button...
7
by: gubbachchi | last post by:
Hi all, In my application I need to display the data fetched from mysql database after the user selects date from javascript calender. I have written the code in which after the user selects the...
2
by: Henry Stock | last post by:
I can understand what these error messages are telling me, but I guess I am not sure how to address them all. The bold tag could be handled in a class attribute, but I need a <br/tag inside the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.