c++ - I need a replacement for goto -


i have searched forums looking post can explain replacement goto on extremely basic level. reading book on cpp have been trying create project requires jumping around program. when ever use goto prints things weirdly, , when around on forums either advanced or not explained @ all.

#include <iostream> int main() //this example of i'm trying not actual program { using namespace std;     int a;     cout << "pick 1 or two.\n";     cin >> a;     if ( = 1 )         goto start1;     if ( = 2 )         goto start2;      start1:         cout << "words\n";      start2:         cout << "more words\n"; return 0; } 

it print: pick 1 or two. (pick 2) words more words

i've tried using functions , same thing. if explain replacement or send me link that'd great.

you can use switch way clearer solution:

#include <iostream> using namespace std;  int main() //this example of i'm trying not actual program {      int a;     cout << "pick 1 or two.\n";     cin >> a;     switch (a) {         case 1: cout << "words\n"; break;         case 2: cout << "more words\n"; break;     }     return 0; } 

Comments

Popular posts from this blog

How to access named pipes using JavaScript in Firefox add-on? -

multithreading - OPAL (Open Phone Abstraction Library) Transport not terminated when reattaching thread? -

node.js - req param returns an empty array -