I am an amateur trying to learn C++. I complie my first prgram, it gives no errors, but when I run it, the output screen disappears so quickly that I barely get a glance of the output. Please help.
C++ Help: I run my program and the output disappears quickly?
Add getch(); in the last row before you close the main footer.
int main(){
//YOUR PROGRAM
getch();
return 0;
}
If you're using Turbo C++, press Alt+F5 to look at output screen.
peace
vixklen
Reply:if u are using dev-c++
use that:
SYSTEM("PAUSE");
Reply:before the end of the program, the last thing in main() add this:
getch();
it'll make it pause and wait for you to press enter.
Reply:use a .bat file (this work only if your OS is windows xp included )
1. create a new text file and rename it to script.bat it will display a warning, accept(in folder setting choose to show file extensions if you can't see the .txt part or don't get the warning)
2. edit the file in notepad and add the follwing code:
@echo off
//replace with path of your program compiled version
c:\mypro.exe
pause
3. just save and click on the bat file it will display :"Press any key to continue.....".
As long as your program name is constant the bat file will work!
OR...
You could run cmd.exe in xp or your command prompt command.com e.t.c (this works in linux too.)
then drag and drop your program onto the screen and press enter.
You could simplify your task by addind your ouput directory into the path environment variable
Like so:
in windows 9x:
Whenever you add a new value to the list of values for an
environment variable, use a semicolon to separate it. For example:
C:\DJGPP\BIN;other_values
The exact way how these variables should be set depends on your
operating system:
* For Windows 98 systems:
- Click START;
- Choose Programs-%26gt;Accessories-%26gt;System Tools-%26gt;System Information;
- Click Tools in the menu-bar, then choose "System Configuration";
- Use the tab provided there for editing your AUTOEXEC.BAT as
explained below (i.e., use the "set" commands for BAT files
as described at the end of this list).
* For Windows ME systems:
- Click START, choose Run, type msconfig.exe, click OK;
- Click the "Environment" tab;
- Edit the PATH system variable to add the C:\DJGPP\BIN subdirectory
(remembering to use a semicolon to separate this directory from
the others);
-
* For Windows NT systems:
- Right-click "My Computer", then select "Properties";
- Click the "Environment" tab;
- Edit the PATH system variable to add the C:\DJGPP\BIN subdirectory;
(if you are not an administrator, add the C:\DJGPP\BIN directory to
the user PATH variable - or create one with only this directory
since it is added to the system path);
* For Windows 2000 or Windows XP systems:
- Right-click "My Computer", then select "Properties";
- Select the "Advanced" tab, then click "Environment Variables" button;
- Edit the PATH system variable to add the C:\DJGPP\BIN subdirectory;
(if you are not an administrator, add the C:\DJGPP\BIN directory to
the user PATH variable - or create one with only this directory
since it is added to the system path);
* For all other systems (DOS, Windows 3.X and Windows 95): use any
text editor, e.g., the standard EDIT, to edit the file
AUTOEXEC.BAT in the root directory of the boot drive (usually,
C:).
Instead of editing your autoexec files and/or global environment,
you may wish to create a djgpp shortcut instead. To do this, create
a BAT file which has the lines below in it. This is often needed if
you have multiple compilers on the same system.
the environment variables DJGPP and PATH should be set like this:
set PATH=C:\DJGPP\BIN;%PATH%
DJGPP is my programming path and bin is my output path.
(sorry for the long answer)
(naturally since you say "I am an amateur trying to learn C++" i am assuming you are writing a command line program)
Reply:You might be using some IDE like visual C++ or something similar. What happens is that you don't have a break at the end of the program so it terminates after displaying the results. You can use the function getch() ( waits for one character at the command line ) which means once the result is displayed, just press any key to exit.
Reply:add cin.get(); or getch(); where you want it to pause.
Reply:Set a breakpoint near the end of the program.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment