Question on if/else statements

T

thetwistedpanda

I am taking a c++ class at msms . ( mississippi school for math and
science ) But anyways, we are doing a program in which, you enter a
number 0-6 , and it outputs a domino. what I am wondering is...

Somewhat of a newbie question..

Is there anyway to for say , if to prevent users from entering a 'c' ,
err character , and decimals ?
 
V

Victor Bazarov

thetwistedpanda said:
I am taking a c++ class at msms . ( mississippi school for math and
science ) But anyways, we are doing a program in which, you enter a
number 0-6 , and it outputs a domino. what I am wondering is...

Somewhat of a newbie question..

Is there anyway to for say , if to prevent users from entering a 'c' ,
err character , and decimals ?

There is no way in C++ to prevent them from pressing those keys, but
if they do, the way to work around it is to read a string (symbols)
and check for the correct contents and reject something that cannot
be processed (with a message).

Victor
 
D

David Hilsee

thetwistedpanda said:
I am taking a c++ class at msms . ( mississippi school for math and
science ) But anyways, we are doing a program in which, you enter a
number 0-6 , and it outputs a domino. what I am wondering is...

Somewhat of a newbie question..

Is there anyway to for say , if to prevent users from entering a 'c' ,
err character , and decimals ?

You may want to browse the FAQ (http://www.parashift.com/c++-faq-lite/).
Section 15 ("Input/output via <iostream> and <cstdio>") should help you out.
 
A

Anil Mamede

I am taking a c++ class at msms . ( mississippi school for math and
science ) But anyways, we are doing a program in which, you enter a
number 0-6 , and it outputs a domino. what I am wondering is...

Somewhat of a newbie question..

Is there anyway to for say , if to prevent users from entering a 'c' ,
err character , and decimals ?

maybe with scanf

char str[20] = "";
int result = scanf("%[abd-z]", str);

if(result == 0) {
/* Error */
} else {
/* ok */
}

Anil Mamede
 
M

Mabden

Anil Mamede said:
I am taking a c++ class at msms . ( mississippi school for math and
science ) But anyways, we are doing a program in which, you enter a
number 0-6 , and it outputs a domino. what I am wondering is...

Somewhat of a newbie question..

Is there anyway to for say , if to prevent users from entering a 'c' ,
err character , and decimals ?

maybe with scanf

char str[20] = "";
int result = scanf("%[abd-z]", str);

if(result == 0) {
/* Error */
} else {
/* ok */
}

I would probably use get() in a loop and discard any key that wasn't a 0-6.
Any other key should just be ignored, except whatever you use to terminate
the input (like 'x' for exit or something).

C / C++ greatest strength is its ability to read a character and decide to
do nothing with it - and to do it fast!
;-)
 
T

Thomas Matthews

thetwistedpanda said:
I am taking a c++ class at msms . ( mississippi school for math and
science ) But anyways, we are doing a program in which, you enter a
number 0-6 , and it outputs a domino. what I am wondering is...

Somewhat of a newbie question..

Is there anyway to for say , if to prevent users from entering a 'c' ,
err character , and decimals ?

There is no method to prevent the user from doing anything; at
least from the _standard_ C++ perspective.

You can input an integer and test for success. Inputting an
integer will fail if the user inputs a character, symbol or
decimals:
unsigned int value;
bool invalid_input(false);

do
{
cout << "Enter a value: ";
cout.flush(); // make sure the prompt is displayed.
if (!(cin >> value))
{
invalid_input = true;
}
else
{
if (value > 6)
{
invalid_input = true;
}
}
if (invalid_input)
{
cout << "\nInvalid input, try again.\n" << endl;
}
} while (invalid_input);

Validating the user input is always a good thing.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top