Lesson 2 – C++ int variables

Spread the love

The int and double. There are many arcane tings’ about C++ programming that we are not going to get into immediately in order to keep things simple.

Those are for advanced developers. The aim is to quickly push you through so that you are programming as quickly as possible. Remember that you can copy and paste this code up at http://cpp.sh and test it immediately.

The online compiler is your friend.


/* We look at variables such as the c++ int
   Programmer: Jane Doe
   Cause : C++ Programming, int and cout
   Date : May 2020
*/

#include <iostream>

using namespace std;

int main(){
   int x, y, z;
   x = 3;
   y = 4;
   z = 5;
   cout << "Sending integers to console\n";
   cout << x << ", " << y << ", " << z;
}


\n is a newline break

The preceding code initializes x, y, z but gives them no values until the immediate lines when values are assigned.

std::cout can be thought of as console out – it prints to the command line.

The above code can also be written like this below


Alternate coding version

#include <iostream>
 
int main(){
   int x=3, y=4, z=5;
   std::cout << "Sending integers to console\n";
             << x 
             << ", " << y 
             << ", " << z;
}


We removed the using namespace std:

which means we had to change cout << to std::out which is more precise

In the c++ programming example above

console\n”; could have been replaced with

… console” << std::endl;

Try these changes on your own at …

Try this code at cpp.sh

Remember

  • Absorb it
  • Memorize it
  • Write it twice
  • Teach some fool

–> Lesson 3 C++ Int to Double Explicit Cast

Top