By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

Question on if/else statements

P: n/a
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 ?
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
thetwistedpanda wrote:
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
Jul 22 '05 #2

P: n/a
"thetwistedpanda" <th*************@yahoo.com> wrote in message
news:54**************************@posting.google.c om...
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.

--
David Hilsee
Jul 22 '05 #3

P: n/a
On Thu, 19 Aug 2004 16:17:26 -0700, thetwistedpanda wrote:
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


Jul 22 '05 #4

P: n/a
"Anil Mamede" <am**@mega.ist.utl.pt> wrote in message
news:pa***************************@mega.ist.utl.pt ...
On Thu, 19 Aug 2004 16:17:26 -0700, thetwistedpanda wrote:
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!
;-)

--
Mabden
Jul 22 '05 #5

P: n/a
thetwistedpanda wrote:
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

Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.