Hello mrtammy2008,
@mbozzi @ne555 , sorry I don't understand anything you guys just said, pretty new to try catch and variable type validation, would appreciate some pointers or solutions. |
The try/catch has it use like when you used it with the "stoi()" function. When you changed "input" to a numeric variable the try/catch can work, but I see it as overkill and more than you need.
Lets start with variables:
An
"int" will hold a number. This is a whole number only like 1, 10, 100, 25.
A
"double" will hole a floating point number and this can also be just a whole number like 10.25, 15.012, 2.0.
So when you define "input" as a "double" you are saying that "2.2" is an acceptable number. The you are using the try/catch to say that this is unacceptable input.
What you may not understand is that if you define "input" as an "int" and enter "2.2" it will store the "2", as a whole number, in the int and drop the ".2" because it is not an integer. This can be used to your advantage.
Using the try/catch can work, but I would consider leaving it until later. As it has been pointed out calling the function recursively and not using the returned value of the function is giving you a problem. This can be replaced using a do/while or while loop for most of the function and only returning a valid input.
As an alternative here is an example for you:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
#include <iostream>
#include <limits>
#include <string>
std::string flavour_function()
{
int input; // <--- Does not need to be a "double".
//char error = 'a'; // <--- Not needed.
std::cout << "Choose between 3 flavours\n";
std::cout << "[1] Strawberry\n";
std::cout << "[2] Chocolate\n";
std::cout << "[3] Vanilla\n";
std::cout << "[4] Exit\n";
std::cout << "Enter an option: "; // <--- Removed the '\n' and added ": ".
std::cin >> input; // <--- Will accept 2 or 2.2, but will fail if the first character is a letter.
while (!std::cin || (input < 0 || input > 4))
{
if (!std::cin)
{
std::cout << "\n==============================\n"; // <--- Added '\n' at the beginning of the string.
std::cout << "Error, numbers must be numeric\n";
std::cout << "==============================\n" << std::endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "Enter an option: "; // <--- Removed the '\n' and added ": ".
std::cin >> input;
std::cout << std::endl;
}
else
{
std::cout << "\n===============================\n"; // <--- Added '\n' at the beginning of the string.
std::cout << "Error, numbers not within range\n";
std::cout << "===============================\n" << std::endl;
std::cout << "Enter an option: "; // <--- Removed the '\n' and added ": ".
std::cin >> input;
std::cout << std::endl;
}
}
switch (input)
{
case 1:
return "Strawberry";
case 2:
return "Chocolate";
case 3:
return "Vanilla";
case 4:
return "";
}
}
int main()
{
std::string flavour;
do
{
flavour = flavour_function();
std::cout << flavour << '\n' << std::endl;
} while (flavour.length());
return 0;
}
|
I used a switch, but you can use the if statements that you started with just as easy. There are no "break" statements in the switch because the returns would prevent you from reaching the break statements. And there is no "default:" because the value of "input" would be 1 - 4 .
In "main" the while condition may appear a bit strange. Weather it is a while condition or the middle part of a for loop the expression is evaluated to either "0" (zero) false or something greater than zero which is considered true. So when the function returns an empty string to end the program "flavour.length()" evaluates to false (0) and the while condition ends. Anything else the string has a length greater than zero, so the do/while loop continues.
As an alternative I think this will do a better job than the try catch. And since it does not call the function recursively you eliminate that problem.
Give it a try. Any questions let me know.
Hope that helps,
Andy