Newbie - Validating Information

M

Morgan

I am new to Programming and need a little help.

I am trying to figure out how to validate information! I have the loop
writen but, when you enter a none integer number into the a varible
defined as an int the program does not finsh! Can any one please
help???? Here is my code:

# include <iostream>
# include <iomanip>

using namespace std;

int main()
{
double hours;
char choice;
char x = '"';


cout << "Please Select what Type of Subscription the Customer has? "
<< endl;
cout << "A: Package (A) = $9.95 per month for 10 hours and $2.00 per
hour over 10. " << endl;
cout << "B: Package (B) = $14.95 per month for 20 hours and $1.00 per
hour over 20. " << endl;
cout << "C: Package (C) = $19.95 per month for unlimited access. " <<
endl;
cout << "Please make a selection: ";
cin >> choice;
system ( "cls");


while ( choice != 'a' && choice != 'A' && choice != 'b' && choice !=
'B' && choice != 'c' && choice != 'C')
{
cout << x << choice << x << " is not a Valid Selcection " <<
endl ;
cout << "Please Try Again! " << endl;
cout << "Please Select what Type of Subscription the Customer has?
" << endl ;
cout << "A: Package (A) = $9.95 per month for 10 hours and $2.00
per hour over 10. " << endl;
cout << "B: Package (B) = $14.95 per month for 20 hours and $1.00
per hour over 20. " << endl;
cout << "C: Package (C) = $19.95 per month for unlimited access. "
<< endl;
cout << "Please make a selection: ";
cin >> choice;
system ( "cls");
}

switch (choice)
{
double monthly;
double hours2;
case 'a':
case 'A': cout << " How many hours did the customer use?:";
cin >> hours;
system ( "cls");

while (hours < 0 || hours < 744)
{
cout << x << hours << x << " is an invalid amount! " << endl;
cout << "Amount cannot be negative, or over 744 hours. " << endl;
cout << "Please re-enter correct amount. " << endl;
cout << "How many hours did the customer use?:";
cin >> hours;
system ( "cls");
}
if (hours <= 10)
{
cout << "The Customer should be billed: $" << "9.95" << endl;
}
else if (hours > 10)
{
hours2 = hours - 10 ;
monthly = 9.95 + (hours2 * 2.00);
cout << "The Customer should be billed: $" << monthly << endl;
}
break;
case 'b':
case 'B': cout << " How many hours did the customer use?";
cin >> hours;
system ( "cls");
while (hours < 0 || hours > 744)
{
cout << x << hours << x << " is an invalid amount! " << endl;
cout << "Amount must be a number, and cannot be negative, or over
744 hours. " << endl;
cout << "Please re-enter correct amount. " << endl;
cout << "How many hours did the customer use?:";
cin >> hours;
system ( "cls");
}
if (hours <= 20)
{
cout << "The Customer should be billed: $14.95" << endl;
}
else if (hours > 20)
{
hours2 = hours - 20 ;
monthly = 14.95 + (hours2 * 1.00);
cout << "The Customer should be billed: $" << monthly << endl;
}
break;
case 'c':
case 'C': cout << "The Customer should be billed: $19.95 " << endl;
break;
default: cout << "That is an Invalid Selection! " << endl;
cout << "Please select again! " << endl;
}
cin.ignore();
cout << "Press Enter to Close";
cin.get();

return 0;

}
 
H

Howard

Morgan said:
I am new to Programming and need a little help.

I am trying to figure out how to validate information! I have the loop
writen but, when you enter a none integer number into the a varible
defined as an int the program does not finsh! Can any one please
help???? Here is my code:

(I take it you mean a "non-integer value", not a "none integer number"?)

There are at least two ways to handle non-numeric data entered via cin. One
is to check the state of the stream, because it will be in an error state if
you enter bad data. The other way is to enter data into a string instead of
an integer, and parse the string yourself to determine if it's a valid
integer before continuing. (That's the way I'd do it.)

-Howard
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top