C++ Tutorial Online Programming Secrets

Spread the love

C++ Tutorial – Lesson 1 Online Programming

c++ tutorials can be best learned by …

  • Giving the lesson “Quality Time”
  • Review the online programming examples more than once
  • Practice – ( code from memory on paper, then code into your compiler/editor and check for errors )
  • Teach some other fool about c++ programming – no one available? Talk to the wall.

So let’s start learning a bit of c++ tutorial syntax with the basics

/* This is a comment block.
   You can put anything that you want here, 
   but we suggest something sensible like
   Programmer: Jane Doe
   Date : April 2020
   Purpose : So I don't have to do the dishes tonight
   Remember you can test this c++ tutorial online
   Copy, Paste, Test at http://cpp.sh 
*/

#include <iostream> // input - output library

int main(){
    cout << "Hello World";
}

Yes, the code above is simple enough. It should compile and run, right?.
Copy, Paste, Test at http://cpp.sh if you don’t yet have a C++ Editor/Compiler on your computer.

The output should be Crash! Ok, we lied ...

This doesn’t run at all.

It comes back with an unfriendly compiler error concerning something called cout

In function ‘int main()’: 4:5: error: ‘cout’ was not declared in this scope 4:5: note: suggested alternative: In file included from 1:0: /usr/include/c++/4.9/iostream:61:18: note: ‘std::cout’ extern ostream cout; /// Linked to standard output

We need to either define cout as std::cout or we need to make the library available like this below. “cout” ( console out ) is a command but the compiler can’t find it in the library because the specific library wasn’t mentioned like this std::cout.

/* This is a comment block.
   tell the compiler where to find the cout command
   mentioned below
*/

#include <iostream> // standard library

using namespace std; 
int main(){
    std::cout << "Hello World"; // or use std::cout
}

This will now compile correctly.

Lines 8 and 10 demonstrate the usage of “using namespace std” or “std::cout

In order to keep our samples looking simple, we will probably standardize on “using namespace std;” during the first 10+ lessons. But later, when you are very familiar with the c++ tutorial constructs and syntax, we will bring back in the std::cout and std::endl syntax. Your future employer will probably prefer that you use the std::cout format.

This below is the simplest C++ Code you can write ( not incl ‘using namespace std;’).

#include <iostream>

int main(){
    std::string howAmI = "Not Bad"; // Default value
    std::cout << "How are you ?\n";
    std::cin >> howAmI;
    std::cout << "\nFeeling " << howAmI;
}

Absorb this. Its not perfect but it is working code.

std::cout << "How are you ?\n"; // Sends this to the screen as output

And the code below uses cin >> to take input from you, the user

 std::cin >> howAmI; // Code pauses and waits for you to enter data

  • Memorize this.
    Write it down ( Pen & paper ).
  • Open cpp.sh, delete what is there, write this from memory
  • compile then go find someone to teach

P.S. if you enter a whole sentence as input only the first word will be output. Can you guess why?

c++ Tutorial and online programming tips presents an online c++ compiler from http://cpp.sh

Remember, your other compiler options are

  • Visual Code Studio ( Microsoft )
  • Code::Blocks ( Free C++ Compiler )
  • Sublime Text ( It will bug u for a paid donation and its great so one day you will give in, ha ha )

There are far more than just these 3. Google for c++ editor/compiler

How well do you know c++ already? Want to take a short quiz?

Welcome to your Lesson 1

What is "cin >> " used for
What does " std::cout << " handle ?

–> Lesson 2 C++ Int Variables

Back to the top of the c++ tutorial

Top