Ive began a book to learn c++ and in the first chapter I ran across my first question(thats never good...). In the book it says I should use this same template in every single c++
program I EVER make. The template is

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
//code
}

How am I supposed to remember that??? If you wouldnt mind could you explain what the
includes do,I thought it might be like import from python, and using namespace std, and finally

int main(int nNumberofArgs, char* pszArgs[].

Thanks a bunch.

Recommended Answers

All 4 Replies

Don't use the same template!!!
Especially using namespace std; And yes, you could use: int main(int argc, char* argc[]) But if you don't need arguments you can use: int main()

Wow thats a quick reply....3 minutes? Yikes. Anyway what do they do though. Whats an argument??Whats using namespace std mean?

>How am I supposed to remember that???
When you use it often enough, remembering becomes simple. In fact, if you program for long enough, you'll actually develop muscle memory for typing out your template and won't have to remember. ;)

>If you wouldnt mind could you explain what the includes do
The include directive is a glorified cut and paste. It copies the contents of a text file and replaces the directive with those contents. For example, let's say you have a file called mytest.txt:

This is a test

Now if you include that into your program using the include directive:

#include "mytest.txt"

int main()
{
}

The result will be as if you selected the include directive and copied the contents of mytest.txt over it:

This is a test

int main()
{
}

>Whats an argument??
An argument is the actual value you give to a function. For example, in "1 + 2", 1 and 2 are the arguments to the addition operation. The concept is the same with C++ functions.

>Whats using namespace std mean?
For now you can just treat it as boilerplate and not worry about it. But every name in the standard C++ library is stored within the std namespace, which basically means to use the name you have to qualify it with std:

#include <iostream> // For cout

int main()
{
  std::cout<<"Hello, world!\n";
}

Notice that cout is qualified with std and a think called the scope resolution operator (which you'll learn about later). This can be verbose and tedious, so there are two ways to qualify names without having to do it every time you use the name. The first is a using declaration, where you can say you want to qualify a single name and then use it unqualified from that point on:

#include <iostream>

using std::cout;

int main()
{
  cout<<"Hello, world!\n";
}

The second way (which isn't recommended) is a using directive, which takes an entire namespace and qualifies every name in it:

#include <iostream>

using namespace std;

int main()
{
  cout<<"Hello, world!\n";
}

It's not recommended because you're not likely to use all of the names in a namespace, and it increases the chances that one of the names you declare will conflict with a name in the namespace (because it's now effectively unqualified).

Thanks for the replies. I believe I get it now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.