Pretty Please Help :(

I've created the following code in a .cpp file using Microsoft Visual Express C++ 2010 (taken from Herbert Schildts A Beginners guide 2nd Edn):

/*
This is a simple C++ program.
Call this file Sample.cpp.
*/
#include <iostream>
using namespace std;
//A C++ program begins at main().
int main()
cout << "C++ is power programming.";
return 0;


When i press the green arrow to debug, it comes up with a empty box labeled form, but the message C++ IS POWER PROGRAMMING DOESN'T APPEAR like the book says it should. How do i get this to work?
closed account (zb0S216C)
That code shouldn't even compile -- you're missing the braces for main().

kryssie wrote:
C++ IS POWER PROGRAMMING DOESN'T APPEAR like the book says it should.

I can think of 3 possible reasons why you never saw the output:

1) The code never even compiled (see the reason above).
2) The code compiled, but was never executed.
3) The window appeared, but closed too quickly -- preventing you from seeing the output.

Solution to 1)
Add the braces for main().

Solution to 2)
Press F5 (build then run).

Solution to 3)
Add this line before return in main():
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 0)

For this code to work, you need to #include <limits>.

Wazzak
Last edited on
If you are using MS 2010 Express, press ctrl+F5 instead of just F5. If you press F5, you will either need to use a system("PAUSE") before the return, or use the <limits> library, like Framework suggested.

But before all of that, you need the braces, left brace before cout, right brace after the the return 0.

Also, you may want to cout a "\n", which will end the line. Otherwise, the console will look like this-

C++ is power programming.Press any key to continue...

Good luck programming.
I've changed it to...

/*
This is a simple C++ program.
Call this file Sample.cpp.
*/
#include <iostream>
using namespace std;
//A C++ program begins at main().
int main()
{
cout << "C++ is power programming.";
return 0;
}

Now an error message comes up saying: Unable to start program "c:\documents....."
The system cannot find the specified file.


At the bottom it says...

Build: 1 succeeded etc etc.


Just a question, you make a project, and in the project you then open a new .cpp file? Does anyone know any good tutorials for basic stuff, this book doesn't seem to be helping!
The .cpp might not be part of the project. In other words, it will always compile nothing.

To add it, you need to see the solution explorer toolbar. To view/hide it, use ctrl+alt+l.

Open or start a new project, when the prompt comes up, name it, then choose Win32 Console application. When the wizard comes up, hit next, and then select empty project, under "Additional Options". Hit finish. Your project will be created.

The next step is to add the .cpp to the project. In the solution explorer toolbar, you will see all the files associated with your project. Right-click source files, then either add an existing item, or add a new item.

After that, you should be ready to write code. If you don't follow these steps, MS Express is nothing more than a text editor. The project will be stored under C:\Documents and Settings\Visual Studio 2010\Projects, unless you specify a different location at the start of the New Project Wizard.
thanks, this worked!

So do you always use the win32 console format?
Last edited on
The different formats are used for different things. win32 is the one to use for basic code writing and debugging.
For pausing after execution, I usually just do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// I always remember these, and prefer not to use any of the messy/bloated standard library stuff (especially STL)... so yes, I implement everything myself for the sake of driving myself even more insane (not really).

extern "C"
{
	void printf (const char*, ...);
	void puts (const char*);
	int getchar ();
};

// A main function is expected, which encapsulates the top-level 'commands' your program will invoke.
int main()
{
	puts("1+1 = 5."); // Let your nonsense be known. At least we're not pretending that C++ increases your popularity (that was another lame joke). Hello, World!
	getchar(); // Wait for enter key.
	return 0;
} // This example has a lot of unnecessarily annoying comments. By the way, that is a curly brace indeed! 


Note: This post is not helpful.
Last edited on
Topic archived. No new replies allowed.