C++

Basic tutorial for C++

Why won’t it compile?

C++ is still the best programming language but any particular syntax doesn’t always compile. Code from older books are now deprecated. The basic structure of a program is a main function that doesn’t return a value and some code within brackets and any library includes before that. Code that used to compile like…

void main(){}

may not compile now because there is no return value and the word void is deprecated as it is redundant. Even…

void main(){return 0;}

won’t compiled because 0 is a value and not void. Even using null instead of 0 won’t work. So the correct syntax now is just…

main(){}

More simple certainly, but frustrating if you are working with old code. I hope that helps.

You could also use int main(){return 0;} but it is unnecessary unless you need to return a value. All of the notes above are true of the main function, but things are different with other functions. See the example below for a more complete program with just a main function and then we’ll talk about adding other functions.

A more complete program might be

#include <stdio.h>
#include <conio.h>
main ()
{
int a,b;
printf (“enter two numbers to add together\n”);
scanf(“%d%d”,&a,&b);//gets input
printf(“%d”,a+b);//displays sum
getch();//pauses for input before closing program
}

Now if we want to add an additional function to our program cpp is particular as to where it must be declared. It must be declared BEFORE the main function. Personally I hate this, but whatever. So main(){} addFunction(){} will not work, but addFunction(){} main(){} will. So we can clean up our program in this way…

#include <stdio.h>
#include <conio.h>

void addFunction()
{
int a,b;
printf (“enter two numbers to add together\n”);
scanf(“%d%d”,&a,&b);//gets input
printf(“%d”,a+b);//displays sum
}
main ()
{
addFunction();
getch();//pauses for input before closing program
}

You’ll notice in the addFunction we’re using the keyword void. Here the word void works, but it doesn’t work on the main function. This is why c++ compiling can be frustrating. It’s all over the place as to what is allowed or demanded and what is not.

Okay that works, but using printf is a little verbose. Can we use the more common cin and cout? Sure, but again we have to be careful about syntax. We can’t use iostream.h because it’s not a .h file and we’ll get a file does not exist type error. So we must exclude the .h and simply say #include <iostream> so that our program looks something like…

#include <iostream>

main(){

std::cout << “hi”;

}

The example above uses the more verbose stream type syntax. If we want to use a simple cout << “hi”; type syntax we have to use namespace std. So our program would have to be like this…

#include <iostream>

using namespace std;

main(){

cout << “hi”;

}

And that will work. CPP is a great programming language but its syntax is very particular. Some compilers will accept some things other will not. So keep that in mind and have fun. Check out my fully commented program to get an idea of how to build something efficient and stay tuned. More to come.

#include <stdio.h>
#include <conio.h>
#include <iostream> //not necessary if not using cout
using namespace std; //not ncessary if not using cout << a+b or you could use std::cout << a+b instead
void addFunction()
{
int a,b;
//printf (“enter two numbers to add together\n”);
//or more efficient…
cout <<“Enter two numbers to add together\n”;
scanf(“%d%d”,&a,&b);//gets input
//printf(“%d”,a+b);//displays sum
//or use this below which is more efficient but require the iostream and using namespace std lines
cout << a+b;
}

main ()
{

addFunction();
getch();//pauses for input before closing program

}