Hi,
I have tried to understand try/catch/throw principle but right now i think i need someone to twist the iron wire with me.
Here is the code:
1 2 3 4 5 6 7 8 9 10 11 12
start:
cout << "How many informations you want to enter: ";
cin >> count;
try {
if (count > 1 || count < 10)
throw(count);
}
catch (int b){
cout << "Number " << b << " is too small.";
goto start;
}
With this i would like to prevent user to put number outside of the 1-10. This works for the first time right but when user put right number (exmpl. 5) it still gaves me the error message "Number is too small".
// First try
try {
if (count < 1)
throw(count);
}
catch (int b) {
cout << "Number " << b << " is too small.";
}
goto start;
// Second try
try {
if (count > 20)
throw(count);
}
catch (int b) {
cout << "Number " << b << " is too big.";
}
goto start;
I also tried this but not working.
1 2 3 4 5 6 7 8 9 10 11
try {
if (count < 1)
throw(count);
if (count > 20)
throw(count);
}
catch (int b) {
cout << "Number " << b << " is too small.";
}
goto start;
#include <iostream>
int main()
{
int count {};
start:
std::cout << "How many informations you want to enter (1 - 10): ";
std::cin >> count;
// First try
try {
if (count < 1)
throw(count);
}
catch (int b) {
std::cout << "Number " << b << " is too small.\n";
goto start;
}
// Second try
try {
if (count > 10)
throw(count);
}
catch (int b) {
std::cout << "Number " << b << " is too big.\n";
goto start;
}
std::cout << "Number OK\n";
}
Right, many blackouts here! Thank you for patience.
I did this with while loop and its working now. I just think is "good coding" use while with this kind of situation? In my code i have many places where I need this kind of check up. Should I put this while loop for everywhere I need?
Serial question: I want to make check up where user cant put number to cin when user needs to put letter. And vice versa.
#include <iostream>
#include <iomanip>
#include <string>
#include <limits>
int main()
{
int count{};
do
{
std::cout << "How many informations you want to enter: ";
std::cin >> count;
try
{
if (count < 1 || count > 10)
throw(count);
}
catch (int b)
{
std::cout << "\n Number " << b << " is too " << (b < 1 ? "small." : "large.") << "\n\n";
}
} while (count < 1 || count > 10);
// <--- Keeps console window open when running in debug mode on Visual Studio. Or a good way to pause the program.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
return 0; // <--- Not required, but makes a good break point.
}
In my code i have many places where I need this kind of check up.
IMO I wouldn't be using try/catch for this type of checking other than as an example. Try/throw is for catching run-time errors, not input validation. For this type of checking, I would use a simple loop.
You can do this with exceptions, but if you're catching and handling the exception at the same level as the try-catch, there's really no point to it at all.
So you've learned how to try, throw and catch. It was a nice learned exercise. You don''t need it for simple checking of input.
The useful property of exceptions is that when you throw them they KEEP going up through scopes until someone catches it, with objects destructing properly as the scopes are destroyed. If you instantly catch the exception at the same scope it is thrown, and deal with it, and the exception never continues upwards towards some higher-level catch in some function that was called five functions back in the call stack (for example) then you really don't need an exception.
#include <iostream>
#include <iomanip>
#include <string>
#include <limits>
void FailedCin(std::string msg);
void OutOfRange(int min, int max, std::string msg);
int main()
{
constexprint MIN{ 1 }, MAX{ 10 };
int count{};
do
{
std::cout << "How many informations you want to enter: ";
std::cin >> count;
if (!std::cin || count < MIN || count > MAX)
{
if (!std::cin)
{
FailedCin("\n Invalid Input! Must be a number\n\n");
}
elseif (count < MIN || count > MAX)
{
OutOfRange(MIN, MAX, "\n Value out of range! Range is ");
}
}
} while (count < MIN || count > MAX);
// <--- Keeps console window open when running in debug mode on Visual Studio. Or a good way to pause the program.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
return 0; // <--- Not required, but makes a good break point.
}
void FailedCin(std::string msg)
{
std::cout << msg;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
}
void OutOfRange(int min, int max, std::string msg)
{
std::cout << msg << min << " to " << max << "\n\n";
}
If you want to use try/catch, note that the try/catch argument can be any type. There are several pre-defined types (http://www.cplusplus.com/reference/exception/exception/) but you can create your own type (class etc) which does what you want and use this. Consider: