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.
/*
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>
usingnamespace std;
int main() {
int unitsSold;
double totalCost;
constdouble RETAIL_PRICE = 99;
constdouble DISCOUNT_50 = 0.50;
constdouble DISCOUNT_40 = 0.40;
constdouble DISCOUNT_30 = 0.30;
constdouble 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;
} elseif (unitsSold >= 50) {
totalCost = ((RETAIL_PRICE * unitsSold) * DISCOUNT_40);
cout << "The total cost is $" << totalCost << " with a discount of 40% included." << endl;
} elseif (unitsSold >= 20) {
totalCost = ((RETAIL_PRICE * unitsSold) * DISCOUNT_30);
cout << "The total cost is $" << totalCost << " with a discount of 30% included." << endl;
} elseif (unitsSold >= 10) {
totalCost = ((RETAIL_PRICE * unitsSold) * DISCOUNT_20);
cout << "The total cost is $" << totalCost << " with a discount of 20% included." << endl;
} elseif (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:
#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.