Error Checking

I'm in my first semester of CS at school and we're starting off with C++. We've been doing projects and I've been able to puzzle my way through most of it. But error checking has been a little bit difficult.

I'm trying to accept only digits. If I enter something like 34#&fhqwhgads, it would accept the 34 and ignore the rest when outputting the final result. I wanted to make sure an error message is outputted if non-digits are entered in addition to digits.

Here's the code I came up with:
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
 /*
 Programming Challenge 12 - Software Sales
 Problem 12 on page 222 of Chapter 4 in the book "starting out with >>> C++: From Control Structures Through Objects" 8th ed.
 Written by Zepher Carnell
 Written for CS-1143
 Written on 09/21/2017
 Last eddited on 09/22/2017
 */
#include <iostream>
#include <iomanip>
#include <ctype>
using namespace std;

int main() {
    
int unitsSold;
double totalCost;
const double RETAIL_PRICE = 99;
const double DISCOUNT_50 = 0.50;
const double DISCOUNT_40 = 0.40;
const double DISCOUNT_30 = 0.30;
const double DISCOUNT_20 = 0.20;
    
    cout << "Please enter the number of units sold. " ;
    cin >> unitsSold;
    cout << unitsSold << " units sold at $" << RETAIL_PRICE << " each." << endl << endl;
    
    cout << setprecision(2) << fixed;
    if (isdigit(unitsSold)) {         // Would isdigit() work here?

    	if (unitsSold  >= 100) {
        	totalCost = ((RETAIL_PRICE * unitsSold) * DISCOUNT_50);
        	cout << "The total cost is $" << totalCost << " with a discount of 50% included." << endl;
    	} else if (unitsSold >= 50) {
        	totalCost = ((RETAIL_PRICE * unitsSold) * DISCOUNT_40);
        	cout << "The total cost is $" << totalCost << " with a discount of 40% included." << endl;
    	} else if (unitsSold >= 20) {
        	totalCost = ((RETAIL_PRICE * unitsSold) * DISCOUNT_30);
        	cout << "The total cost is $" << totalCost << " with a discount of 30% included." << endl;
    	} else if (unitsSold >= 10) {
        	totalCost = ((RETAIL_PRICE * unitsSold) * DISCOUNT_20);
        	cout << "The total cost is $" << totalCost << " with a discount of 20% included." << endl;
    	} else if (unitsSold >= 1) {
        	totalCost = (RETAIL_PRICE * unitsSold);
        	cout << "The total cost is $" << totalCost << "\nNot eligible for a discount with this purchase." << endl;
    	} else
        	cout << "Error: invalid input. \nPlease input an integer value that is greater than or equal to 1." << endl;
    
    return 0;
	}
}


and here's the code I received from my teacher after e-mailing him:

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
/*
 Programming Challenge 12 - Software Sales
 Problem 12 on page 222 of Chapter 4 in the book "starting out with >>> C++: From Control Structures Through Objects" 8th ed.
 Written by Zepher Carnell
 Written for CS-1143
 Written on 09/21/2017
 Last eddited on 09/22/2017
 */
#include <iostream>

#include <iomanip>
using namespace std;

int main()
{
    const double RETAIL_PRICE = 99;
    const double DISCOUNT_50 = 0.50;
    const double DISCOUNT_40 = 0.40;
    const double DISCOUNT_30 = 0.30;
    const double DISCOUNT_20 = 0.20;
    
    bool error = 0;
    int unitsSold = 0;
    double totalCost = 0;
    double discount = 0;
    double noDiscount = 0;

    cout << "Please enter the number of units sold. " ;
    cin >> unitsSold;

    noDiscount = (unitsSold * RETAIL_PRICE);
    
    if (unitsSold >= 100) {
        discount = ((RETAIL_PRICE * unitsSold) * DISCOUNT_50);
        totalCost = noDiscount - discount;
        
    } else if (unitsSold >= 50) {
        discount = ((RETAIL_PRICE * unitsSold) * DISCOUNT_40);
        totalCost = noDiscount - discount;
        
    } else if (unitsSold >= 20) {
        discount = ((RETAIL_PRICE * unitsSold) * DISCOUNT_30);
        totalCost = noDiscount - discount;
        
    } else if (unitsSold >= 10) {
        discount = ((RETAIL_PRICE * unitsSold) * DISCOUNT_20);
        totalCost = noDiscount - discount;
        
    } else if (unitsSold >= 1) {
        discount = 0;
        totalCost = (RETAIL_PRICE * unitsSold);
        
    } else {
        cout << "Error: invalid input. \nPlease input an integer value that is greater than or equal to 1." << endl;
        error = 1;
    }

    if (error == 0){
        cout << endl;
        cout << setprecision(2) << fixed;
        cout << left << setw(25) << "Regular Price: "  << "$"<< right << setw(10) << noDiscount << endl;
        cout << left << setw(25) << "Discount Amount: "  << "$"<< right << setw(10) << discount << endl;
        cout << left << setw(25) << "Total Amount: " << "$" << right << setw(10) << totalCost << endl;
        cout << endl;
    }
    
    return 0;
}


Thanks. :)
Something like this, perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cctype>

int main()
{
    std::cout << "enter an integer: " ;
    int n ;
    std::cin >> n ;

    char c ;
    // read the characters one by one (including white space characters),
    // till a new line or a non-white-space is read
    while( std::cin.get(c) && c != '\n' && std::isspace(c) ) ;

    // if the last character read is not a new line, it must have been a
    // non-white-space character which was not extracted as part of the number
    if( c != '\n' ) std::cout << "error: invalid character '" << c << "' in input\n" ;

    // if the last character read is a new line, all non-white-space characters
    // in the input were part of the number and were extracted into n
    else std::cout << "you entered the number " << n << '\n' ;
}


Another option is to read the input into a string and then parse the string to see if it contains only a number.
Topic archived. No new replies allowed.