Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. My console program behaves different on linux and windows
Forum Updated to NodeBB v4.3 + New Features

My console program behaves different on linux and windows

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 482 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    U7Development
    wrote on 20 Mar 2020, 01:19 last edited by U7Development
    #1

    Hello!

    I have written a very simple menu console program that connects to a mysql db over internet, it works.. just something is not working, since this is a console program it uses std::cin.get() to catch inputs, this happens::

    On Windows (works fine)
    Program ask user for a name and wait...
    user input name
    success on saving the name into a std::string
    program ask for telephone and wait...
    user input telephone
    success on saving the telephone into a std::string

    On Linux
    Program ask user for a name but not wait..
    program ask for telephone and wait...

    as you can see, there is no way the user can input a name...
    i'm flusshing stdin using fflush(stdin) either win and linux, its suppose they must to work the same way..

    Any idea?
    Thanks so much!

    Note my program is a QApplication console program.

    J 1 Reply Last reply 20 Mar 2020, 05:44
    0
    • K Offline
      K Offline
      Kent-Dorfman
      wrote on 20 Mar 2020, 01:42 last edited by
      #2

      and you included no code for us to see your probable misuse of the cin stream object.

      1 Reply Last reply
      3
      • U U7Development
        20 Mar 2020, 01:19

        Hello!

        I have written a very simple menu console program that connects to a mysql db over internet, it works.. just something is not working, since this is a console program it uses std::cin.get() to catch inputs, this happens::

        On Windows (works fine)
        Program ask user for a name and wait...
        user input name
        success on saving the name into a std::string
        program ask for telephone and wait...
        user input telephone
        success on saving the telephone into a std::string

        On Linux
        Program ask user for a name but not wait..
        program ask for telephone and wait...

        as you can see, there is no way the user can input a name...
        i'm flusshing stdin using fflush(stdin) either win and linux, its suppose they must to work the same way..

        Any idea?
        Thanks so much!

        Note my program is a QApplication console program.

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 20 Mar 2020, 05:44 last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • U Offline
          U Offline
          U7Development
          wrote on 21 Mar 2020, 02:16 last edited by U7Development
          #4

          Hm.. may i misunderstood the use of cin object... may be you're right, but Windows is not complaining..
          I didn't because code is so long, but i could isolate this:

          //Pide al usuario la informacion del nuevo centro
              std::vector<std::string> campos{"Region     :   ", "Nombre     :   ", "Direccion  :   ", "Telefono   :   ", "Email      :   ", "Contrasena :   "};
              for (std::vector<std::string>::iterator it = campos.begin(); it != campos.end(); ++it){
          
                  if (*it == "Region     :   ") continue;
                  fflush(stdin);
                  std::cout << *it;
                  char* t = new char[256];
                  std::cin.getline(t, 256);
                  centro_actual.push_back(static_cast<std::string>(t));
                  delete [] t;
                  std::cout << "\n";
              }
          
          //Thanks!!
          

          Here is the full version:
          link text

          Please, don't tell me that my code is a mess , i know it.. , i wrote it on the fly, the point is that it works on Windows but not on Linux.. :)

          I have created a short video explanation:
          link text

          Thanks again...

          J 1 Reply Last reply 21 Mar 2020, 12:06
          0
          • U U7Development
            21 Mar 2020, 02:16

            Hm.. may i misunderstood the use of cin object... may be you're right, but Windows is not complaining..
            I didn't because code is so long, but i could isolate this:

            //Pide al usuario la informacion del nuevo centro
                std::vector<std::string> campos{"Region     :   ", "Nombre     :   ", "Direccion  :   ", "Telefono   :   ", "Email      :   ", "Contrasena :   "};
                for (std::vector<std::string>::iterator it = campos.begin(); it != campos.end(); ++it){
            
                    if (*it == "Region     :   ") continue;
                    fflush(stdin);
                    std::cout << *it;
                    char* t = new char[256];
                    std::cin.getline(t, 256);
                    centro_actual.push_back(static_cast<std::string>(t));
                    delete [] t;
                    std::cout << "\n";
                }
            
            //Thanks!!
            

            Here is the full version:
            link text

            Please, don't tell me that my code is a mess , i know it.. , i wrote it on the fly, the point is that it works on Windows but not on Linux.. :)

            I have created a short video explanation:
            link text

            Thanks again...

            J Offline
            J Offline
            JonB
            wrote on 21 Mar 2020, 12:06 last edited by JonB
            #5

            @U7Development said in My console program behaves different on linux and windows:

            cin.getline

            I don't know, but may I suggest you Google for cin.getline, possibly with "not working". There seems to be quite a few refernces out there saying "it sometimes doesn't work". Various reasons. For one thing, I don't know how well fflush(stdin) plays with cin level, stdin & cin aren't the same thing. I have seen (old) posts like http://www.cplusplus.com/forum/general/1477/. Some suggestion that things like

            cin.clear();
            cin.sync();
            cin.ignore()
            

            might play better? The behaviour difference between Windows vs Linux is presumably down to implementation, even different compilers (with different runtime libraries) might vary?

            1 Reply Last reply
            2
            • K Offline
              K Offline
              Kent-Dorfman
              wrote on 21 Mar 2020, 13:53 last edited by Kent-Dorfman
              #6

              This is my OCD influenced rewrite of your code, and it works fine in c++ on Linux. I did change to c++11 constructs because they are more elegant, but I don't really see any problem with your algorithm in the code you supplied, which makes me believe the problem is something else that is influencing the behaviour in a way that makes you think it is a a cin problem, but maybe is not.

                  char t[256];
                  for (auto it : campos) {
                      if (it == "Region     :   ") continue;
                      fflush(stdin);
                      cout << it;
                      cin.getline(t, 256);
                      // should really check the error state of cin before adding element
                      centro_actual.push_back(t);  // static_cast<string>(t));
                      cout << "\n";
                  }
              
              
              1 Reply Last reply
              2
              • U Offline
                U Offline
                U7Development
                wrote on 21 Mar 2020, 17:06 last edited by
                #7

                Ok thanks!.. i will check both answers..

                1 Reply Last reply
                0

                1/7

                20 Mar 2020, 01:19

                • Login

                • Login or register to search.
                1 out of 7
                • First post
                  1/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved