Cin doesn't work in console but works correctly with terminal in OSX. (QT 5.8)



  • Hi all,

    I'm new to QT and I'm trying simple examples to practice with this great tool.

    After some work with this tool I have a "simple" problem with the the console and cin.

    I did a simple program where I have to input a number (Celsius degree) and the program converts to Fahrenheit after I press ENTER key.

    The program runs perfectly if I use the terminal of OSX SIERRA but if I use the console, cout shows a message correctly to enter a number but after I do it and press the enter key the program doesn't return the result and "freezes".

    Is there any way to use the console instead the terminal of OS X?

    Thanks in advance.

    The program:

    // Program to convert temperature from Celsius degree
    // units into Fahrenheit degree units:
    // Fahrenheit = Celsius * (212 - 32)/100 + 32 //
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    int main(int nNumberofArgs, char* pszArgs[])
    {
    // enter the temperature in Celsius
    int celsius;
    cout << "Enter the temperature in Celsius:";
    cin >> celsius;
    // calculate conversion factor for Celsius to Fahrenheit
    int factor;
    factor = 212 - 32;
    // use conversion factor to convert Celsius into Fahrenheit values
    int fahrenheit;
    fahrenheit = factor * celsius/100 + 32;
    // output the results (followed by a NewLine)
    cout << "Fahrenheit value is:";
    cout << fahrenheit << endl;
    // wait until user is ready before terminating program to allow the user to see the program results system(“PAUSE”);
    return 0;
    }


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What do you mean by console ?



  • Just for curiosity, could you add #include <string> and try:

    // enter the temperature in Celsius
    int celsius;
    string celsiusString;
    cout << "Enter the temperature in Celsius:";
    getline(cin,celsiusString);
    celsius = stoi(celsiusString);
    


  • @SGaist

    With console I mean the application output of QT.
    As I said it shows the text "Enter the temperature in Celsius:" correctly but doesn't do nothing when I write a number and press enter key.


  • Moderators

    @itzalak2 As far as I know you cannot enter there anything. Check the "Run in terminal" checkbox in Run settings of your project.



  • Thanks to all. Definitively you can't input anything through the console so I have to use the terminal.

    One thing I find horrible is that when I run a simple app, QT launches the terminal but there is a very long message and I was asking if there is a way to avoid it.

    I attach a capture, there you can see what shows the terminal when is launched by QT.

    terminal
    https://ibb.co/n7yMbF


  • Moderators

    @itzalak2 What is the problem with this message? It is just what QtCreator is doing in the terminal to start your app. It will not be there if you start your app manually.



  • Yes, I know, but I would like not to show this loooooong message because it fills the 50% of the terminal screen. Is it possible to hide this message?

    Thanks for the help.


  • Lifetime Qt Champion

    Like @jsulm wrote, it won't appear if you run it from a normal terminal.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.