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

  • #1
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?
 

Answers and Replies

  • #2
MATLABdude
Science Advisor
1,655
4
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.
 
  • #3
How do I execute the compiled program from the command prompt?

Also, is there anyway to fix this problem for the future?
 
  • #4
MATLABdude
Science Advisor
1,655
4
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
 
  • #5
If my project were c:\documents and settings\(username)\desktop\foo, how would I find that out? How do I locate the location?
 
  • #6
wait nevermidn I found it
 
  • #7
MATLABdude
Science Advisor
1,655
4
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"
 
  • #8
Where is the .exe to right click?
 
  • #9
MATLABdude
Science Advisor
1,655
4
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
MATLABdude
Science Advisor
1,655
4
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
jhae2.718
Gold Member
1,161
20
scanf is a function in C's stdio that gets characters from the CLI.

cin << is the C++ equivalent.
 
  • #14
MATLABdude
Science Advisor
1,655
4
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
MATLABdude
Science Advisor
1,655
4
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
MATLABdude
Science Advisor
1,655
4
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
34,158
5,777
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
MATLABdude
Science Advisor
1,655
4
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
191
3
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.
 

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

Replies
7
Views
4K
  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
5
Views
2K
Replies
1
Views
54K
Replies
9
Views
13K
Replies
1
Views
2K
  • Last Post
Replies
0
Views
1K
Replies
3
Views
2K
  • Last Post
Replies
4
Views
4K
Replies
12
Views
495
Top