C++ Skeleton

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
rickyok
New poster
Posts: 8
Joined: Mon Jun 10, 2002 5:17 pm
Location: Jakarta, Indonesia

C++ Skeleton

Post by rickyok »

I usualy used this on BC 3.1 Windows

[cpp]
#include <iostream.h>

void main() {
int a;
cout << "Hello World!";
cin >> a;
}[/cpp]

But I try this at linux (because acm compile it at linux right?)
But it didn't work
Why?
Can some one give me the skeleton that I can used on linux
Thank you :D
arnsfelt
New poster
Posts: 44
Joined: Wed Oct 17, 2001 2:00 am
Location: Denmark
Contact:

Post by arnsfelt »

Your code is not ansi c++
Try this instead

[cpp]
#include <iostream>

using namespace std;

int main() {
int a;
cout << "Hello World!";
cin >> a;
return 0;
}
[/cpp]
junjieliang
Experienced poster
Posts: 169
Joined: Wed Oct 31, 2001 2:00 am
Location: Singapore

Post by junjieliang »

Can anyone tell me what the "using namespace std;" is for?
Ming Han
Learning poster
Posts: 77
Joined: Thu Jun 06, 2002 7:10 pm
Location: Singapore
Contact:

"using namespace std;"

Post by Ming Han »

I am also wondering what using namespace std; if for.
Can somebody please explain?

Thank You
Juergen Werner
New poster
Posts: 27
Joined: Wed Apr 17, 2002 7:54 pm

namespace

Post by Juergen Werner »

With the ANSI C++ standard, the concept of namespaces was introduced. Roughly speaking, global identifiers can belong to a certain namespace, so you can have different global functions/variables with the same identifier, but within different namespaces (this is rather needed when working on a project with several developers, for acm problems there's no need to define your own namespaces). When "using namespace MySpace", you can directly use the identifiers from MySpace, otherwise you'd have to address them like "MySpace::Variable1".

According to the ANSI C++ standard, all elements of the C++ Library are defined in the namespace std. So when using these function, you either got to refer to that namespace via "using namespace std" or put the namespace in front of each element you're using like "std::cout << std::endl;".

Some C++ Compilers are now refering to the namespace std on default (like rickyoks "BC 3.1 Windows") where you can just use the functions without doing "using namespace std" by yourself, with other compilers, you got do one of the above mentioned methods to address the C++ Library functions correcty (I guess that's the correct way according to the standard, but since most times you're using std anyway, there's no need to state that in some compilers explicitely).

Well, you should be able to read more about namespaces in any decent C++ reference.
rickyok
New poster
Posts: 8
Joined: Mon Jun 10, 2002 5:17 pm
Location: Jakarta, Indonesia

Post by rickyok »

I've tried that arnsfelt give. I compiled it with GCC on mandrake 8.2 But it still didn't work.... gee i wonder whyy..

And what is synonim on ANSI C with this code

gotoxy(int x , int y);
clrscr();

Thank you
Adrian Kuegel
Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany

Post by Adrian Kuegel »

gcc is for C only, not for C++. You have to use g++ to compile it.
zorbathut
New poster
Posts: 16
Joined: Tue Nov 05, 2002 6:31 am

Post by zorbathut »

C++ has no equivalents to gotoxy() or clrscr(). In fact, C doesn't either, technically, those functions are compiler-specific.

Now, as for compiler-specific, if you're on Linux check out the ncurses library. If you're going for Win32, start with looking up AllocConsole(). If you're on anything else, you might be able to use ANSI escape/color codes.
Post Reply

Return to “C++”