how to catch bad input?

Hello everybody! This is my project for my fundamentals of programming class. I was wondering how I would be able to output "Invalid input entered" or something like that when the user enters a bad input like letters or symbols. I assumed that when it reads the letters, it takes the ASCII number of the first letter as the value and stores that in balance or checks but that's not what happens, it always outputs "The bank's service fees for this month are $25.00" no matter what letter or symbol I input. I tried using the trailing else to catch these bad inputs but it does not work. Here is the prompt to the project:

A bank charges $10 per month plus the following check fees for a commercial checking
account:
$0.10 each for fewer than 20 checks
$0.08 each for 20-39 checks
$0.06 each for 40-59 checks
$0.04 each for 60 or more checks
The bank also charges an extra $15 if the balance of the account falls below $400
(before any check fees are applied).
Write a program that asks for the beginning balance and the number of checks written.
Compute and display the bank's service fees for the month.
Input validation: Do not accept a negative value for the number of checks written.
If a negative value is given for the beginning balance, display an urgent message
indicating the account is overdrawn.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
 #include <iostream>
#include <iomanip>
using namespace std;

int main()
{
 //define a list of vars & input
 int checks;
 double balance, bankFees;
 
 cout << setprecision(2) << fixed << showpoint;
 
 cout << "Please enter the beginning balance of your account: $";
 cin >> balance;
 
 cout << "Please enter the number of checks written: $";
 cin >> checks;
 


 //processing
if (balance < 0)
{
	balance = false;
	cout << "Your account is overdrawn!\n";
}
else if (balance < 400)
{
 	balance = true;
 	bankFees = 15;
}
else if (balance >= 400)
{
	balance = true;
	bankFees = 0;
}
else
	cout << "Invalid input entered.";
	
	

if (checks >= 60 && balance)
{
	bankFees += checks * .04 + 10;
	cout << "The bank\'s service fees for this month are $" << bankFees;
}

else if (checks >= 40 && balance)
{
	bankFees += checks * .06 + 10;
	cout << "The bank\'s service fees for this month are $" << bankFees;
}

else if (checks >= 20 && balance)
{
	bankFees += checks * .08 + 10;
	cout << "The bank\'s service fees for this month are $" << bankFees;
}

else if (checks < 20 && checks >= 0 && balance)
{
	bankFees += checks * .10 + 10;
	cout << "The bank\'s service fees for this month are $" << bankFees;
}
else if (checks < 0)
	cout << "Number of checks entered has to be greater than 0.\n";
else
	cout << "Invalid input entered.";


 return 0;   

}
the general gist of it is to read the input as a string and then attempt to convert the string into the correct data type (there are built in ways to do this for atomic types like string to integer / double / etc). If it cannot be converted, you catch it and complain.

try this post for some ideas. I think the stringsteam is probably the cleanest way.

http://www.cplusplus.com/forum/general/13135/

give it a try, if you can't make it work we can show you more but I like to let folks try a few times first.
Last edited on
> I was wondering how I would be able to output "Invalid input entered" or something like that
> when the user enters a bad input like letters or symbols.

Check if the input was successful. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
if( std::cin >> balance ) // if the input of balance was successful
{
    // ...
    if( std::cin >> cheques && cheques >= 0 ) // if we successfully read an int, and the int is non-negative
    {
         // do computations
         // print result
         return 0 ; // we are done: normal program termination
    }
    else std::cout << "invalid input for number of cheques\n" ;
}

else std::cout << "invalid balance entered\n" ;
return 1 ; // we indicate that the program terminated because of an error by returning a non-zero value from main 
Last edited on
ok, so this is what I got and it looks like it works.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
 //define a list of vars & input
 int checks;
 double balance, bankFees;
 
 cout << setprecision(2) << fixed << showpoint;
 
 cout << "Please enter the beginning balance of your account: $";
 
if(cin >> balance)
 	if (balance < 0)
	{
	cout << "Your account is overdrawn!\n";
	return 0;
	}
	else if (balance < 400)
	{
 	bankFees = 15;
	}
	else 
	{
	bankFees = 0;
	}

else
{
	cout << "Invalid balance entered";
	return 0;
}
 
 cout << "Please enter the number of checks written: ";

 
if(cin >> checks) 
{
	if (checks >= 60)
	{
	bankFees += checks * .04 + 10;
	cout << "The bank\'s service fees for this month are $" << bankFees;
	}

	else if (checks >= 40)
	{
	bankFees += checks * .06 + 10;
	cout << "The bank\'s service fees for this month are $" << bankFees;
	}

	else if (checks >= 20)
	{
	bankFees += checks * .08 + 10;
	cout << "The bank\'s service fees for this month are $" << bankFees;
	}

	else if (checks < 20 && checks >= 0)
	{
	bankFees += checks * .10 + 10;
	cout << "The bank\'s service fees for this month are $" << bankFees;
	}
	else if (checks < 0)
	cout << "Number of checks entered has to be greater than 0.\n";

return 0;   

}	
else
{
 cout << "Invalid number of checks entered.";
 return 0;
}
}
Last edited on
Topic archived. No new replies allowed.