Hi,

I am writing a student database in C++.
There comes a point in my program where I have a succession of "cout's" requesting the user to input the students grade for a particular course.
I want to check that the input is not less than 0 or greater than 100 (since the grade is a percentage).

I can do this using the following code:

#include <iostream>
using namespace std;

int main(){

double grade(0);
int a(0);

do{
		cout<<"Enter students grade: ";
		cin>>grade;
		if(grade<0 || grade>100){
		    a = -1;
		    cout<<"Sorry, the grade you entered was either < 0 or > 100 "<<endl;
		}
		else break;

	}
	while(a == -1);

	cout<<"Grade = "<<grade<<endl;

return 0;

}

My question is this:

I have been reading up on exception handling using the try, throw and catch keywords and this seems to be a situation where it would be very useful, since in my actual program there are lots of repeats of the above piece of code asking for the grade for lots of courses. My best guess at using try, throw and catch is the following:

#include <iostream>
using namespace std;

int main(){

double grade(0);

try{
    cout<<"Enter students grade: ";
		cin>>grade;
		if(grade<0 || grade>100){
		throw -1;
		}
		else cout<<"Grade = "<<grade<<endl;
   }

catch(int a){
   cerr<<"Sorry, you entered an incorrect value for 'grade'"<<endl;
}

return 0;

}

This code achieves the requirement of raising an exception if an incorrect grade is entered, however, I cannot see how using try, throw and catch, the user can be given the option to enter the grade again.

Any help would be greatly appreciated

Thanks,

Dave

Recommended Answers

All 2 Replies

You'd still have to put it in a loop:

#include <ios>
#include <iostream>
#include <limits>

using namespace std;

int main()
{
    bool done = false;
    int number;

    while (!done) {
        try {
            cout<<"Enter a number: ";
            if (!(cin>> number))
                throw -1;
            done = true;
        } catch (int) {
            cerr<<"Invalid input\n";
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }
    }

    cout<<"You entered "<< number <<'\n';
}

But exception handling is not recommended for "normal" error situations like invalid input. The reason is that the cost of throwing and handling an exception is very high compared to simply recognizing the problem and handling it without throwing an exception.

Hi Narue,

Thanks a lot for that.
Just a couple of questions:

1) Could you give an example of when it would be appropriate to use exception handling, i.e what kind of errors necessitate try, throw and catch.

2) What is the meaning of the line:

cin.ignore(numeric_limits<streamsize>::max(), '\n');

specifically,

numeric_limits<streamsize>::max()

Again, thank you for your reply,

Dave

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.