# Noob question.. ,

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

## Recommended Posts

// Hello planet #include <iostream> using namespace std; int main () { cout << "Hello World!"; return 0; } Whenever I try run this, it fails.. Is something wrong with the coding?

##### Share on other sites
It appears to be working fine,

although you may wish to write this line of code instead:

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

this just ensures that Hello World! is printed on a line separate from your terminal/cmd call line.

If it's still not working, check out how you are compiling the program.

##### Share on other sites
I don't think it fails, it just does it so fast that it just opens up and shuts down.

you might want something like this:

// Hello planet

#include <iostream>
using namespace std;

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

system("PAUSE");

return 0;
}

though I suggest you avoid using system("Pause") when ur programming for real because it takes a lot of memory or something. cin.get() would probably work better.

##### Share on other sites
How do you "try to run it"?

How does "it fail"?

##### Share on other sites
Yea that works fine. If you do the whole std::cout and std::endl then you won't need the line of code "using namespace std;"

I wouldn't use system("PAUSE")though.

Try using cin instead or if in visual studio you can do "Start without Debugging" under Debug. That should work (Just make sure it builds okay though)

As for failing, what are the errors? Or what happens?

##### Share on other sites
What do you have against system("pause"); ?

##### Share on other sites
I believe its cuz system("PAUSE"); is not very effecient in terms of speed and memory speed.

http://www.gidnetwork.com/b-61.html

it's a link describing why you should avoid it and alternate methods such as cin.get()

##### Share on other sites
Hmm, I thought the code was correct.

After trying to run at the bottom it says

"========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped =========="

And nothing happens >,>

##### Share on other sites
Quote:
 Original post by Mr_FayceHmm, I thought the code was correct.After trying to run at the bottom it says"========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped =========="And nothing happens >,>
And what does it say above that? Why is it failing to build?

##### Share on other sites
Copy and pasted;

1>------ Build started: Project: fdsgafdg, Configuration: Debug Win32 ------
1>Compiling...
1>fdsgafdg.cpp
1>c:\users\mr_fayce\documents\visual studio 2008\projects\fdsgafdg\fdsgafdg\fdsgafdg.cpp(4) : warning C4627: '#include <iostream>': skipped when looking for precompiled header use
1>c:\users\mr_fayce\documents\visual studio 2008\projects\fdsgafdg\fdsgafdg\fdsgafdg.cpp(15) : fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?
1>Build log was saved at "file://c:\Users\Mr_Fayce\Documents\Visual Studio 2008\Projects\fdsgafdg\fdsgafdg\Debug\BuildLog.htm"
1>fdsgafdg - 1 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

##### Share on other sites
Turn off precompiled headers. The stadfx thing came up a few times when I was messing with sfml. I assume this is a windows console program? Anyway, turning off precompiled headers should work

##### Share on other sites
I haven't used Microsoft Visual cpp for a while, but I notice the compiler is complaining a lot about stdafx.h. I think there is an option to create a completely blank console project instead of using a precompiled header i.e. stdafx.h (I think it might be a checkbox in the new project dialogue). Try finding this then pasting your code into the new project and recompiling it.

Quote:
 Original post by Atrix256What do you have against system("pause"); ?

system("PAUSE"); will only work on Windows, so it isn't portable. Also, the command "pause" just runs a program that waits for input to continue, so there is a possibility that this program can be replaced with malicious code.

I don't think that is very likely but it's still better practice to use something more like:
#include <iostream>#include <string>using namespace std;int main (){cout << "Hello World!" << endl;//system("PAUSE"); Don't use thiscout << "Press enter to terminate the program" << endl;std::string myString;getline(cin, myString);return 0;}

That said, for beginners it's generally fine to use system("PAUSE"); on their own machines until they get the hang of other methods. I'm only a beginner too :)

Hopefully this helps, good luck!

P.S. It's a good idea for you to get into the habit of placing your code in source tags when posting it (see the forum faq). Folk around these parts get cranky if you don't :)

[Edited by - chrisparton1991 on December 4, 2009 7:05:57 PM]

##### Share on other sites
I have a problem with cin.get()... If i have a cin >> VARIABLE right before it, it skips it... I just use system("PAUSE"); for now, but im not doing any complex programs yet... But yeah it always skips te cin.get(); whenever I use it like its not there...

##### Share on other sites
How can a pause be horribly inefficient in speed? I never understood that.

##### Share on other sites
Quote:
 Original post by MAGIgullorksI have a problem with cin.get()... If i have a cin >> VARIABLE right before it, it skips it... I just use system("PAUSE"); for now, but im not doing any complex programs yet... But yeah it always skips te cin.get(); whenever I use it like its not there...

I think the reason it skips is because there is a return left in the stream. (The return you pressed earlier doesn't get removed automatically)
You should be able to remove the remaining return using cin.ignore(...), however using cin's operator >> is too low level when reading input from the users, you are better of using getline(cin, ...) instead. The getline function should take care of removing the remaining return as well.

##### Share on other sites
Quote:
 Original post by CzarKirkHow can a pause be horribly inefficient in speed? I never understood that.

lol

The way I see it using system("PAUSE") is syntactically sluggish. A bit like shooting sparrows with a cannon. It will schedule a new process and it is not portable.
If you'r fine with all that I see no reason not to use it.

##### Share on other sites
Don't pause your program artificially at the end, in any manner. Learn to run it properly, instead. Use a batch script, run it from the command line, set a breakpoint on the last line, whatever is appropriate.

A pause makes for sloppy design because it's an extra step the user has to deal with. It makes it harder to integrate your program with others in batch scripts and looks ugly when the user *is* running the program from the command line (because it's unnecessary and doesn't make sense in context). Of course, none of these things are likely to matter for games, but it's good to take the opportunity to learn good design early.

##### Share on other sites
Quote:
Original post by pulpfist
Quote:
 Original post by CzarKirkHow can a pause be horribly inefficient in speed? I never understood that.

lol

The way I see it using system("PAUSE") is syntactically sluggish. A bit like shooting sparrows with a cannon. It will schedule a new process and it is not portable.
If you'r fine with all that I see no reason not to use it.

The problem with system( "PAUSE" ) is that it is equivalent to opening a console and typing "pause" (try it!) But what if I, as an evil, malicious user created a file with something like
@format c: /quiet
called it "pause.bat" and placed it in the same folder as you're program?

Now, it is highly unlikely that this would actually happen, but it's a good idea to be defensive and there are better solutions to pausing (some of which has already been posted in this thread.)

##### Share on other sites
Quote:
Original post by Promethium
Quote:
Original post by pulpfist
Quote:
 Original post by CzarKirkHow can a pause be horribly inefficient in speed? I never understood that.

lol

The way I see it using system("PAUSE") is syntactically sluggish. A bit like shooting sparrows with a cannon. It will schedule a new process and it is not portable.
If you'r fine with all that I see no reason not to use it.

The problem with system( "PAUSE" ) is that it is equivalent to opening a console and typing "pause" (try it!) But what if I, as an evil, malicious user created a file with something like*** Source Snippet Removed ***called it "pause.bat" and placed it in the same folder as you're program?

Now, it is highly unlikely that this would actually happen, but it's a good idea to be defensive and there are better solutions to pausing (some of which has already been posted in this thread.)

drobole@fluxbox:~$pause-bash: pause: command not founddrobole@fluxbox:~$

It's lack of portability saved me(!)

You have a good point though.
Reminds me of the old 'ls' hack from the days when root's PATH still contained the current directory (.) on linux systems