• Welcome to Valhalla Legends Archive.
 

cin question

Started by Eli_1, February 09, 2004, 07:45 PM

Previous topic - Next topic

Eli_1

Console app... fyi

I wanted to know if it was possible to get user input while other things are happening... such as receiving data and displaying it... I can't really use cin.getline() because when my loop hits that it won't move on and display everything. is there a way around this or is it impossible with console apps?

Maddox

#1
Quote from: Eli_1 on February 09, 2004, 07:45 PM
Console app... fyi

I wanted to know if it was possible to get user input while other things are happening... such as receiving data and displaying it... I can't really use cin.getline() because when my loop hits that it won't move on and display everything. is there a way around this or is it impossible with console apps?

I know that on unix based operating systems you would use fcntl() but I'm not sure if this works on windows.
asdf.

iago

You can do it using threads for sure, but that becomes difficult.  I think there is a better way, but I'm not sure.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Adron

I know of no standard way of doing it. In Windows, I'd msgwaitformultipleobjects on among other things a console input handle.

j0k3r

I know how to do this in another programming language, and it's from a loooong time ago... Multithreading is not something very hard in that language, but I don't know much about C++ and so couldn't tell you how to do it.
QuoteAnyone attempting to generate random numbers by deterministic means is, of course, living in a state of sin
John Vo

Maddox

Quote from: j0k3r on February 09, 2004, 10:12 PM
I know how to do this in another programming language, and it's from a loooong time ago... Multithreading is not something very hard in that language, but I don't know much about C++ and so couldn't tell you how to do it.

Creating a new thread with CreateThread() is not too difficult but it seems like overkill for what he is doing.
asdf.

Kp

Quote from: Maddox on February 10, 2004, 08:14 AMCreating a new thread with CreateThread() is not too difficult but it seems like overkill for what he is doing.

Also, adding a thread would introduce all sorts of synchonization issues.  I say give up cin and use ReadConsoleInput in conjunction with MsgWaitForMultipleObjects (as Adron suggested).  ReadConsoleInput is better anyway -- you can get much finer control, and avoid the potential chokeups cin suffers when it gets a datatype it doesn't like.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

iago

Using a floating textbox might also work for you.  Hopefully Adron will see this and give some sort of detail on that one :)
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Adron

Quote from: iago on February 10, 2004, 11:51 AM
Using a floating textbox might also work for you.  Hopefully Adron will see this and give some sort of detail on that one :)

Haha, floating textbox works fine. You'd want to use the MsgWaitForMultipleObjects technique with that as well, just create the floating input window some time before you enter your main loop.

Eli_1

#9
Ok thanks, I'll look around for that.

Eli_1

#10
Nevermind, I found a CreateThread() example and worked off that and it does exactly what I need. I'll post the code that I used soon as an example to anyone else that doesn't know.

iago

Quote from: Eli_1 on February 11, 2004, 07:01 PM
Nevermind, I found a CreateThread() example and worked off that and it does exactly what I need. I'll post the code that I used soon as an example to anyone else that doesn't know.
Keep in mind there is a lot of overhead stuff happening there, but if you're ok with that then life is good :)
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Adron

Quote from: Eli_1 on February 11, 2004, 07:01 PM
Nevermind, I found a CreateThread() example and worked off that and it does exactly what I need. I'll post the code that I used soon as an example to anyone else that doesn't know.

CreateThread is fine, but you'll eventually run into synchronization issues. Do share your code though, both for others and for review :)

DVX

#13
just my two sense about cin:

cin is worthless in use when you can use an stdio input method for the following reasons:  when cin is called it makes a call to a class, which is slightly more overhead than a function call (not really a relevant concern however), you have much finer control with stdio functions than you do with streams, you avoid the potential defects of cin such as giving cin different input than what it expects from the variable you are writing too (your program won't crash if you use stdio method and give a bad input)

iago

Quote from: DVX on February 19, 2004, 09:28 AM
just my two sense about cin:

cin is worthless in use when you can use an stdio input method for the following reasons:  when cin is called it makes a call to a class, which is slightly more overhead than a function call (not really a relevant concern however), you have much finer control with stdio functions than you do with streams, you avoid the potential defects of cin such as giving cin different input than what it expects from the variable you are writing too (your program won't crash if you use stdio method and give a bad input)

Granted, but not all stdio functions are good either.  gets() and scanf("%s"), for example, can very sasily overflow buffers, but cin >> [std::string]; won't.  

Besides, the issue here isn't whether or not he's using cin, take your complaints about it to a different thread.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*