c++ execution screen not stable [duplicate] - c++

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to stop C++ console application from exiting immediately?
I am running a simple program, written in Dev C++ 4.9.9.2 IDE in Windows 7:
// my second program in C++
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World! ";
cout << "I'm a C++ program";
system("pause");
return 0;
}
This compiles successfully, but when I run it, the terminal screen comes for a second and then vanishes. How can I keep the output screen of the program visible?

You need to do cause the program to stop and wait for input. Use
system("pause");
at the end of your program before the return from main.

In addition to the options already presented (std::getchar, cin, system("pause"), if the only reason you want the window to persist is to read the output of your program (i.e. debugging), you can simply run the executable from a command prompt.
If you don't mind running the application this way, you can avoid having extra code to prompt a user for input (even if it just a single line) - and if you don't need the window to stay open under normal usage, you don't have to modify anything about your code.

Related

Why is the console asking for input even though there is no input in code?

I'm taking an online computer science class for grade 12 and we're using c++. I've never touched c++ and I'm starting to wish I never had. The teacher is comparing c++ to java (a language I can use perfectly fine) and we're currently learning how to input and output strings and chars. The simple practice problem was
Use one of fputc(), putc(), or putchar() to print your name one char at a time.
Since I have no clue how to use fputc() or putc() I decided to go with putchar()
#include <iostream>
using namespace std;
#include <stdio.h>
int main() {
cout << "My name is :" << endl;
putchar('J');
putchar('a');
putchar('c');
putchar('o');
putchar('b');
return 0;
}
I tried just using putchar(), and then added the cout, and have tried restarting eclipse, etc. but every time I run the program, the console asks for an input. There should not be an input for this program at all.
Try running your program from outside of the IDE and see what happens. When you launch a console program from inside of an IDE, a new console window is created to run the program in. When the program ends, the console window will close. Many IDEs setup the console to wait for you to press a key, giving you a chance to see the program's output, before the window closes.

c++ getchar works in vs 2010, doesn't in 2012

Why the program just exits, in vs 2012, while in 2010 does it wait for my input?
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string path = "c:\somepath";
cout << "Path is" + path << endl;
getchar();
return 0;
}
The getchar() function is not used in the program flow and only the side effect of waiting for input is used in your case. Instead of hacking your program to avoid closing the window you should use the proper way to keep it open.
Choose one of:
Command window
open a command prompt
navigate to the folder where you want your program to execute
type the path to the .exe and hit Enter. The command prompt is still open after execition of your program and there is not need for getchar().
Start without debugging
just use the menu item Debug->Start Without Debugging. The new console window is still open after execition of your program and there is not need for getchar().
Start with debugging
Set a breakpoint at the closing '}' of your main function.
use the menu item Debug->Start. The debugging session is still active and the window for your console program is still open. There is not need for getchar().
Why you should avoid getchar() to keep a window open?
It's a hack that depends on the program before. There can be some characters in the input buffer a s pointer out here: getchar() doesn't work well?
Your program can't be used in batch file if you come beyond the phase where you try to learn C or C++.
It's not necessary.

How to prevent visual studio 2013 console window to close right after running a program? [duplicate]

This question already has answers here:
Keeping console window open when debugging
(3 answers)
Closed 8 years ago.
Hi I am using visual studio express 2013.I have never used vs before and so just to test it out I ran a simple c++ program where the user enters 2 integers and their sum is then displayed. The problem is that the console window appears and takes the inputs, but then immediately closes once the output is displayed. Please note that this happens right after all the inputs are taken and when the output is shown. Is there anyway to fix this? I have looked all over and cant find a solution. I have tried including a bunch of things such as the getch() function at the end of my program, and pressing ctrl F5 to debugg my programs,but nothing seems to work. Please help!!!
I have been using this,
int getchar ( void );
Get character from stdin
Returns the next character from the standard input (stdin).
OR
use this from process.h
system("PAUSE");
This approach is for beginners. It's frowned upon because it's a platform-specific hack that has nothing to do with actually learning programming, but instead to get around a feature of the IDE/OS - the console window launched from Visual Studio closes when the program has finished execution, and so the new user doesn't get to see the output of his new program.
One decent approach is Debug.WriteLine
// mcpp_debug_class.cpp
// compile with: /clr
#using <system.dll>
using namespace System::Diagnostics;
using namespace System;
int main()
{
Trace::Listeners->Add( gcnew TextWriterTraceListener( Console::Out ) );
Trace::AutoFlush = true;
Trace::Indent();
Trace::WriteLine( "Entering Main" );
Console::WriteLine( "Hello World." );
Trace::WriteLine( "Exiting Main" );
Trace::Unindent();
Debug::WriteLine("test");
}
In debug mode, Before your main return you could put a breakpoint and you will be able to see your console and the result you are waiting for.
Put
system("PAUSE");
wherever you need the program execution window to pause. If you're using int main(), usually it'll be right before you return 0

C++ , won't display last line of code

Shouldn't this work? I mean, the code is merely a test and is meant so that the dialogue goes this way : What is your name? name here, Hello name here, and yet it does not show the last line of Hello after I type in my name and click enter it just dissapears. Here is the code.
#include <iostream>
#include <string>
int main (void)
{
using std::cin;
using std::cout;
using std::string;
string name = "";
cout << "What is your name, pls?\n";
cin >> name;
cout << "\nHello " << name.c_str() << "\n";
return 0;
}
My guess is that you are running from the debugger, or double clicking the executable. In either of those cases, when the program ends, the console will close. So, the program produced output, but you just could not see it before the console closed.
Run the program from a pre-existing console so that the console remains after your program ends. Or, just whilst debugging, arrange that your program does not terminate immediately after emitting its final output. A simple way to do that is to place a break point at the end of the program.
It probably showed it right before it disappeared. If you're going to write console programs, and if you're going to send output to a console, you should run them from a console so the output has some place to go.
After you are done with your program, press Ctrl + F5 ( Run without debugging). This will prompt before closing the window and this is what you want.
Make sure you put a breakpoint before main goes out of scope. I guess your console disappears under VS?
Also, you don't need to extract the char* in the last cout statement:
cout << "\nHello " << name << endl;
Open a terminal (or a command prompt window).
Navigate to the folder that contains the executable.
Run it.
It's not disappearing. It is just running really fast.
Every IDE has a keyboard shortcut that allows you to run code and pause after the execution has finished.
This keyboard shortcut is Ctrl-F5 in Visual Studio.
I have no idea what IDE you're running, but that is your basic problem.
The other thing you can do is to test your code in ideone : ideone.com/hb4Cel (it's the same code. There is no point pasting it here)
A dirty workaround is to add something like this
cin >> name;
at the end, just before return 0;. It forces the window to wait for input (i.e. hitting return) before returning (which closes the program).
This isn't necessarily good design, but if all you want to do is run some tests then it'll do the trick.
Basically when you enter your name it displays your last line and exits after return 0.
Here are the following things to avoid that
1- use command line to run the application
Start->accessories->command prompt
Go to folder in which your application is using cd command
c:>cd c:\path\foldername
Now run the application by typing the program name e.g
c:\path\foldername>my_application.exe
It will display your last line.
2- Now if your are using microsoft visual c++ press ctrl+F5 to run your program
3- This is not recommended but you an use it as long as your are debugging then remove it from the code afterwards. Include conio.h header file and add getch(); line before return statement. It would hold the screen for you till you press a key.

Xcode not showing console output; How do you flush the console?

I have a simple C++ program that uses cout and printf to log stuff and it is only showing at the end when the program is closed but if I'm stepping through the program using debug nothing is shown. Did anybody have this problem?
If you're practicing c try fflush, if c++ try cout << endl; each time you want to print.