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

J

jel

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
 
D

Derek Baker

jel said:
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

Look here for code that will reject non numbers:

http://www.parashift.com/c++-faq-lite/input-output.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';
}

}
 

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
474,147
Messages
2,570,833
Members
47,378
Latest member
BlakeLig

Latest Threads

Top