How can I do incorporate this in the current code I already wrote: If the user enters a negative number or a non-digit number, throw and handle an appropriate exception and prompt the user to enter another set of numbers.
#include<iostream>
using namespace std;
int main()
{
//Declare Variables
int lengthInFeet, lengthInInches; // Length in feet and Length Inches
double lengthInCentimeters;
const double FEET = 30.48;
const double INCHES = 2.54;
cout << '\t' << '\t' << "This program Converts Length given in Feet and Inches into Centimeters;" << endl << endl;
cout << '\t' << '\t' << " Enter the length in feet and inches respectively seperated by a space: ";
cin >> lengthInFeet >> lengthInInches;
cout << endl << endl;
What have you tried so far? I would factor out your user input into its own function. In your main, have a while loop and a try-catch.
Aside from practicing exceptions and looking up a tutorial on them, one tricky thing is that when you enter a non-digit, your cin (input) stream will go into a bad state, and you have to reset it. To reset the stream into a good state, you do something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <limits>
// ...
int feet = -1;
int inches = -1;
while (true)
{
if (!(cin >> feet >> inches) || feet < 0 || inches < 0)
{
// reset state of stream
std::cin.clear();
std::cin::ignore(std::numeric_limits<std::streamsize>::max(), '\n')
}
else
{
break;
}
}
In your actual code, I would suggest asking and getting the input in its own function, and then throw from within that function.
#include<iostream>
usingnamespace std;
int main()
{
//Declare Variables
int lengthInFeet{0}, lengthInInches{0}; // Length in feet and Length Inches
double lengthInCentimeters;
constdouble FEET = 30.48;
constdouble INCHES = 2.54;
bool doAgain{true};
cout << '\t' << '\t' << "This program Converts Length given in Feet and Inches into Centimeters;" << endl << endl;
while( doAgain == true)
{
try
{
cout << '\t' << '\t' << " Enter the length in feet and inches respectively seperated by a space: ";
cin >> lengthInFeet >> lengthInInches;
if( (lengthInFeet < 0) or (lengthInInches < 0) ) // EDIT
throw(100);
else
doAgain = false;
}
catch(int error)
{
doAgain = true;
cout << "Error: " << error << '\n';
cin.clear();
cin.ignore(1000, '\n');
}
}
cout << endl << endl;
//Calculations
lengthInCentimeters = lengthInFeet*FEET + lengthInInches*INCHES;
cout << '\t' << '\t' << lengthInFeet << " feet (foot) and " << lengthInInches << " inch(es) is equivalent to ";
std::cout << lengthInCentimeters << " centimeter(s) " << endl << endl;
}
This program Converts Length given in Feet and Inches into Centimeters;
Enter the length in feet and inches respectively seperated by a space: 2 cvf
Error: 100
Enter the length in feet and inches respectively seperated by a space: cvf 2
Error: 100
Enter the length in feet and inches respectively seperated by a space: -1 2
Error: 100
Enter the length in feet and inches respectively seperated by a space: 2 -1
Error: 100
Enter the length in feet and inches respectively seperated by a space: 2 1
2 feet (foot) and 1 inch(es) is equivalent to 63.5 centimeter(s)
Program ended with exit code: 0
std::ios_base::badbit is set in the stream's exception mask to indicate an error that's generally not recoverable. https://eel.is/c++draft/ios.types#ios.iostate
Therefore it's an error to reset the state mask and try again forever.