11

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.

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Austin Hyde
  • 26,347
  • 28
  • 96
  • 129

6 Answers6

13

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.

Austin Hyde
  • 26,347
  • 28
  • 96
  • 129
2

I thought I would just add that if you are using Windows 10 and Qt Creator close to v4.13.2.

And you are developing a general console project.

Go to menu:

Tools > Options > Build & Run > General

and Find Default for "Run in terminal": and select Enabled, as its disabled by default.

Now when you click Run it will automatically open a command prompt window. Provided your code compiles :)

crowie
  • 171
  • 5
1

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.

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
0

The following solution is currently working. go to:

Project settings > Run > mark "Run in terminal"
0

For those who uses Qt Creator 10.0 on Windows 10:

Go to Edit->Preferences -> Build and Run and set the option Default for "Run in Terminal" to Enabled

enter image description here

Anton
  • 4,544
  • 2
  • 25
  • 31
-4
#include <QCoreApplication>
#include <iostream>
#include <string>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    std::string name;
        std::cout << "Enter name: ";
        std::cin >> name;
        std::cout << "Your name is " << name << std::endl;


    return a.exec();
}
forsvarir
  • 10,749
  • 6
  • 46
  • 77
Huffy
  • 1
  • 1