Add two numbers

This tutorial will read in two numbers from the console line and output the answer of the two integer values.

The cin refers to the console input, the >> allows the console input value placed into the variable (in this case the val1, val2), NOTE. << would mean output. With using the exception, and the try catch block of code allows for any errors in the conversion of the input into a integer value. Within the printf, there are a few conversion types, the %d means using the corresponding parameter within printf and output a integer value, %s means a string. When I say the corresponding parameter, if there are two %d within the output string, it will display in order the values attached to the printf(...) call. The code

#include <iostream>
#include <exception>
 
//using the namespace std (standard) for the cin -> console input
using namespace std;       
 
int main()
{
       // setup the defaul values.
       int val1 =0, val2 =0;
       try 
       {
              // output to the console
              printf("Please enter number 1 : ");
              // read in the input, if not a integer value, this will
              // cause a error 
              cin >> val1;       
              printf("Please enter number 2 : ");
              cin >> val2;
       }
       catch (exception& e)
       {
              // write out any error exception
              printf("Not a valid input %s\n", e.what());
       }
       // output the answer of the two inputted values.
       printf("Answer : %d\n", (val1 + val2));
       return 0;
}

save this as cppaddtwonumbers.cpp. Once compiled (g++ cppaddtwonumbers.cpp -o cppaddtwonumbers (the -o means to output to the filename else a a.out file name would be created))
Once executed the program, cppaddtwonumbers within Windows, or ./cppaddtwonumbers within Linux/Unix.

The output would be

Please enter number 1 : 23
Please enter number 2 : 41
Answer : 64

Read / Write files

This tutorial will demonstrate how to read in a file and also output the details to another file. The headers
fstream and iostream
basically include details on File-streaming (fstream) and In-Output-Streaming (iostream).

I have attached the code within a zip file with the required input file as well.

The namespace denotes which area are you in, for example : maths:algebra would mean you are doing maths and with the topic of Algebra, within the c/c++ example std::ifstream means that the namespace is std and the function/variable is ifstream.

NOTE: the { } are the begin and end of a code structure, e.g. While begin; do something; end;

#include 
#include
#include

using namespace std;// use the namespace (e.g. instead of putting std::ifstream you just need to put fstream.)

int main(void)
{
char InStr[100];
ifstream InFile("countrys.txt",ios::in);// infile
ofstream OutFile("outcountrys.txt", ios::out);// outfile

while (InFile.getline(InStr,100))// while reading each line within the file
{
printf("%s\n", InStr);// output the read line to the console
OutFile <<

Hello World C C++

You can obtain a few version of a c/c++ compiler from GNU, but if you are using Linux then you should have the compiler already installed.

To display the classic Hello World, save this code below

#include <iostream>
 
int main(void)
{
  std::cout << "Hello world!!";
}

as helloworld.c

Then to compile (if using gcc) into the platform that you are using

#Linux
g++ helloworld.c -o helloworld
#Windows because windows likes .exe
g++ helloworld.c -o helloworld.exe

to run the program, you just need to execute the helloworld file that was created from the above command, if you are using Windows, then you will need to change the line to

The output should be

Hello world!!

Well that is the classic!!