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

My console program behaves different on linux and windows

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 1.3k 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 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.

    jsulmJ 1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #2

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

      The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

      1 Reply Last reply
      3
      • U U7Development

        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.

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • U Offline
          U Offline
          U7Development
          wrote on 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...

          JonBJ 1 Reply Last reply
          0
          • U U7Development

            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...

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on 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
            • Kent-DorfmanK Offline
              Kent-DorfmanK Offline
              Kent-Dorfman
              wrote on 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";
                  }
              
              

              The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

              1 Reply Last reply
              2
              • U Offline
                U Offline
                U7Development
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0

                • Login

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