dissagreement with compiler!

May 10, 2008 at 9:12am
i'm trying to learn to write programs. i have gotten borland c++ 5.02 to use as a compiler, and i figured that the "hello world" program was a good place to start. i printed it exactly as it did in the cplusplus.com tutorial, but for some reason, it kicks back a compiling error. it says "namespace name expected" after the word namespace
1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;

int main ()
{
	cout << "Hello World!";
   return 0;
}

what am i doing wrong?
Last edited on May 10, 2008 at 9:53am
May 10, 2008 at 12:26pm
what am i doing wrong?

the problem is you're using borland :)
try this: instead of having
1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;

int main ()
{
	cout << "Hello World!";
   return 0;
}

try using this instead, and tell me if it still gets an error.
1
2
3
4
5
6
7
#include <iostream>

int main ()
{
	std::cout << "Hello World!";
   return 0;
}

if that still spits out an error, try
1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;

int main ()
{
	std::cout << "Hello World!";
   return 0;
}

and if that fails, go out and get bloodshed dev c++
May 10, 2008 at 1:30pm
That compiler might not support namespaces by default. If none of the above work, try just not using the std namespace at all.

1
2
3
4
5
#include <iostream>

int main() {
    return cout << "Hello World!";
}


And I agree, use a different compiler :)
May 11, 2008 at 3:21am
any suggestions on which compiler to use?

btw, i the std::cout statement didn't work, it kicked back errors, bu i tried taking out the using namespace line all together, and i'm not sure if it worked or not. a dosbox pops up and it looks like code or execution text flashes accross the screen for a fraction of a second before the box disappears again. it might have worked, but just not stayed long enough for me to read it?
Last edited on May 11, 2008 at 3:30am
May 11, 2008 at 7:56pm
I just posted that question, and here is the solution...

1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

int main ()
{
cout << "Hello World!";
system ("pause");
return 0;

}


The underlined part is added to tell Windows to pause before closing the console window. Add that just before the "return" command, and you will be able to see the results. I think that will work for any program.

By the way, I use the Borland Turbo C++ myself, and have no problems yet. If the experienced programmers out there don't like it, tell me what free one to use.
May 11, 2008 at 8:51pm
That is the common answer, but it is like using a bulldozer to knock over a lawn-weed.

I've addressed this issue here before:
http://www.cplusplus.com/forum/general/489/
and here:
http://www.daniweb.com/forums/post510545-13.html

The greatest problem with using system("pause") is the massive security hole it puts in your program. All other problems being equal: it is a waste of time and energy.

Hope this helps.
May 11, 2008 at 11:53pm
I am having the same problem using gcc in Linux. The code is:

#include <iostream>

int main()
{
return cout << "Hello World!";
}

Error: 'cout' was not declared in this scope.

When I used namespaces, the error listing was longer than the program.

Any suggestions?
Thanks
May 12, 2008 at 12:10am
You're compiling for the wrong language. Fix your cout back to std::cout and compile with
g++ myprog.cpp

Good luck.
May 17, 2008 at 6:17am
duaos, i read one of the links and the code worked to 'pause' the dosbox till i pressed enter, as i expected it would, but what does each part of the code mean? (sry, i'm a sucker for mechanics and syntax...)
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
May 17, 2008 at 2:41pm
ignore
http://www.cplusplus.com/reference/iostream/istream/ignore.html

numeric limits
http://msdn.microsoft.com/en-us/library/c707ct0t(VS.80).aspx
(don't forget to click the link for the list of class members)

:-)
Topic archived. No new replies allowed.