Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 05-09-2005, 10:56 AM   #1 (permalink)
 
True Techie

Join Date: Dec 2004

Posts: 145

Mohan Giri

Default C++ Compiling Error

Hello everyone,
I have compiled my first c++ program in Windows environment. (MS VC++). But I am getting error for the simple program.

My program is:

#include<stdio.h>
main()
{
cout<<"Welcome to C++";
cout<<"All the best";
}


Error is

--------------------Configuration: first - Win32 Debug--------------------
Compiling...
hello1.cpp
e:\giri\c++\ex\first\hello1.cpp(4) : error C2065: 'cout' : undeclared identifier
e:\giri\c++\ex\first\hello1.cpp(4) : error C2297: '<<' : illegal, right operand has type 'char [10]'
e:\giri\c++\ex\first\hello1.cpp(5) : warning C4508: 'main' : function should return a value; 'void' return type assumed
Error executing cl.exe.

hello1.obj - 2 error(s), 1 warning(s)


Can anybody tell me what is the problem.
Mohan Giri is offline  
Old 05-09-2005, 11:18 AM   #2 (permalink)
 
Ultra Techie

Join Date: Sep 2003

Location: Bamberg, Germany

Posts: 549

Iron_Cross

Send a message via ICQ to Iron_Cross Send a message via MSN to Iron_Cross Send a message via Yahoo to Iron_Cross
Default

You need to include

Code:
#include <iostream>
using namespace std;

int main(void)
{
cout<<"Welcome to C++";
cout<<"All the best";
cout<< endl;

return 0;
}
There's several fixes I did there...

1.) I included iostream not stdio. << is part of the iostream header. It's also a part of the std namespace, so I included a using statment. Otherwise, every time I referenced cout I'd have to use std::cout it's much shorter to write the using statement.
2.) the main method now returns a value (int) and it tells the compiler that there are no arguments (void). It's mainly just good programming practice, it's not necesarry though.
3.) I included a call to endl which flushes the output buffer. Again, it's just good practice to flush the output buffer when you're done writing. That way you make sure all changes get made.
4.) I returned a value (of type int). I returned 0 which lets the operating system know that there was no errors during execution.

Hope that helps.

EDIT: also, if your book is teaching you put the .h at the end of your include statments, then chances are that your book is a bit outdated. It's not standard to do that anymore, you should leave it off.
__________________

See today\'s Penny-Arcade!(May contain foul lanuage)
Pain is weakness leaving the body.

PM Me for my MSN
Iron_Cross is offline  
Old 05-09-2005, 11:25 AM   #3 (permalink)
 
True Techie

Join Date: Dec 2004

Posts: 145

Mohan Giri

Default

I fixed the error. I have worked in C environment this long. So mistakenly I include <stdio.h> hearder file instead of <iostream.h>. Thanks for your try.
Mohan Giri is offline  
Old 05-09-2005, 04:43 PM   #4 (permalink)
 
Ultra Techie

Join Date: Sep 2003

Location: Bamberg, Germany

Posts: 549

Iron_Cross

Send a message via ICQ to Iron_Cross Send a message via MSN to Iron_Cross Send a message via Yahoo to Iron_Cross
Default

Please re-read my post.
__________________

See today\'s Penny-Arcade!(May contain foul lanuage)
Pain is weakness leaving the body.

PM Me for my MSN
Iron_Cross is offline  
Old 05-10-2005, 04:24 PM   #5 (permalink)
 
True Techie

Join Date: Dec 2004

Posts: 145

Mohan Giri

Default

Tell me why we are including "using namespace std" statement in our code. And if we use the code while specifying ".h" in iostream include statement getting error like

--------------------Configuration: fibonaci - Win32 Debug--------------------
Compiling...
fibonaci.cpp
e:\giri\c++\ex\fibonaci\fibonaci.cpp(2) : error C2871: 'std' : does not exist or is not a namespace
Error executing cl.exe.

fibonaci.obj - 1 error(s), 0 warning(s)

what is the reason?? Can you explain me??
Mohan Giri is offline  
Old 05-10-2005, 05:07 PM   #6 (permalink)
 
Ultra Techie

Join Date: Sep 2003

Location: Bamberg, Germany

Posts: 549

Iron_Cross

Send a message via ICQ to Iron_Cross Send a message via MSN to Iron_Cross Send a message via Yahoo to Iron_Cross
Default

What compiler are you using? You may be using an old compiler that doesn't conform to the new standards.
__________________

See today\'s Penny-Arcade!(May contain foul lanuage)
Pain is weakness leaving the body.

PM Me for my MSN
Iron_Cross is offline  
Old 05-10-2005, 09:40 PM   #7 (permalink)
 
True Techie

Join Date: Dec 2004

Posts: 145

Mohan Giri

Default

I am using MS VC++ 6.0
Mohan Giri is offline  
Old 05-10-2005, 09:48 PM   #8 (permalink)
 
Super Techie

Join Date: May 2005

Posts: 479

furtivefelon

Default

try GCC? it's the best C/C++ compiler around.. http://gcc.gnu.org/, i have no experience with microsoft VC++, so i can't say :|
__________________
lisp hacker
running: FreeBSD 5.4 - still learning
develop with: SBCL + emacs for lisp, Anjuta IDE +gcc for c, SPE for python..
browse with: opera
furtivefelon is offline  
Old 05-12-2005, 10:32 PM   #9 (permalink)
 
Ultra Techie

Join Date: Apr 2005

Posts: 949

M4A1 is on a distinguished road

Send a message via AIM to M4A1
Default

That's totally a homework assignment program.
M4A1 is offline  
Old 05-15-2005, 01:52 AM   #10 (permalink)
 
Super Techie

Join Date: Jan 2005

Posts: 295

gab00n

Default

No it's way too simple to be a homework assignment. If it is then shame on that teacher, how pathetic.
__________________
\"Today\'s scientists have substituted mathematics for experiments, and they wander off through equation after equation, and eventually build a structure which has no relation to reality.\" Nikola Tesla
gab00n is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On