Solved My console program behaves different on linux and windows
-
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::stringOn 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.
-
and you included no code for us to see your probable misuse of the cin stream object.
-
This post is deleted! -
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 textPlease, 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 textThanks again...
-
@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 wellfflush(stdin)
plays withcin
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 likecin.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?
-
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"; }
-
Ok thanks!.. i will check both answers..