best way to print error and terminate

So I have written a math tutor program for first, second, and third graders. I used if/else statements for the whole thing and when i finally finish I read i need to add an appropriate error message and quit the program. Im wondering if there is a more intuitive way of doing this seeing as I have a bunch of if/else statements already and dont want to add another. Here is the beginning of my code, the only relevant part to this question.

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <cmath>
using namespace std;

string name;
int grade;
double a, b, c;
double userAnswer;
int main()
{
unsigned seed = time(0);
srand(seed);

cout <<"Welcome to the Math Tutor\n\n";

cout <<"Enter your name: ";
getline(cin, name);
cout <<"Enter your grade (1, 2, or 3): ";
cin >> grade;


cout <<"\nPlease answer the following problem "<< name <<".\n"<< endl;

if(grade == 1){
a = rand() % 10;
b = rand() % 10;
c = a + b;
cout <<"Problem: ";
cout << right << setw(5);
cout << a << endl;
cout << right << setw(13);
cout <<"+ " << b << endl;
cout << right << setw(14);
cout <<"----"<< endl;
cout << "Answer: ";
Etc. I have 2 other if statements for if grade is equal to 2 or 3. I need to put the error message after the cin >> grade; and terminate the program provided the input isn't one of the choices. Like I said I would just like to know if there is a more intuitive way to do those two things. Im a newbie to c++ and can't see it or havent been taught it yet. Thanks for any help.
Last edited on
Yes, there are a few more ways to do what you want.

But based on what you have, I would try to use an if-else statement.

Here's how it works:

1
2
3
4
5
6
7
8
9
10
11
12

if (//some condition here){
    //some code here.....
}


else{
    //If the the if-statement above is true and runs, then the else will not. 
//If the if-statement above is false and does not run, then the else will.
}




So in your case, I would try:

1
2
3
4
5
6
7
8
9
10
11
12

if (grade > 3)//If grade is greater than 3...
{
//output message here.
}

else{

//all the options for 1, 2, and 3 would go here.

}



It's not the best solution, but it works.

And this is sort of off-topic, but you should try not to use global variables so much. It can be a bad habit to develop.
Last edited on
I suppose I'll just stick with that then. Only reason I didnt want to do that was because I didnt want to bury 150 lines of code into that else. And using those globals were how we were taught to do this assignment. Im sure I will stray from them as I progress in class and gain experience.
I guess another problem I have with that assignment is what if they enter a zero, or anything else. You see why I am looking for a more intuitive way? Will that assignment cover all the bases? ultimately what I'm looking for. Thanks for the reply though
Sry about late reply:

What if they enter a zero? You can try something like this:

1
2
3
4
5
6
7
8
9
10
11
12

//This says if grade is greater than 3, OR if grade is less than 1, 
//then the if-statement will execute.
//Zero will trigger the if-statement. So will -1, -2, 4, 5, etc...
if (grade > 3 || grade < 1){
//code goes here...
}


else{
//...
}




Using if-else statements are generally very inefficient. So don't worry too much about sticking 150 lines into the else. You'll learn how to organize things more efficiently very soon.
If you are in your main() statement, you can normally end it by simply returning early. Alternately, you can use cstdlib's exit() or abort() functions.
For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <cstdlib>

// ...

if (grade > 3 || grade < 1) {
    // Error printing code here
    // If in main():
    return 1;       // Return an error code to the OS

        // Otherwise, you could do this:
    abort();        // Ends the program immediately, without cleaning data
        // OR, in less extreme cases:
    exit(1);        // Ends the program normally, as if
                    // you called return 1 in main.   

}

// Rest of code goes here 


EDIT:
Another option is to use std::terminate() in #incude <exception> , though from memory that just calls abort() anyway.
Last edited on
1
2
3
4
5
6
7
try {
        //some code here...
}

catch (/*Some exception name here...*/) {
        //Do something with the exception or terminate
}
Topic archived. No new replies allowed.