Learning C++ - Is this good to begin with?

  • Thread starter Thread starter Boy12
  • Start date Start date
  • Views Views 7,693
  • Replies Replies 63
Code:
#include <iostream> // contains cout and cin
#include <string> // contains string
#include <conio.h> // contains getch
 
using namespace std; // let's use not need to type std:: before all standard libarary stuff
 
int main( int argc, char* argv[] ) { // the standard console main looks like this
    string myInput = "";
    while ( myInput != "1" ) {
        cout << "Please enter something:";
        cin >> myInput; // the program will pause here and wait for input until it gets a return-key press then it will continue
        if ( myInput == "1" ) {
            cout << "You typed the number 1, congratulations, now the program will close after you press ANY key on your keyboard which is connecter to the computer you are using right now.";
            getch(); // just take a keyboard input and then continue with the program.
        }
        else {
            cout << "Wrong input! You could try typing 1 instead for great success! ;)\r\n";
        }
    }
    return 0;
}
here's some easy hello world-ish code for you.
 
If you want to start learning with c++ do so, a lot of the concepts you will learn apply to most other programming languages as well.
Goto isn't inefficient, its just terrible for most applications. Most of the time when you feel the need to use goto you need to review the entire code structure to see what led you to that conclusion and have probably missed something major.
 
  • Like
Reactions: Relys
If you want to start learning with c++ do so, a lot of the concepts you will learn apply to most other programming languages as well.
Goto isn't inefficient, its just terrible for most applications. Most of the time when you feel the need to use goto you need to review the entire code structure to see what led you to that conclusion and have probably missed something major.


We were taught gotos were the statements of the devil at my University. lol XD
 
We were taught gotos were the statements of the devil at my University. lol XD
goto can be awful when used improperly, but there are also circumstances where they make life easier. The Linux kernel, for example, uses goto for error handling. goto is also known to be used for Finite State Machines. Need to break out of nested loops? goto.

You do have to think very carefully before you decide to use a goto, because they can quickly spaghettify your code, but if you know what you're doing, then you can make code that's cleaner and simpler than without.
 

Site & Scene News

Popular threads in this forum