Connecting Tech Pros Worldwide Forums | Help | Site Map

Valid answer 1-10 only. All else gets error message.

jel
Guest
 
Posts: n/a
#1: Jul 22 '05
I ran what was submitted in several forums, but it's not exactly what
i'm looking for. I'm dy'n over here. Ah, the frustrations of an
amateur programmer. I included the code below in c++. which details
what i'm looking for. I'm sure its just a small detail. But I'm in the
dark.

// numbers1to10only.cpp

#include <iostream.h>

int main()
{
double x; // Variable for user input

do // I want it to loop until user <ctl+c> to quit.
{
cout << "\nWhich NUMBER 1-10 do you wish to display?\n";
cin >> x; // Get Input from user.
cout << "\nThe VALUE you entered was: " << x << ".";
if ((x < 1) || (x > 10))

/* I'm on my second bottle of asprin now. Still no effect.

I only want the numbers 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 to be
valid. No #'s containing decimals (before, middle, or after), no
letters of any kind, no special characters of any kind, etc...
**I just want the numbers 1-10 to be valid.** All else, I want
the error "below" to be displayed. ALSO, even after the user gives
what is asked (above), I'm looking to have it prompt again with
the same question above, (As it currently does above.)

I've tried several options from online forums but not exactly
what i'm looking for. Very close thought. If you run throught
my program as it is now, you'll see that #'s 1-10 (1.2, 9.0)
come up as valid answers. That's not what i'm looking for.
Also, if the user enters an (or several) alpha character, or
special character, the program does a scrolling thing with the
error below. I don't know how to make that stop. I just want
the error to display once, then prompt user with ? above. You'll
see what I mean if you run the program and put in various imputs.

Sorry about length of explination.*/

{
cout << "\nERROR, Ahhh!, You're trying to be funny.\n";
cout << "However, the NUMBER: " << x << " you selected is not
valid.\n";
cout << " It must be 1-10.\n";
cout << " Nice try though, but do try again.\n";
cout << "************************";
}
} while ((x > 1) || (x < 10));
return 0;
}

Thanks for any help you-all can provid.
JEL
Derek Baker
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Valid answer 1-10 only. All else gets error message.


"jel" <loisjeit2@hotmail.com> wrote in message
news:1993e8d6.0311181944.3faed888@posting.google.c om...[color=blue]
> I ran what was submitted in several forums, but it's not exactly what
> i'm looking for. I'm dy'n over here. Ah, the frustrations of an
> amateur programmer. I included the code below in c++. which details
> what i'm looking for. I'm sure its just a small detail. But I'm in the
> dark.
>
> // numbers1to10only.cpp
>
> #include <iostream.h>
>
> int main()
> {
> double x; // Variable for user input
>
> do // I want it to loop until user <ctl+c> to quit.
> {
> cout << "\nWhich NUMBER 1-10 do you wish to display?\n";
> cin >> x; // Get Input from user.
> cout << "\nThe VALUE you entered was: " << x << ".";
> if ((x < 1) || (x > 10))
>
> /* I'm on my second bottle of asprin now. Still no effect.
>
> I only want the numbers 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 to be
> valid. No #'s containing decimals (before, middle, or after), no
> letters of any kind, no special characters of any kind, etc...
> **I just want the numbers 1-10 to be valid.** All else, I want
> the error "below" to be displayed. ALSO, even after the user gives
> what is asked (above), I'm looking to have it prompt again with
> the same question above, (As it currently does above.)
>
> I've tried several options from online forums but not exactly
> what i'm looking for. Very close thought. If you run throught
> my program as it is now, you'll see that #'s 1-10 (1.2, 9.0)
> come up as valid answers. That's not what i'm looking for.
> Also, if the user enters an (or several) alpha character, or
> special character, the program does a scrolling thing with the
> error below. I don't know how to make that stop. I just want
> the error to display once, then prompt user with ? above. You'll
> see what I mean if you run the program and put in various imputs.
>
> Sorry about length of explination.*/
>
> {
> cout << "\nERROR, Ahhh!, You're trying to be funny.\n";
> cout << "However, the NUMBER: " << x << " you selected is not
> valid.\n";
> cout << " It must be 1-10.\n";
> cout << " Nice try though, but do try again.\n";
> cout << "************************";
> }
> } while ((x > 1) || (x < 10));
> return 0;
> }
>
> Thanks for any help you-all can provid.
> JEL[/color]

Look here for code that will reject non numbers:

http://www.parashift.com/c++-faq-lit....html#faq-15.2

Though the code there will convert floating point numbers to ints, and
accept them.

This would reject floating point numbers:

#include <iostream>

int main()
{
std::cout << "Enter a number, or -1 to quit: ";
float i = 0;
while (std::cin >> i) { // GOOD FORM
if (i == -1 || i != (int) i) break;
std::cout << "You entered " << i << '\n';
}

}

--
Derek


Closed Thread