I’m trying to write a program about tater tots, but my exception handling isn’t working correctly. Instead I get the following output:
output showing the if (DaysNumber < 7) output instead of the error message
I have the following code (this is my main file; I have a link to my GitHub repository for the other files at the bottom of this question if you’d like to see those files as well):
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <stdexcept>
#include "tatertot.h"
using namespace std;
int main() {
int DaysNumber;
// Asks the user how long it’s been since they ate tater tots
cout << "How long has it been since you had some yummy tots?" << endl;
cout << "Enter the number of days: " << endl;
cin >> DaysNumber;
`// Handles invalid inputs`
try {
// If it’s been a week (or more) since the user ate tater tots…
if (DaysNumber >= 7) {
cout << "Yay! I bet those tots were delicious. Here's a recipe to try next time you want to eat tater tots: " << endl;
cout << "https://www.allrecipes.com/recipe/236749/tater-tots-nachos/";
abort();
}
`// If the user ate tater tots recently…`
`else {`
`cout << "It. Is. TATER TOT TIME!" << endl;`
`cout << "Go enjoy some yummy tots!" << endl;`
`cout << "https://www.allrecipes.com/recipe/236749/tater-tots-nachos/";`
`abort();`
`}`
`}`
`catch (string e) {`
`cin.clear();`
`cout << "I'm sorry, but your input is invalid. Could you please try again?" << endl;`
`cin >> DaysNumber;`
`}`
`catch (double e) {`
`cin.clear();`
`cout << "I'm sorry, but your input is invalid. Could you please try again?" << endl;`
`cin >> DaysNumber;`
`}`
`return 0;`
};
I’ve tried using other methods of exception handling and defining DaysNumber in my class file and accessor-mutator file, but to no avail. Maybe there’s something I’m missing here?
Oh, and here’s the link to my GitHub repository: https://github.com/NEE64/potate-repository
NEE64 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.