I havent programmed in C++ for about 3 years.. Since i started uni actually.. Now i gotta implement some stuff for Algorithms and Complexity.. I've got a vague idea of how to do that but thats not my problem.. According to my memory and old programs that i dug up for my hard disk the following code should not be wrong.. But it is.. At the moment im working on Windows XP with Microsoft Visual Studio 2008.. I've tried various versions of the std thingy like

std::cout << "sfsd";
using namespace std;
include "stdafx.h"

but nothing works.. Heres the first program i did on c++ which now isnt working :p

#include <iostream.h>
#include <conio.h>
#include "stdafx.h"

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

Recommended Answers

All 21 Replies

Try this:

#include <iostream>
using namespace std;

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

#include <iostream> to get the standard header. Then you need either using std::cout; at the top or qualify it in the body of your code with std::cout <<"Yada"; You can also go the using namespace std; route but that brings its own problems.

EDIT: Dang... JH, does someone start a stopwatch when we begin posting?

cout << "Hello World /n";

Actually this might give you a more 'expected' result:

cout << "Hello World \n";

/n is not the same as \n :)

Try this:

#include <iostream>
using namespace std;

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

I've tried what you said Jason and i get:

fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?

and when i do include that library i get the first error again

Actually this might give you a more 'expected' result:

cout << "Hello World \n";

/n is not the same as \n :)

Would that produce the cout error though?

#include <iostream> to get the standard header. Then you need either using std::cout; at the top or qualify it in the body of your code with std::cout <<"Yada"; You can also go the using namespace std; route but that brings its own problems.

EDIT: Dang... JH, does someone start a stopwatch when we begin posting?

Tried that as well nothing changed :(

Did you select a Win32 console application at the beginning? or an empty project? (cause the empty one really is devoid of a lot of settings that need to be in place so it's easiest to do the Win32 and weed the stuff out you don't need)

I started a win32 console (w/ precompiled headers)

#include "stdafx.h"
#include <iostream>

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

}

Also when creating a console-app in VS be sure to UNcheck "use precompiled headers"

Did you select a Win32 console application at the beginning? or an empty project? (cause the empty one really is devoid of a lot of settings that need to be in place so it's easiest to do the Win32 and weed the stuff out you don't need)

I started a win32 console (w/ precompiled headers)

#include "stdafx.h"
#include <iostream>

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

}

I did a new one using the Win32 console application just in case i did something wrong there.. I didnt delete anything just added the iostream library and the cout line.. Still the same error.. Sorry..
Heres the code:

#include "stdafx.h"
#include <iostream>


int _tmain(int argc, _TCHAR* argv[])
{
	cout << "Yada";
	return 0;
}

Also when creating a console-app in VS be sure to UNcheck "use precompiled headers"

I even unchecked it but still the same error with this code:

#include "stdafx.h"
#include <iostream>


int _tmain(int argc, _TCHAR* argv[])
{
	cout << "Yada";
	return 0;
}

But now you want to add the std:: to see if it will work in this context. I don't think the wide character main changed anything but try clearing it out for a regular main instead.

#include "stdafx.h"
#include <iostream>

void main()
{
	std::cout << "Yada";
	getchar();
}

Works now but i have to press enter to close the console window anything else just gets typed into the console window as well.

#include "stdafx.h"
#include <iostream>


int main()
{
	std::cout << "Yada";
	return 0;
}

Opens and closes the console window in like a second..

Anyway the main problem is fixed THANK YOU everybody!! xx

Wow, this thread went kinda crazy for a simple little thing didn't it!
Heh heh!

Actually this might give you a more 'expected' result:

cout << "Hello World \n";

/n is not the same as \n :)

Oops, I didn't spot that! I cut 'n pasted the OPs code and made some very minor adjustments..Very shoddy of me! :$

I've tried what you said Jason and i get:

fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?

and when i do include that library i get the first error again

Um.. Yes, sorry, when I edited your code in my post I assumed that the inclusion of stdafx was one of your failed attempts at getting your hello to work, I didn't realise that you were actually using precompiled headers in your program (for something as simple as a hello world it seems a bit overkill!). So yes, in that case if your program is set up to use precompiled headers then stdafx should be included!

However, as niek_e said:

Also when creating a console-app in VS be sure to UNcheck "use precompiled headers"

And Jonsca, regarding the std namespace thing...Yes I used 'using namespace std'. It's certainly not going to cause a problem with a one line "hello world" program. But you are right, I could have put a 'using std::cout' instead or explicitly specified std::cout in main itself, both are acceptable alternatives to opening up the entire std:: namespace!

Anyways, apologies for my trivial cut/paste errors...Normal service will be resumed shortly...Heh heh! ;)

Cheers for now,
Jas.

Yup its funny how sometimes errors are due to little stuff like that.. Anyway thanks again guys!!

#include <iostream>using namespace std; int main(){	cout << "Hello World /n";	return 0;}
#include <iostream>
using namespace std;

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

I am very sorry but why have you said my code is un organized?

I am very sorry but why have you said my code is un organized?

For the third time: Read the rules
Under heading "Keep It Organized" :

For easy readability, always wrap programming code within posts in (code) (code blocks) and (icode) (inline code) tags.

Which is, what I was referring to.

I am sorry. I have used codetags!

I am sorry. I have used codetags!

But not in post #16. I added them.

But enough of this, this thread is going off-topic. If you want to discuss this further, feel free to PM me.

Sorry I forgot. Please take of the Infraction and -1

make sure "using namespace std;" is spelled correctly along with all other directory

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.