Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,395 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,127 people online right now. Registration is fast and FREE... Join Now!




Checking to make sure the input is NOT a character

 
Reply to this topicStart new topic

Checking to make sure the input is NOT a character, error-checking input

jeisma
27 Sep, 2006 - 12:22 PM
Post #1

New D.I.C Head
*

Joined: 14 Sep, 2006
Posts: 17


My Contributions
I want the user to input 2 values, and I'm unsure of what to use to make sure when the user enters a character rather than a numeric values, that it prompts the user to enter a numeric values.

CODE

int main()
{
  int choice;

  cout << "\nKU Standard to Metric System Conversion 1.0\n\n";
  cout << "What would you like to convert?\n";
  cout << "  1) Fahrenheit to Celsius\n";
  cout << "  2) Miles to Kilometers\n";
  cout << "  3) Pounds to Kilograms\n";
  cout << "  4) Gallons to Liters\n";
  cout << "  5) ~ End Program ~\n\n";
  cout << "Menu Choice: ";
  cin >> choice;

// It is at this point I want the input evaluated and made sure that it is between 0 - 5 and an integer.
  while (choice > 5 || choice < 0)
    {
      cout << "\nChoose a menu choice from above.\n";
      cout << "Menu Choice: ";
      cin >> choice;
      cout << "\n";
    }

  switch (choice)
    {
     case 1:
       fahr2cels();
       main();
       break;
     case 2:
       mi2km();
       break;
     case 3:
       lb2kg();
       break;
     case 4:
       gal2lit();
       break;
     case 5:
       break;
    }
}


This is only part of my code; however, this is the code that matters. I tried doing, in my while statement, "choice != int(choice)," and similar things. I just want a basic way of fixing this if available or at least insight on how I could easily do it. Thanks!

EDIT: Also, as of right now, when you enter anything other than a number, it repeats what is in the while loop infinitely.

This post has been edited by jeisma: 27 Sep, 2006 - 12:26 PM
User is offlineProfile CardPM
+Quote Post

tody4me
RE: Checking To Make Sure The Input Is NOT A Character
27 Sep, 2006 - 12:49 PM
Post #2

Only Jenny Craig makes thin clients...
Group Icon

Joined: 12 Apr, 2006
Posts: 1,279



Thanked: 3 times
Dream Kudos: 100
My Contributions
Try changing this
QUOTE

int choice;


to
CODE

char choice;


and change this
QUOTE

while (choice > 5 || choice < 0)


to
CODE

while (choice < 49 || choice > 53)


I haven't tested it on my box, since I don't have a compiler, but that should fix the issues. If you want to read multiple
characters, just change the char choice to char [x] choice with x being the number of characters that you want.

HTH
User is offlineProfile CardPM
+Quote Post

jeisma
RE: Checking To Make Sure The Input Is NOT A Character
27 Sep, 2006 - 01:04 PM
Post #3

New D.I.C Head
*

Joined: 14 Sep, 2006
Posts: 17


My Contributions
tody4me,

This didn't work? I can see where you're going but you're going to have to at least explain further because now when you enter a numbe 1-5, I'm not sure what it is, but something doesn't recognize it. Do I need to do a little ascii conversion somewhere?
User is offlineProfile CardPM
+Quote Post

chris.tkd
RE: Checking To Make Sure The Input Is NOT A Character
28 Sep, 2006 - 03:18 AM
Post #4

D.I.C Head
Group Icon

Joined: 26 Sep, 2006
Posts: 60


Dream Kudos: 25
My Contributions
you need to use the ascii values for this. http://www.lookuptables.com/

ie
CODE

do
{
    printf("please enter an int");
} while(input<49 || input>54);

i think thats whay your talkin about anyway. using a do loop will mean that it will ask for the input atleast once, if they give the correct input it will go to the next thing or if the input is incorrect it will ask again till they get it rite.
User is offlineProfile CardPM
+Quote Post

DeeViLiSh
RE: Checking To Make Sure The Input Is NOT A Character
28 Sep, 2006 - 09:47 AM
Post #5

D.I.C Head
Group Icon

Joined: 25 Jul, 2006
Posts: 175



Thanked: 1 times
Dream Kudos: 575
My Contributions
In other words, he wants you to make the input a character and make it work from there.

CODE
char str;

cout<<"Enter :"//Put a character
cin>>str;

if(str > 'A' && str < 'z')
cout<<"What are you doing? I need a number!!";

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 02:52AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month