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

  • Thread starter Thread starter UltimateSomni
  • Start date Start date
  • Tags Tags
    Basics
AI Thread Summary
The discussion focuses on troubleshooting issues with compiling and running a C++ program in Windows, specifically using Dev-C++ 4.9.9.2. Users encounter a problem where the command prompt closes immediately after execution. To resolve this, it's suggested to run the compiled executable directly from the command prompt instead of double-clicking it. Users are guided to locate the executable file and execute it by typing the correct path in the command prompt. To prevent the command prompt from closing right away, it's recommended to include a wait-for-user-input line in the code, such as using `cin >> myCharacter;` or `cin.get();` before the program terminates. There are also discussions about potential compatibility issues with Windows 7 and suggestions for alternative compilers, including the free Express version of Microsoft Visual C++. The thread emphasizes the importance of distinguishing between source files (.cpp) and executable files (.exe) during execution.
UltimateSomni
Messages
62
Reaction score
0
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?
 
Technology news on Phys.org
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?
 
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
 
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?
 
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.
 
  • #10
what does "scanf in C or cin << in C++" mean
 
  • #11
UltimateSomni said:
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.
 
  • #12
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?
 
  • #13
scanf is a function in C's stdio that gets characters from the CLI.

cin << is the C++ equivalent.
 
  • #14
UltimateSomni said:
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:
https://www.physicsforums.com/showthread.php?t=32703

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.
 
  • #15
C:\Users\Fred\Documents\TEST does nothing
adding cin >> x does nothing
 
  • #16
Okay, please post your code. Make sure it's between
Code:
[ /code] tags, with the space removed from the second tag.
 
  • #17
I copy pasted exactly what is used for the task in part 2.
 
  • #18
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:
  • #19
Yes, that. I'm using Dev-C++ 4.9.9.2

*cin.getch();* doesn't work either
 
  • #20
Is that program (the one from the tutorial) compatabile with windows 7?
 
  • #21
UltimateSomni said:
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.
 
  • #22
UltimateSomni said:
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
 
  • #23
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?
 
  • #24
cin.get() worked. I love you!
 
  • #25
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.
 
Back
Top