🔒 Locked
Problem with program running.
Advertisement
Original Post
This is what I have. //my first program in C++ #include <iostream> 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?
Hello,
If you are using Microsoft visual c++ .net try running the program without debugging. (the icon is a red exclamation mark)
Or you could run the exe from the command line.
EDIT: http://www.gamedev.net/community/forums/faq.asp#tags will tell you how to use the code formatter on these forums.
If you are using Microsoft visual c++ .net try running the program without debugging. (the icon is a red exclamation mark)
Or you could run the exe from the command line.
EDIT: http://www.gamedev.net/community/forums/faq.asp#tags will tell you how to use the code formatter on these forums.
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.
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.
This doesn't work?
Did you make the project a console application?
EDIT: Actually, I would chage the code to this:
[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;}
http://www.gamedev.net/community/forums/faq.asp#tags will tell you how to use the code formatter on these forums.
The std::
is used in the place of
using namespace std
The line 'using namespace std' is considered bad practice.
The std::
is used in the place of
using namespace std
The line 'using namespace std' is considered bad practice.
... 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.
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.
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
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
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement