Valhalla Legends Forums Archive | C/C++ Programming | cin question

AuthorMessageTime
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?
February 10, 2004, 1:45 AM
Maddox
[quote author=Eli_1 link=board=30;threadid=5201;start=0#msg43417 date=1076377539]
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?
[/quote]

I know that on unix based operating systems you would use fcntl() but I'm not sure if this works on windows.
February 10, 2004, 2:38 AM
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.
February 10, 2004, 2:57 AM
Adron
I know of no standard way of doing it. In Windows, I'd msgwaitformultipleobjects on among other things a console input handle.
February 10, 2004, 3:42 AM
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.
February 10, 2004, 4:12 AM
Maddox
[quote author=j0k3r link=board=30;threadid=5201;start=0#msg43462 date=1076386370]
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.
[/quote]

Creating a new thread with CreateThread() is not too difficult but it seems like overkill for what he is doing.
February 10, 2004, 2:14 PM
Kp
[quote author=Maddox link=board=30;threadid=5201;start=0#msg43497 date=1076422486]Creating a new thread with CreateThread() is not too difficult but it seems like overkill for what he is doing.[/quote]

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.
February 10, 2004, 3:12 PM
iago
Using a floating textbox might also work for you. Hopefully Adron will see this and give some sort of detail on that one :)
February 10, 2004, 5:51 PM
Adron
[quote author=iago link=board=30;threadid=5201;start=0#msg43524 date=1076435493]
Using a floating textbox might also work for you. Hopefully Adron will see this and give some sort of detail on that one :)
[/quote]

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.
February 10, 2004, 5:59 PM
Eli_1
Ok thanks, I'll look around for that.
February 10, 2004, 7:51 PM
Eli_1
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.
February 12, 2004, 1:01 AM
iago
[quote author=Eli_1 link=board=30;threadid=5201;start=0#msg43744 date=1076547674]
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.
[/quote]
Keep in mind there is a lot of overhead stuff happening there, but if you're ok with that then life is good :)
February 12, 2004, 3:16 AM
Adron
[quote author=Eli_1 link=board=30;threadid=5201;start=0#msg43744 date=1076547674]
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.
[/quote]

CreateThread is fine, but you'll eventually run into synchronization issues. Do share your code though, both for others and for review :)
February 12, 2004, 10:45 AM
DVX
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)
February 19, 2004, 3:28 PM
iago
[quote author=DVX link=board=30;threadid=5201;start=0#msg45034 date=1077204511]
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)
[/quote]

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.
February 19, 2004, 6:01 PM
Skywing
I'd use fgets(stdin, ...) or cin.getline and parse the line out myself with stuff like strchr/strtok/strtoul.
February 19, 2004, 6:16 PM
iago
I would, too. For the assignments I'm marking, most people use either fgets() or getchar(). Both are good.
February 19, 2004, 6:48 PM
Kp
If you're willing to go Windows-specific, you may be able to get much finer control using ReadConsoleInput and parsing the messages yourself. It's more work, but you'll be able to do things like respond to "special" keystrokes in the stream without the user having to press enter (and afaik there's really no advantage to getchar(stdin) over reading the console directly with WinAPI).

Also, DVX: printf ("%s\n", (char *) 1); /* splat with stdio! */
February 19, 2004, 6:51 PM
Eli_1
[quote author=Kp link=board=30;threadid=5201;start=15#msg45060 date=1077216698]
If you're willing to go Windows-specific, you may be able to get much finer control using ReadConsoleInput and parsing the messages yourself. It's more work, but you'll be able to do things like respond to "special" keystrokes in the stream without the user having to press enter (and afaik there's really no advantage to getchar(stdin) over reading the console directly with WinAPI).

Also, DVX: printf ("%s\n", (char *) 1); /* splat with stdio! */
[/quote]

well the reason I'm trying to move away from iostream is because I want to be able to make things on a linux machine also...
February 19, 2004, 7:32 PM
Skywing
[quote author=Eli_1 link=board=30;threadid=5201;start=15#msg45066 date=1077219162]
[quote author=Kp link=board=30;threadid=5201;start=15#msg45060 date=1077216698]
If you're willing to go Windows-specific, you may be able to get much finer control using ReadConsoleInput and parsing the messages yourself. It's more work, but you'll be able to do things like respond to "special" keystrokes in the stream without the user having to press enter (and afaik there's really no advantage to getchar(stdin) over reading the console directly with WinAPI).

Also, DVX: printf ("%s\n", (char *) 1); /* splat with stdio! */
[/quote]

well the reason I'm trying to move away from iostream is because I want to be able to make things on a linux machine also...
[/quote]
You should be able to use STL on Unix...
February 19, 2004, 7:33 PM
Kp
[quote author=Skywing link=board=30;threadid=5201;start=15#msg45067 date=1077219220]You should be able to use STL on Unix...[/quote]

Yes, but many versions of gcc tend to be a bit dumb about templates (fails to properly determine which template functions need to be implemented, so it just implements them all). IMO, you're much better off not mixing gcc and templates if you can avoid it.
February 19, 2004, 8:06 PM
DVX
i was giving my opinion..besides, it doesn't really matter
February 20, 2004, 3:02 AM
Adron
[quote author=Kp link=board=30;threadid=5201;start=15#msg45068 date=1077221181]
[quote author=Skywing link=board=30;threadid=5201;start=15#msg45067 date=1077219220]You should be able to use STL on Unix...[/quote]

Yes, but many versions of gcc tend to be a bit dumb about templates (fails to properly determine which template functions need to be implemented, so it just implements them all). IMO, you're much better off not mixing gcc and templates if you can avoid it.
[/quote]

Is that any problem other than a size issue? If it only affects size, I wouldn't worry about it, but rely on a newer version of gcc appearing before it becomes a problem.
February 20, 2004, 7:39 PM

Search