I'm working on a console app in QtCreator 9. I need user inputs at some point, so I use a few std::cin in my c++ code.
The integrated "application output" pane does not accept any input though. I tried to switch to the Terminal by checking the right option (Projects->Run->Run in terminal). This works only when the app is launched with the Run button, but not with the Start Debugging of Startup Project button, which then falls back to the application output pane.
Any workaround ?
edit : I need interactive input, so a workaround with command line argument is not an option.
You can use the QTextStream class in Qt to get input from the terminal. You can redirect stdin to QTextStream using the following code:
#include <QTextStream>
#include <iostream>
int main(int argc, char *argv[])
{
QTextStream in(stdin);
QString input;
std::cout << "Enter some text: ";
in >> input;
std::cout << "You entered: " <<
input.toStdString() << std::endl;
return 0;
}
This way you can use the "Start Debugging of Startup Project" button and get the input from the terminal
Related
When i run the .exe release file it shows some kind of awkward error in my IDE.
And when I try to run from outside the IDE as an appilcation it terminates as soon as it opens without showing any output.
I am using Eclipse and MinGw
This is the error message my ide shows.
And i guess there is nothing wrong with my code.
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World" << endl;
cout << "hello Again" << endl;
return 0;
}
This is not an error, you are viewing the contents of the exe file which is not human-readable, but binary data. The output from your application is in the 'Console' window at the bottom of the screen.
I am using sublime text 3 with the plugin 'sublimeGdb' and trying to give input to it via sublimegdb input box that appears below on pressing the "shift
+ f5" to a program whilst a debugging session .
int main(int argv, char **argc){
int a;
cin >> a;
cout << a;
cout << "hello";
return 0;
}
Lets say i was running the above program with sublimeGdb and put via breakpoint on the line cout << a;.
Now the gdb session waits for the input of a .When the try to give input by entering in the gdb input text box ,say I entered "1".it then appends some value to the front of the value entered and assign int a = 121;. Also I tried giving input arguments in the project settings with the line "sublimegdb_arguments": "89". didn't work out .
How to properly give input values ?
How to see the stdout of the
program?
What I want to do is create a program to prompt the user for input. If they select yes I want to terminate the rest of the program. If they select no I want to immediately run a script. If there is no input for 5 minutes I want the script to automatically run. The only reason I want to use the script or batch file is I previously created it and it is already done.
The problems I am having is I am not sure how to declare the file system and file so it can be called. Then, I am not sure how to call the script to run. I am also not sure how to count down the 5 minutes to auto launch the script when ready. Below is my file in all it's current form.
//Program Name Apex Database Backup
//Written 8/3/2016
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include <windows.h>
#include <stdio.h>
using namespace std;
int main()
{
//Declarations
bool yes;
bool no;
char yesOrno;
string open;
FILE *C$;
//Prompt the User for Input
cout << "Are You Currently Loading Out? If Not This Program Will Execute in 5 Minutes. " << endl;
cout << "You Will Lose Your Connection to Apex! " << endl;
cout << "Enter yes or no: " << endl;
//Get User Input
cin >> yesOrno;
//Open the File
public FILE *DatabaseBackup.bat fopen(*C$)
//Process the Selection
if (no)
{
ShellExecute (DatabaseBackup.bat);
}
return 0;
}
The problems I am having is I am not sure how to declare the file system and file so it can be called.
I'm not sure what you mean by "declare the file system". If you want to open a file, take a look at C++ File IO
Then, I am not sure how to call the script to run
Running the script is a OS related function. See this answer on how to do this.
I am also not sure how to count down the 5 minutes to auto launch the script when ready.
You need to use a timer/clock with a signal/interrupt.
when I try to debug this code to read from a file and display it, the console screen comes and goes quickly and I don't understand why it's doing this. Can anyone help me please?
#include "Questions.h"
#include <iostream>
using namespace std;
const int MAXITEMS = 10;
struct quiz
{
string question;
string anser;
};
int main ()
{
string str;
ifstream ifs("Questions2.txt.txt");
getline (ifs,str);
cout << "first line of the file is " << str << ".\n";
return 1;
}
You should click some breakpoints in VS window.Then when you press F5,it will pause at breakpoint, then it will run continue until you press F5 again.
Or,if you make sure your code is correct.You can press Ctrl+F5.This means "Run Without Debug".
This situation,your program will run to end and suggest you "Press any key to continue".
Sorry for my bad english. Hope you can understand.
try with ifs.open, and then assure yourself by using ifs.is_open () function with an if and an error code, I always use it and it worth
and of course, use a breakpoint before the return (clicking it or using system ("pause")
You can try including a pause function. This way it will display your data and then wait for a response. I've included the function I typically use.
void myPause()
{
cout << " Press enter to continue... ";
char blank[8];
cin.getline(blank,8);
cin.sync();
}
Unless you run with some breakpoints, Visual Studio will close the window after the program terminates.
If you want the window to stay on the screen, use Debug->Start without debugging
Or, add a breakpoint at return 1;
Press F10 instead of F5. By pressing F10, you can go line by line
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to stop C++ console application from exiting immediately?
I am a newbie on C++. Following tutorials, my program selfkilled when it finished executing simple commands (like cout ans stuff). I discovered the get.cin() function that avoided that. However, anytime I use 'cin' commands to insert variables, the program selfkills just after receiving the input and executing the work. Is there a way to avoid that? thanks a lot!
The reason it quits when your program receives input, even though you are using std::cin.get() is because whenever cin reads input, there's a chance that some junk is left behind; when you call std::cin.get(), you will get that junk.
What you have to do is clear cin of any undesired data, such that std::cin.get() has nothing to read and is required to wait for new input.
...
std::cin.clear();
std::cin.get();
return 0;
A program doesn't "kill itself". It just exits when it has finished doing everything it was supposed to do (i.e. when you return from main).
It is up to you to set up your work environment so that you can see the output of a program. For example, if you are in Windows, you could open your own command line (run cmd) and run your program from there; or instruct your IDE to not close the terminal window after the program exits.
Your program doesn't kill itself after execution, it just ends it.
Simple example:
#include <iostream>
int main( int argc, const char* argv[] )
{
std::cout << "Hello, World" << std::endl;
return 0; // End of execution
}
In that example a small window opens then close very fast because the logic of the code says so, However in the next example:
#include <iostream>
int main( int argc, const char* argv[])
{
std::cout <<"Hello, World!" << std::endl;
std::cin.get();
return 0;
}
Your application will still be showing in the screen until you press enter key 'Return key' then it will exit.
In case you are using Windows Operating System, consider the next example:
#include <iostream>
int main( int argc, const char* argv[])
{
std::cout << "Hello, World!" << std::endl;
system("PAUSE");
return 0;
}
Please note that system("PAUSE") is in Windows only and won't run on other operating systems.
One more thing worth mentioning here, there are a lot of methods to use other that these, but I wrote the most common ones.
In some windowing systems, a console window is created when your program executes. When your program finishes, this console window disappears.
I always recommend the "pause" pattern to newbies:
cout << "Paused. Press ENTER to continue.\n");
cin.ignore(10000, '\n'); // Ignore until 100000 characters are entered or a newline is entered.
Sometimes, I make this into a function:
void Pause(void)
{
cout << "Paused. Press ENTER to continue.\n");
cin.ignore(10000, '\n'); // Ignore until 100000 characters are entered or a newline is entered.
}
Hope this helps,
std::cin.get() work well and its usage is pretty easy but it expect user to press return.
I used to end my program using ESC, so it won't work for me, so I use this
#ifdef _WIN32
std::system( "pause" );
#else
std::system( "read -n1 -r -p \"Press any key to continue...\"" );
#endif
It would print "Press any key to continue..." and continue execution with pressing any key so I can use my lovely ESC