I have found out that goto is bad in C++? Why? I just got a book the other day (for my birthday :)) and it says it is fine to use.
Also the book doesn't use brackets ({}) these things around else (statement?), I can see why that is a bad habit though. Because the else statements can 'fall through' the code below it
ex:

else
cout<<"Next time please enter one of the fruits requested!"<<endl;
Answer:
      cout<<"You have selected "<<ftype<<" "<<fruit<<endl;
      cin.ignore();
      cin.get();

I have read stuff online about cin and cin.ignore and why you must use that at the end, but I dont really understand what it means when it says \n is built into it or something. Again, my book doesn't explain.
I could really use a good beginner's guide to C++ (mine is obviously junk :()
Thanks to everyone that reads and posts :). Good night :)

Recommended Answers

All 5 Replies

Read these google links about goto. The main reason its considered bad it because it makes the program very hard for others to read and understand. If you ever get a job writing programs then you will not be the only person reading the code you write. So you have to make the code as easy to understand as possible.

In the code you posted in that other thread the goto statements were not even needed.

The cin.get() you posted makes the program stop execution until you have a change to read whats on the screen and press some keyboard key. Similar affect as the "Press any key to continue" message you sometimes see.

cin.ignore() is usually used to ignore the next key in the keyboard buffer. You need that after entering numbers because the <Enter> key, which is identified as '\n', is left in the keyboard buffer.

int n;
cin >> n;
cin.ignore(); // discard the '\n' key

You only need a bracket {} after an if-statement if there is more than one afterward procedures(';').

gotos make code hard to read, they're often not optimizable by the compilers, and being hard to read with a glance: lead to broken code.

edit:
Once again, AD posted just enough seconds before me.

There are lots of different c++ coding standards that people use. No one agrees on everything, but almost everyone agrees that goto is unnecessary most of the time. Study other professional programmers code and pick the coding standard you like the best. And don't be afraid to change from time-to-time.

These are pretty good standards to follow:
http://www.possibility.com/Cpp/CppCodingStandard.html

but in reality, there isn't a standard, as you code more and more, you come up with your own style.

for example i have a uniqu comment style:

/*** This makes an int ***/
int x = 0;

coding has no set in stone standards, so feel free to experiment a little, but remember to keep code readable :D

I have found out that goto is bad in C++? Why? I just got a book the other day (for my birthday ) and it says it is fine to use.

Happy birthday! :) goto is too easy to use the wrong way. I think K&R calls it "infinitely abusable". goto is like multiple inheritance. Most of the time you don't need it, but when you do it makes all the difference in the world.

You only need a bracket {} after an if-statement if there is more than one afterward procedures(';').

A good compromise is always using brackets unless you can fit the whole if statement onto one line:

// fits on one line
// no brackets
if (done) break;

// kind of long for one line
// use brackets
if (option == 1)
{
    copy(
        myVector.begin(), 
        myVector.end(), 
        ostream_iterator<string>(cout, "\n"));
}

// two statements in the body
// brackets required
if (error)
{
    PrintError(error);
    break;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.