Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Console input with Qt Creator

Tags:

qt

qt-creator

I'm developping a very simple app on my Mac using QtCreator.

It's a console application and I want the user to enter its name, and then I display his name. Here is the code :

#include <iostream>  int main(int ArgC, char* ArgV[]) {     char Name[1000];      std::cout << "Type your name : ";     std::cin >> Name;      std::cout << "Hello " << Name << "\n";     return 0; } 

When running this app with QtCreator, the string "Type your name :" is displayed in the 'Application Output' tab. But if I type some text and press the enter key, nothing is happening.

What's wrong ?

like image 869
Jérôme Avatar asked Aug 31 '09 07:08

Jérôme


2 Answers

I found a solution. With Qt Creator 1.3.0 (on Mac OS X), here is what I had to do :

  • Project->Run settings, check "Run in Terminal" (thanks Ropez)
  • Qt Creator->Preferences : Environnement : General : Terminal : I had to put the whole path to XTerm. For my config, I had to put /usr/x11/bin/xterm -e.

Now, everything is working fine !

like image 95
Jérôme Avatar answered Sep 25 '22 12:09

Jérôme


Go to Project -> Run settings, and make sure "Run in Terminal" is checked.

BTW:

std::cin >> Name; 

is probably not what you want. It will read just a single token (typically only the first name). You should have a look at getline, or the string version.

like image 45
Ropez Avatar answered Sep 25 '22 12:09

Ropez