I'm trying the super basics and the command prompt keeps disappearing

For the second part of the tutorial, I'm suppose to compile and run to see what the output is. But, each time I click compile and run the command prompt shows up for less than a second and then goes away. What am I suppose to do?

Related Programming and Computer Science News on Phys.org
MATLABdude
Welcome to PhysicsForums!

I'll assume that you're doing this in Windows. Open up the command prompt (Start > Run > cmd) and then execute the compiled program (the .exe that's generated) from the command prompt. If you're lazy like me, you can copy the path of the .exe by right clicking on it and opening up its properties. You should then be able to paste this in the command prompt.

How do I execute the compiled program from the command prompt?

Also, is there anyway to fix this problem for the future?

MATLABdude
That's the default behaviour of the command prompt. Let's say your program is on your desktop, and named foo.exe

From the command prompt, you'd type in:
c:\documents and settings$$username)\desktop\foo That or you can move the program to the root directory (c:$$ or a more immediate folder (c:\temp) and then run the program. For instance,
cd c:\ (or cd c:\temp)
foo

If my project were c:\documents and settings\(username)\desktop\foo, how would I find that out? How do I locate the location?

wait nevermidn I found it

MATLABdude
See my comment about right-clicking on the .exe and opening up the properties to get the file path.

Now, as for a way to "fix" this "problem", I generally wait for user input (e.g. scanf in C or cin << in C++) at the end just before terminating.

EDIT: Along with printing a reminder to "Press any key to continue / terminate"

Where is the .exe to right click?

MATLABdude
The specifics depend on which compiler / IDE you're using. However, it should be inside your project folder. Give consideration to the wait-for-user-input idea.

what does "scanf in C or cin << in C++" mean

MATLABdude
what does "scanf in the C programming language or cin << in the C++ programming language" mean
Fixed that for you/me There should be an equivalent in whatever language you're programming in.

Okay so I enter in "C:\Users\Fred\Documents\TEST.cpp" without quotes and the command prompt minimizes and nothing happens . What did I do wrong?

jhae2.718
Gold Member
scanf is a function in C's stdio that gets characters from the CLI.

cin << is the C++ equivalent.

MATLABdude
Okay so I enter in "C:\Users\Fred\Documents\TEST.cpp" without quotes and the command prompt minimizes and nothing happens . What did I do wrong?
You're not using the right file. .cpp is your source file, not the executable. Try entering just "C:\Users\Fred\Documents\TEST" (without the quotes)

I'll presume that you're going through the C++ tutorial that's stickied in this forum and going through the second assignment:

Just before the return 0;, add a new line cin >> x;

The command prompt should now terminate whenever you hit enter.

EDIT: And yes, pay attention to which way the arrows point: I goofed up the direction up until this post.

C:\Users\Fred\Documents\TEST does nothing
adding cin >> x does nothing

MATLABdude
Code:
[ /code] tags, with the space removed from the second tag.

I copy pasted exactly what is used for the task in part 2.

MATLABdude
This?

Code:
#include<iostream>

using namespace std;

int main( int argc, char *argv[]) {
char myCharacter = 'z';
cout << "The datatype is " << sizeof(int) << " bytes!" << endl;
cout << "The variable has a value of " << myCharacter << endl;
[b]cin >> myCharacter;[/b]
return 0;
}
Before compiling and running. If that still doesn't work, try cin.get(); instead. I probably should have said this right off the bat, but what are you using as your IDE / compiler?

Last edited:
Yes, that. I'm using Dev-C++ 4.9.9.2

*cin.getch();* doesn't work either

Is that program (the one from the tutorial) compatabile with windows 7?

Mark44
Mentor
Yes, that. I'm using Dev-C++ 4.9.9.2

*cin.getch();* doesn't work either
What do you mean, it doesn't work? Please be more specific.

It's possible that your compiler puts the executable (test.exe) in a different directory than your source file (test.cpp) is in. I use a different compiler, Visual Studio 10, and it stores the executable two directories down from where the source file is.

MATLABdude
Yes, that. I'm using Dev-C++ 4.9.9.2

*cin.getch();* doesn't work either
Yes, that part was messed up. As per the edit, it should be cin.get() and not cin.getch() Are you getting any compiler error messages?

As well, there seems to be an issue with Dev-C++ running its own console program, instead of the standard one. If cin.get() STILL doesn't work, see FAQ points 1 and 2 for another work-around (note that this would only work in windows):
http://www.bloodshed.net/faq.html#1

All right I'm pretty sure it is not compatible with windows 7. Do you any of you know a free compiler that works with C++ 64 bit?

cin.get() worked. I love you!

You can use the "Express" version of Microsoft Visual C++ for free, it's a cut down version of the full release, and you can't release your code commercially. I've used it in a brief foray into Source Modding (<3) and it was easy.