Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,480 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,718 people online right now. Registration is fast and FREE... Join Now!




number validation check

 
Reply to this topicStart new topic

number validation check, trying to non numeric inputs in code

mrn97
5 Mar, 2008 - 08:42 PM
Post #1

New D.I.C Head
*

Joined: 5 Mar, 2008
Posts: 1

Im my current code it prompts the user to enter the amount of the item. If a char is entered it causes the program to loop, how do I get the program to check for non numeric inputs? here is my code I need to do a validation check for both the amount and for the store selection

cpp
 

float Tax[3]={7.25, 7.5, 7.75};
float input = 0.0f;
int choice = 0;

printf("\n\n Please enter the amount of the item: ");
scanf("%f", &input );

printf("\n Please select a Store: (1) Delmar, (2) Encintas, (3) La Jolla\n ? ");


while ((choice < 1) || (choice > 3)){

scanf("%i", &choice ); //use & for storing user input

if ((choice < 1) || (choice > 3))

printf ("\n Enter the number associated to the store: ");

} // end of while loop

printf("\n The sales tax is %f.\n" , input*(Tax[choice-1]/100)); //Tax calculation

return 0;

} // end of main

*edit: You were close with the tags, but you forgot the end code tag.

This post has been edited by Martyr2: 5 Mar, 2008 - 10:27 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Number Validation Check
5 Mar, 2008 - 10:33 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,212



Thanked: 215 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
When you read in data using scanf you can read it in instead as a character string and then use a great standard library function called strtol() which will evaluate the string to see if it is numeric and give you some options for testing the value. One great link to check out about how to use this function is here...

Bullet Proof Integer Input Using strtol()

That link will show you the details of using the function and its features, plus has an example for you.

Enjoy! smile.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 06:51PM

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