I've coded in C++ since a few months ago and I often found people who recommended me not to use the much hated Goto...
But I haven't found any reason yet!
I know that sometimes it makes you lose your own code logic, but look at this:
Yes, I can get the same result with a do/while cycle, but I think this is more understandable, shorter and maybe a bit more performing too!Code:#include <iostream>// std::cin std::cout
#include <string>// getline() tolower()
#include <limits>// numeric_limits<streamsize>::max()
using namespace std;
void EnterNumber()
{
int something;
cin1:
cout << "Enter something: ";
cin >> something;
if (!cin)
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "invalid answer!\n\n";
goto cin1;
}
}
Can you tell me why shouldn't I use GoTo for a function like that?