• Advertisement
Sign in to follow this  

Normal C++ on VC 2005 EE

This topic is 4386 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Just starting and when reading older tutorials, to make a hello world program all that is needed in the code is: #include <iostream> using namespace std; void main(void) { cout << "Hello World!" <<endl; } ...To make a Hello World program, while in VS C++ 2005EE you have to add more code: #include "stdafx.h" #include <iostream> using namespace std; void main(void) { cout << "Hello World!" <<endl; cin.get(); // so the program doesnt close right away <_< } ... So I was wondering why is it different, why do i need to include stdafx.h now, and i have to add code so the program doesnt close right away? Is it because of .net? Because I want to program some stuff to put on my PSP, as sort of a hobby, and the PSP doesnt have a .net Framework, so... I don't want to have it using .net coding. And I rather like Visual C++ 2005 EE, and don't really want to use dev++. :/ So, as a beginner, how do i know if i'm using .net or not. The new tutorials never tell you what you are doing in terms of requirements on the user side. Going to sleep for now. ;)

Share this post


Link to post
Share on other sites
Advertisement
First off it's int main and not void main... although some compilers might (wrongly)allow void I am almost certain VS2005 does not.

To your question, VS2005 will compile the simpler "Hello World", You just need to create an empty project or turn off pre-compiled headers setting. By default VS will use precompiled headers which then require the extras you speak of.

Share this post


Link to post
Share on other sites
You can get away with this in VC++ EE:

#include <iostream>

int main()
{
std::cout<< "Hello World!" << std::endl;
return 0; // you might have to put this in, but really you shouldn't have to
}

Share this post


Link to post
Share on other sites
Quote:
Original post by DigiDude
You can get away with this in VC++ EE:

#include <iostream>

int main()
{
std::cout<< "Hello World!" << std::endl;
return 0; // you might have to put this in, but really you shouldn't have to
}


(emphasis mine)
It should really be the other way around. The program needs to return something to the OS. I guess its not required, but it is definately good practice.

As for the OP, just make sure to create a blank project (or simply turn off precompiled headers). Then your code will look exactly the same. Also, instead of just hitting F5 to run your code, use ctrl-F5 (Run w/out debugging), that will make sure the console doesn't close as soon as the app finishes.

Share this post


Link to post
Share on other sites
They way I've come to think of stdafx.h (and this could be completely wrong for all I know) is as a container for all of my global headers. All of the #include<> I slap in stdafx.cpp, and then I put my local #include"" in the files I'm using them in. I've also got into the habit of using:

system("PAUSE");

to stop the console from closing, as cin.get() can have unexpected results if you are using a cin call above it, also..

void main()
{
}

The main call just tells the compiler there will be no return from the main function. Apparently using int main(void) is considered bad practice(dont ask me!). While the stdafx thing is local to msvc, other C++ compilers have had the same issue with the console.

If you're concerned about compatibility with your PSP, there is a way to remove the stdafx stuff, If I had the link handy I'd provide it..but it was something I read in passing. It's a flag in the project settings if I remember correctly.

Share this post


Link to post
Share on other sites
Quote:
As far as main goes, void main() ( note the empty () ) is viable in C++ ee.
void main is non-standard. Just because your compiler of choice will let it slide does not mean other compilers or future versions will. It's like the if() scope issue of MSVC6. People would declare variables inside if blocks and use them outside in VC6 because, you know, it like, worked. Consequence: compiling in GCC or a more modern version of VC would give you compilation errors.

Quote:
I've also got into the habit of using:

system("PAUSE")

to stop the console from closing, as cin.get() can have unexpected results if you are using a cin call above it. While the stdafx thing is local to msvc, other C++ compilers have had the same issue with the console.
system("PAUSE") is great and all, but the truth is that it could do anything, even on different computers with the same version of the OS.

If you want to stop the console window from closing, either run from an external console, or "Run without debugging" (for MSVC).


jfl.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sr_Guapo
Quote:
Original post by DigiDude
You can get away with this in VC++ EE:

#include <iostream>

int main()
{
std::cout<< "Hello World!" << std::endl;
return 0; // you might have to put this in, but really you shouldn't have to
}


(emphasis mine)
It should really be the other way around. The program needs to return something to the OS. I guess its not required, but it is definately good practice


(second emphasis mine)Actually the first guy is correct. The standard allows an implicit
return 0;
from main if you don't put it at the end. ALL other functions should have one though, and you should probably put one at the end of main for consistancy.

Quote:
from msdn:
The return value of main represents a program's exit status. By convention, an exit status of zero indicates program success. With standard C++, a main function without an explicit return value means the compiler inserts a
return 0;
prior to the end of main.

Share this post


Link to post
Share on other sites
edited: Will not remove as I did post it...in retrospect it's irrelevant, if future posts quote, assume it to be correct.

Share this post


Link to post
Share on other sites
Hi everyone; I'm a beginner just like the OP and having similar problems and confusion with the VC 2005 Express IDE/compiler. I tried 3 approaches to making a console app for "Hello World": selecting a CLR console app, selecting a Win32 console app, and selecting a Win32 console app but removing the pre-prepared MS code and adding my own. Here are the results:


CLR Console Application: This works!

// CLR_Console_App1.cpp : main project file.

#include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
return 0;
}


_______________________________

Win 32 Console Appication: This doesn't work. I'm using pre-printed code supplied by the compiler and added the "Hello World" line.


// Win32_Console_App1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])

{
std::cout<< "Hello World!" << std::endl;
}

return 0;

________________________________

Win 32 Console App: (Removing the Microsoft code from the source file and adding what's below). This still doesn't work.

#include <iostream>
using namespace std;

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

______________________

If I've made some obvious errors, please bear with me. Just trying to get the compiler working initially with C++ code seems a monumental task at this point!

Dave FF



Share this post


Link to post
Share on other sites
You do have the PSDK properly installed right? After, create a console win32 C++ application, when the little dialog window comes up where you click submit/apply or whatever, to ocnfim the creation, click on settings on the left, then check the 'empty project' checkbox. After that, the solution and project will be created without any source files. You then add your main source file, say, 'main.cpp', by right clicking the source folder and clicking add new file. Then put in the code you'd like, i.e.:


#include <iostream>
using namespace std;

int main(int argc, char **argv)
{
cout << "Yep, it works." << endl;
return 0;
}




So that it doesn't close fast, compile and then 'Run without Debugging', adding a system call to command 'PAUSE' can create a lot of overhead (i.e. first contact the os, give it executation permission, let it do it's thing, then waiting for it to give it back to you, and then blah)

Hope that helps?

EDIT: I really doubt you have the PSDK installed (which lets you create native C++ applications), check out these instructions: http://msdn.microsoft.com/vstudio/express/visualc/usingpsdk/

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement