Whats wrong here?

Started by
12 comments, last by Zahlman 18 years, 8 months ago
I dont know whats wrong with my first program. Its written in C++ and its my very first program but I cant seem to find out what the problem is. I put everything in exactly like it says. //first program #include <iostream.h> int main() { cout << "Hello, World!\n"; return0; } I also noticed most ppl usually somehow put that into a code box. How do I do that on these forums?
Advertisement
put in the line

using namespace std;

then look up about namespaces.


to do the code put it inbetween <source > < /source>

but replace '<' with brackets '['


IF that doesn't help, then tell us what the problem is. What errors etc..
Dude im sorry I dont mean to sound more new to this than I am but. What the heck does that mean? The second part I understand and I thank you for it. But the first part (which I assume is talking about my code)I have no clue whatsoever your talking about.
Well what errors are you getting?

Read the below and see if that helps.

//first program#include <iostream>  //don't put the .h at the end of the include fileusing namespace std; //some identifiers are 'inside' a namespace.                     //you must tell the compiler you are using that                      //namespace so those identifiers will be in scope.                     //cout is in the 'std' name space.                     //the 'std' namespace is where all the C++ standard                     //library identifiers are located.int main(){cout << "Hello, World!\n";return 0;            // you need a space after the return}
i get these


C:\Dev-Cpp\Makefile.win [Build Error] [main.o] Error 1 (Each
undeclared identifier is reported only once for each function it appears in.)

18 C:\Dev-Cpp\main.cpp `return0' undeclared (first use this function)

C:\Dev-Cpp\main.cpp In function `int main()':

7 C:\Dev-Cpp\main.cpp previous declaration `int main(int, char**)' here

16 C:\Dev-Cpp\main.cpp declaration of C function `int main()' conflicts with

C:\Dev-Cpp\main.cpp In function `int main()':
OK i was messing around with it a little bit and i have gotten rid of some of the errors now I have.




int main(0)
{
cout << "Hello, World!\n";
return0;
}



the first error is the problem (i think)

return0 is unknown... but
return 0
is known by the compiler

you forget the space !!! ;-)
You forgot a space between "return" and "0".
return0; // noreturn 0; // yes

Also, change
int main ()

to
int main (int argc, char *argv[])
First, get yourself a better tutorial. Whatever you're working from is at least six years out of date, which is an eternity in IT.

Second, fix the problem which indicated to me that the above was the case ;)

#include <iostream> // Proper C++ headers have no filename extension.using namespace std; // These headers put their "stuff" (functions and global// variables) into the 'std' "namespace", which prevents them from conflicting// with any stuff you might make with the same name. We don't want to make// anything called "cout", so we can use this line to tell the compiler "if// something doesn't appear to be defined, look in the std namespace for it".int main(){cout << "Hello, World!\n";return 0;}


Quote:Original post by Doom-Box
i get these


C:\Dev-Cpp\Makefile.win [Build Error] [main.o] Error 1 (Each
undeclared identifier is reported only once for each function it appears in.)

18 C:\Dev-Cpp\main.cpp `return0' undeclared (first use this function)


"return0" doesn't mean anything to the compiler. "return" does, and "0" does. There needs to be a space in there.

Quote:
C:\Dev-Cpp\main.cpp In function `int main()':

7 C:\Dev-Cpp\main.cpp previous declaration `int main(int, char**)' here

16 C:\Dev-Cpp\main.cpp declaration of C function `int main()' conflicts with

C:\Dev-Cpp\main.cpp In function `int main()':


You have gotten two versions of the main() function in your file somehow. Either version is correct (with no parameters, or with the int and char** parameters), but you can only have one.
i don't know how you are learning c, but if you want a good tut, here is a link (of course you can learn faster than written here !)

http://newdata.box.sk/bx/c/

How in this forum do you write links???? doesn't work...

This topic is closed to new replies.

Advertisement