473,396 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Output of result closing window

I have been reading a book titled "Learn C in 21 days" and ive noticed
that everytime i program something I try to open it and it comes up for a
split second then closes. In some programs where you are supposed to
enter something for instance your age or weight it stays up to allow me
to do that. But once i hit the enter button for it to display the answer
or output the box just closes. i was a little skeptical on exactly how
to run the program I have just been opening the program .exe. perhaps i
am opening it wrong or there is something that I need to do in order to
fix this. Please help me out.
Feb 8 '07 #1
15 2086
RedSon
5,000 Expert 4TB
Whats happening is you are probably opening it up from Windows Explorer when what you should be doing is opening it from the command line.
Feb 8 '07 #2
AdrianH
1,251 Expert 1GB
I have been reading a book titled "Learn C in 21 days" and ive noticed
that everytime i program something I try to open it and it comes up for a
split second then closes. In some programs where you are supposed to
enter something for instance your age or weight it stays up to allow me
to do that. But once i hit the enter button for it to display the answer
or output the box just closes. i was a little skeptical on exactly how
to run the program I have just been opening the program .exe. perhaps i
am opening it wrong or there is something that I need to do in order to
fix this. Please help me out.
What the last guy said, or you are running it from the debugger. Put a break point at the end of the main function, or request input from the user before leaving the main function to keep the window from closing.


Adrian
Feb 9 '07 #3
Alright im very new to programming. is the command line the same thing as the command prompt and if so how do i access a program from it. if it isnt how do i access the command line.
Feb 9 '07 #4
AdrianH
1,251 Expert 1GB
Alright im very new to programming. is the command line the same thing as the command prompt and if so how do i access a program from it. if it isnt how do i access the command line.
Command line=Command prompt

To execute an executable from the command line type the name of the executable

In windows it will search for the executable in the current directory and if it doesn't exist will search each path (separated by semicolons) in the path environment string in order. To see the current path environment variable type
Expand|Select|Wrap|Line Numbers
  1. echo %path%
.

In Linux/Unix, it doesn't search the current directory unless it is in the path (security reasons). The current directory's name is specified with a '.' in all operating systems.

If the executable is in a location that is not in the current directory and not in the path but somewhere relative to it, you can specify a relative path in front of the executable name. Say the executable is called 'Bar.exe' and is in the child directory from where you are named 'Foo'. Type
Expand|Select|Wrap|Line Numbers
  1. Foo\Bar.exe
or
Expand|Select|Wrap|Line Numbers
  1. Foo\Bar
. If it is in the parent directory, you can type
Expand|Select|Wrap|Line Numbers
  1. ..\Bar.exe
or
Expand|Select|Wrap|Line Numbers
  1. ..\Bar
. You can traverse the entire directory tree relatively if you want. I.e.
Expand|Select|Wrap|Line Numbers
  1. ..\..\..\Foo\Boo\Bar
will go to the parent's parent's parent's Foo's Boo directory and try and execute Bar.

You can also specify a path to the executable absolutely, which just means it is relative to the root drive (or in the case of Linux/Unix the root). I.e.
Expand|Select|Wrap|Line Numbers
  1. C:\Foo\Bar
will try and execute Bar in a directory off of the C drive named Foo. Linux/Unix doesn't have drive letters, it just has a single root in its file system. This presents something that occurs only in Windows, a relative root. If your current directory is in D: drive, then if you specify \Foo\Bar, it will look for Bar in D:\Foo. If your current directory is on C: drive, it would look for Bar in C:\Foo.

In Linux/Unix, the directory separators are different from that in windows. Linux
uses the slash (/) where as the Windows uses the backslash (\) (for interest sake, the Mac used to use colon (:) but is now moved to a Unix system so uses slash).

Oh, and before I forget, to change the current directory from the command line, use cd <path>, where <path> is an absolute or relative path.

And to change the search path in Windows for all newly executed applications and command prompts, right click on "My Computer" located either on the desktop or by clicking on the Start button, go to Properties, Advanced, Environment Variables and double click on PATH in the System (affects all uses) or User (affects current user only).

You can modify the current command prompt's path by typing
Expand|Select|Wrap|Line Numbers
  1. path=%path%;<new path(s)>
or
Expand|Select|Wrap|Line Numbers
  1. path=<new path(s)>;%path%
. The first will tack the new paths to search at the end and so would be searched last, the last will put it at the beginning and so would be searched first.

In Linux/Unix, it is
Expand|Select|Wrap|Line Numbers
  1. PATH=<new path(s)>:$PATH
, or
Expand|Select|Wrap|Line Numbers
  1. PATH=$PATH:<new path(s)>
and you may have to export the path by typing
Expand|Select|Wrap|Line Numbers
  1. export PATH
if you use a derivative of sh (ash, bash), but I'm not sure about that one as I think that it is already exported.

Boy, I bet you thought that this was a simple question, eh? ;)

For ease, just change to the correct directory and type the executable name. And if you are using Linux/Unix put the current directory in your path (.) when you are doing development, though you shouldn't have it there by default.

One other thing, if you don't like a lot of typing, you can use what is called tab completion which is you type the first letters of the directory or executable and press tab. It will try and complete it for you. Hit it again to go to the next one that matches. Unix/Linux will only complete till it becomes ambiguous, pressing a second time may get you a list of possible completions depending on the shell you are using.

Ok, I'm done now. Go on your way. :)

Hope this helps.


Adrian
Feb 9 '07 #5
When i go to the command prompt and i type in the .exe for my programs it says that it is not recognized as an internal or external command, operable program or batch program.
Feb 9 '07 #6
RedSon
5,000 Expert 4TB
When i go to the command prompt and i type in the .exe for my programs it says that it is not recognized as an internal or external command, operable program or batch program.
Thats because of the PATH issues that Adrian talks about above. Try navigating to the directory where your exe is built. Use the "cd" (change directory) command and "dir" (list directory contents) command to navigate. (Or "ls" if you are on a linux box)

You will probably need to do something like this:
Expand|Select|Wrap|Line Numbers
  1. C:\> cd myProjectDirectory\myProject\obj\release\ <press enter>
  2. C:\myProjectDirectory\myProject\obj\release\> myExecutable.exe
  3.  
  4. ....Hello World!...
  5.  
  6. press any key to continue...
  7.  
Feb 9 '07 #7
Alright guys I finnaly got it. thank you so much for all your help.
Feb 9 '07 #8
AdrianH
1,251 Expert 1GB
Alright guys I finnaly got it. thank you so much for all your help.
Eeeexlelent Smithers. :)
Feb 9 '07 #9
RedSon
5,000 Expert 4TB
Eeeexlelent Smithers. :)
OMG did you just make a Simpson's joke!? LOL
Feb 9 '07 #10
AdrianH
1,251 Expert 1GB
OMG did you just make a Simpson's joke!? LOL
And what, pray tell, is wrong with that? I bestoweth upon you... a boot to the head! :p
Feb 9 '07 #11
RedSon
5,000 Expert 4TB
Nothing, I just prefer Family Guy references.
Feb 9 '07 #12
Motoma
3,237 Expert 2GB
I have been reading a book titled "Learn C in 21 days" and ive noticed
that everytime i program something I try to open it and it comes up for a
split second then closes. In some programs where you are supposed to
enter something for instance your age or weight it stays up to allow me
to do that. But once i hit the enter button for it to display the answer
or output the box just closes. i was a little skeptical on exactly how
to run the program I have just been opening the program .exe. perhaps i
am opening it wrong or there is something that I need to do in order to
fix this. Please help me out.
The easiest way is to include system("pause"); before int main() returns. This will allow you to view the output from your IDE without having to switch to the command line. It will also allow you to see output when you double click the executable.
Feb 9 '07 #13
RedSon
5,000 Expert 4TB
The easiest way is to include system("pause"); before int main() returns. This will allow you to view the output from your IDE without having to switch to the command line. It will also allow you to see output when you double click the executable.
Using system("pause") is dependent on what system you are on. It would be better to do something like

Expand|Select|Wrap|Line Numbers
  1. ...
  2.  
  3. cout << "Press any key to continue":
  4. cin >> throwAwayExit;
  5.  
  6. }
  7.  
  8.  
Feb 9 '07 #14
Motoma
3,237 Expert 2GB
Using system("pause") is dependent on what system you are on. It would be better to do something like
That's true, but he specified he was using Windows.
Feb 9 '07 #15
RedSon
5,000 Expert 4TB
That's true, but he specified he was using Windows.
Oh in that case...whatever floats your boat.
Feb 9 '07 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Dave | last post by:
I'm getting different results when I display a value in the Output window as opposed to the Immediate window. Why? Code to recreate problem: 'Purpose: Get different results from Output...
0
by: Keith Dick | last post by:
I'm trying to use the free command line C/C++ compiler for .NET and when I try to debug a simple C program that uses printf(), I don't see the output it produces. A console window does open when...
5
by: Ron L | last post by:
I have an MDI application with a number of child windows. In each child window I am catching the Closing event and having the child window decide if it should set cancel to true. The intent here...
1
by: Chris Bruce | last post by:
In my application I need a way to distiguish between the following events: 1. When a user closes an MDI child window. 2. When the user closes the MDI parent window which subsequently closes the...
8
by: Patreek | last post by:
Hi, On the line where I'm assigning RecordCount to be the value of my output parameter, I'm getting the generic "Object reference not set to an instance of an object" error. I've isolated it...
4
by: Kevin Mansel via .NET 247 | last post by:
Ok, basically this is my problem. I'm building a console app tocall a dos program. So i'm using the Shell command to call theprogram, now depending on what happens, I want to read theoutput that...
2
by: Tom | last post by:
How is the best way to avoid validation when closing a window? For instance, I have a Windows Forms window which has a validation event for a text box. However, if one enters invalid data in then...
3
by: Jason | last post by:
I've got a simple program in which I just write something to the console output window, but when I compile and run the program, the output windows displays the output and closes right away. How do...
14
by: fdu.xiaojf | last post by:
Hi, I'm writing a program which imports an external module writing in C and calls a function provided by the module to do my job. But the method produces a lot of output to the stdout, and this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.