Problems with GoTo

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#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;
	}
}

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!

Can you tell me why shouldn't I use GoTo for a function like that?
Last edited on
more understandable
Debatable.

shorter
Marginally maybe, and irrelevant anyway.

maybe a bit more performing too!
Nope.

Can you tell me why shouldn't I use GoTo for a function like that?
Do you know for a fact that the function will stay like that forever? How much do I need to change it before erroneous and/or unintuitive behavior arises? In particular, how does goto behave when used close to objects with non-trivial destructors?

Personally, I have the policy that if a goto can be replaced with a different construct at no technical cost (if there's a technical cost it must be weighted it against competing concerns) then it must be replaced. This prevents goto from proliferating in codebases.

Is there any good reason not to rewrite that code like so?
1
2
3
4
5
6
7
8
9
10
int something;
while (true){
	cout << "Enter something: ";
	cin >> something;
	if (cin)
		break;
	cin.clear();
	cin.ignore(numeric_limits<streamsize>::max(), '\n');
	cout << "invalid answer!\n\n";
}
I can think of a few reasons to change it:
1. People don't immediately reject while loops, even if while (true) is often cause for pause.
2. This code behaves reasonably around common program-structure-sensitive constructs, such as RAII.
Last edited on
> But I haven't found any reason yet!

"I know that sometimes it makes you lose your own code logic"
Yes. Readability. That is the reason why jump statements other than return are viewed unfavourably.

ES.76: Avoid goto
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-goto

ES.77: Minimize the use of break and continue in loops https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-continue


As CoreGuidelines points out, often, these are good candidates for a function, where the jump statement becomes a return. For example:

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
#include <iostream>
#include <limits>
#include <string_view>

int get_int( std::string_view prompt )
{
    std::cout << prompt << ": " ;
    int n ;
    if( std::cin >> n ) return n ; // valid input; return it

    // input failure; inform user, clean up and try again
    std::cin.clear();
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
    std::cout << "invalid answer! try again.\n";
    return get_int(prompt) ;
}

int main()
{
    // with a get_int function, we can (should) make something const
    // if there is no intent to modify it later in the program
    const int something = get_int( "Enter something" ) ;

    std::cout << something << '\n' ;
}
Hi, Foxel.
At the end of this page about goto
https://en.cppreference.com/w/cpp/language/goto
I’ve found this link:
http://david.tribble.com/text/goto.html

I haven’t still read it entirely, but it looks like an exhaustive discussion about this topic.
(If you’re a beginner programmer, it could be too long and technical.)
@salem c

I'm new and I don't know the rules... Is it a problem if I cross-post to get an answer earlier?
@helios

then it must be replaced


is it valid using GoTo to break from a multiple cycle too?
It requires a temporary variable and I've always used GoTo in these cases up to now...
Last edited on
@Enoizat

yeah, it is a bit too technical for me right now... but I'm going to take a look anyway
Last edited on
@JLBorges

thank you... I didn't thought at the return as an alternative to GoTo

Problem Solved ;)
Last edited on
is it valid using GoTo to break from a multiple cycle too?
Only if for whatever reason it's not possible or practical to refactor that code into a function, as JLBorges suggested.
> Is it a problem if I cross-post to get an answer earlier?
http://www.catb.org/esr/faqs/smart-questions.html#forum
and
http://www.catb.org/esr/faqs/smart-questions.html#urgent

Then read the rest.
Topic archived. No new replies allowed.