Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using cin in QtCreator

For school, we use C++ as the language of choice. I am currently using QtCreator as an IDE, and for its GUI library, it is wonderful. The school is using Visual Studio.

However, most of the programs we are writing make use of cin and cout for input/output. cout works fine as output, as you can see what it puts out in the application output, but there is no way to provide to cin as if it were on a console, like Visual Studio uses for its C++.

An example:

#include <iostream>
#include <string>
using namespace std;
int main() {
    string name;
    cout << "Enter name: ";
    cin >> name;
    cout << "Your name is " << name << endl;
}

Is there a way to use a console or provide input to cin like in Visual Studio?

I am currently running OS X Leopard, if it matters.

like image 613
Austin Hyde Avatar asked Sep 28 '09 00:09

Austin Hyde


People also ask

Can you use Qt with C++?

Qt Creator is a cross-platform (Windows, Linux, Mac) IDE that is part of the Qt SDK and is aimed at simplifying the development of cross-platform GUI applications. It is mostly used with C++ (especially with embedded devices that require UI front), but also with JavaScript and QML.

Is Qt Creator better than Visual Studio?

In the end it comes down to preferences if you want to use Qt Creator or Visual Studio for development. It is easy to create a VS project file from Qt project files using qmake. I prefer using Qt Creator because I can navigate a lot faster. My colleague, however, uses Visual Studio.

Is Qt and Qt Creator same?

Qt Creator is just an IDE used to build QT applications; both Qt Widgets and Qt Quick can be composed. When writing Qt Widgets applications you can edit your GUI in Qt Designer but in case of Qt Quick applications you use Qt Quick Designer, both integrated into Qt Creator.

How do I run Qt in terminal?

For console applications, check the Run in terminal check box. To specify the terminal to use on Linux and macOS, select Edit > Preferences > Environment > System. To run with special environment variables set up, select them in the Run Environment section. For more information, see Selecting the Run Environment.


2 Answers

In Preferences, under the Environment section, set the "Terminal" option to /Applications/Utilities/Terminal.app, as pointed out by Alex Martelli.

Then, in the Projects tab, under Run Settings, check the box marked "Run in Terminal".

Now, QtCreator will use Apple's built-in Terminal.app instead of Qt's console, allowing for interactive input.

like image 96
Austin Hyde Avatar answered Sep 20 '22 19:09

Austin Hyde


If you're doing "console"-style apps with no GUI, Qt Creator may not be the most appropriate IDE -- why not try Apple's own XCode, which probably comes on your OS DVD (as a separate installer), and worst-case can be freely downloaded by registering at Apple Developer Connection?

Edit: as the OP indicates that all they need is the location to Mac's terminal app, that's easy: it's /Applications/Utilities/Terminal.app.

like image 21
Alex Martelli Avatar answered Sep 21 '22 19:09

Alex Martelli