Why is goto the devil and exiting programs question.
Why should not we use "goto"? I am a programming newb and for the following task I would consider it practical.
Task:
1. Get user input
2. If the input is out of a desired range, end the program before doing something else
This example does NOT use goto but splits up these two choices into an if else structure.
Code:
//
#include "stdafx.h"
#include <iostream>
using namespace std;
int money;
int main() {
cout<<"Enter amount of cents (1-99): ";
cin>>money;
/* -----Exit Program if money out of range----- */
if (money < 1 || money > 99) {
//Print out error and quit program
cout<<"<ERROR> Enter a value between 0-99. "<<endl;
return 0;
}
/* -----Else continue----- */
else {
//Do something
return 0;
}
}
Would not using a goto to "hop" straight to the end more practical? Also what is the most elegant way to exit a program when used on a simply program like this?