Skip to main content
GameDev.net gamedev.net
🔒 Locked

Problem with program running.

Started by rsyerigan Mar 31, 2007 at 8:17 PM 14 replies 1.4k views
Original Post
rsyerigan
rsyerigan
This is what I have. //my first program in C++ #include using namespace std; int main () { cout << "Hello World!"; return 0; } Sorry i dont know how to put it in program format on the forums...feel free to fill me in =] But the "black box" just pops up then instantly goes away... anyone know why?
rsyerigan
rsyerigan
I'm using Dev C++ 4.9.9.2

I have no clue whats wrong...its working it just wont stay up.
EnigmaticCoder
EnigmaticCoder
I would advise against this, but try putting a cin.get() at the end of your code (right after the return).

EDIT: The reason why I wouldn't recommend this method is because console programs are supposed to be run from the windows command line. Adding a cin.get() would make the program have an extra 'pause'. This method should work though.
--------------------Enigmatic Coding
rsyerigan
rsyerigan
why do you advise against it?
rsyerigan
rsyerigan
didnt work either =[
GamerYZ
GamerYZ
There is nothing wrong with your program. The "black box" is the console window, and according to your code, as soon as it prints out "hello world", it returns, which terminates the program.
rsyerigan
rsyerigan
thanks so how do i make it stay up to where it says press any key to continue or should i not worry about that?
rsyerigan
rsyerigan
is it the system pause thing?
EnigmaticCoder
EnigmaticCoder
This doesn't work?

[source lang ="cpp"]#include <iostream>using namespace std;int main(){    cout << "Testing...";    cin.get();    return EXIT_SUCCESS;}


Did you make the project a console application?

EDIT: Actually, I would chage the code to this:

[source lang ="cpp"]#include <iostream>int main(){    std::cout << "Testing...";    std::cin.get();    return EXIT_SUCCESS;}
--------------------Enigmatic Coding
rsyerigan
rsyerigan
Yes what you just posted worked


i think what it was is if i want it to stay i need to put

system("PAUSE");


and how did you get the box thing around it?
rsyerigan
rsyerigan
whats the std do?
Zahlman
Zahlman
... Wow. Could we possibly get more inaccurate?

0) There is nothing wrong with 'using namespace std' in implementation files. You just have to be aware of what it is doing. Don't put any kind of using-declaration in a *header* file, because files which include that header won't be able to undo the effect, and it could silently change the meaning of code.

In C++, identifiers (the names of functions, variables and classes, and probably other things that I've forgotten) can reside in "namespaces". The std namespace contains all the identifiers for things that are provided by the C++ standard library - hence "std", short for "standard". When you type one of these identifier names, the compiler won't recognize it as referring to the standard library component unless you tell it to look in that namespace.

This is a feature. It allows you, for example, to declare your own variable named "cout" and not have it be ambiguous.

When you write 'std::', you are using the "namespace resolution operator" (the double colon) to tell the compiler specifically "this name is one that you should look for in the std namespace".

When you write "using namespace std;", you tell the compiler "try also looking in the std namespace for any name." You can also write, for example "using std::cout;", meaning "whereever you see 'cout', the name 'cout' found in the std namespace is a valid interpretation".

When you use the using-declaration, you avoid needing to say "std::" explicitly for everything that needs to be looked up there. This is fine; avoiding repetition is a *good* idea in programming (repetition is the computer's job). However, you are consciously trading off flexibility: you now have problems if you want a variable named 'cout', because it will conflict.

1) The "black box" is a console window. The IDE creates it to display console text, for the lifetime of your application. It disappears right away because immediately after outputting the text, your program is finished, so the console window is not needed any more.

2) It is *not correct* to address this from within your program code, via system("pause"), a cin.get() request or any other hack. This is for the simple reason that waiting for user input *is not a logical part of the task your program is intended to perform*.

Instead, you can:

- Run the program from the command line, by opening a command window (Start->Run->put 'cmd' in the box and hit OK), navigating to whereever the program is stored on the hard drive (you are on your own for (a) learning to use DOS and (b) figuring out where your IDE puts your .exe), and running the program (just use the program name as a command).

- Create a batch script which runs your program, followed by the 'pause' program (this is the behaviour that the system("pause") hack tries to emulate). Assuming this is properly saved with a .bat file extension, it should be double-clickable in Windows.

- Learn how to tell your IDE to keep the command window open. I don't use an IDE so I can't be very helpful here; but common things I have heard are to try "Run without debugging", or to set a breakpoint on the final close brace of the program before you run it.

4) Please, read the FAQ. It's linked at the top of every forum page (including the post submission form), with an icon of an inner tube and a magic wand. It explains in great detail how all the code formatting works here.

There is also a FAQ for each forum. For example, the For Beginners Forum FAQ.
lolamay
lolamay
Hi Everyone. I seem to be going through a java problem. Due to a school project, I am making a phone number program that searches through to search for the caller's id and number but it doesn't seem to compile.

public String searchID(String[] idNumbers, String[] numbers, String callersNumber)

boolean found = false;
int n=0;
while ((!found) && (n < inputs)){
if (callersNumber == numbers[n]){
found = true;
n = n + 1;
}
if (found) {
return idNumbers[n-1];
}
else
{
return "New Number";
}
}

Does anyone see the problem?

Thanks,
Lolamay
Ravuya
Ravuya
Start your own thread if you're going to ask a question; don't resurrect and steal someone else's thread.

Locked.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.