Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
* * * * * 1 votes

C++ output without moving the console cursor

console

  • Please log in to reply
7 replies to this topic

#1 EtaCarinae

EtaCarinae

    CC Lurker

  • New Member
  • Pip
  • 3 posts
  • Programming Language:C++
  • Learning:C++, Python

Posted 05 October 2012 - 08:51 PM

I am doing a program that has a timer running while the user outputs answers. I managed to do this with multithreading, but when it comes to the user input...the timer always puts the console cursor in it's location. I made it to return in back where it was... but at every tick it disrupts input longer than 1 string. So my question is - Is it possible to output something in the console without moving the console cursor there?

Thanks.
  • 0

#2 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 06 October 2012 - 05:17 AM

I am wondering that too, do you have the source code so we can do test some theories on the matter ?
  • 1

#3 EtaCarinae

EtaCarinae

    CC Lurker

  • New Member
  • Pip
  • 3 posts
  • Programming Language:C++
  • Learning:C++, Python

Posted 06 October 2012 - 07:17 AM

Yes.

The first time someone that cares!
(4th try in 4th forum)
I will post the code in matter of minutes.

Here link with all the project files. +Compiled program.

I know my code looks lame, but I learn all by reading.
The most interesting parts of the code are at the first 60 lines and at the end (the multithreaded function for the timer).

http://dox.bg/files/dw?a=95bae741da

You can delete the unnecessary code and make a simple program to try with... I won't give up easy! (Until I find easy way to output text with OpenGL so I can make this ** there)
  • 0

#4 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 06 October 2012 - 12:29 PM

Ok, i am downloading it, lets see what happens :D
  • 1

#5 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 06 October 2012 - 12:59 PM

I found that in line 1972, which is inside the timer, you repositionates the cursor every time the timer works

    cCoord.X = /*C-2*/ 25;
    cCoord.Y = 15;
    SetConsoleCursorPosition(hStdout, cCoord);
    Sleep(1000);

I changed C-2 by 25, so you can see how now it repositions to X=25 instead of the start of the line,
What you need to do is to calculate the length of the input and position the cursor one place after :D, like in : cCoord.X = C+inputStrLen;

This is the first time i see this problem in the forum, are you sure you have posted it here before ?

I want to believe it was in another forum pages where you didnt get any answer, here in codecall every S.O.S usually gets answered, so give us some credit EtaCarinae and since i see you are just starting here i give you my welcome and suggest you to go to the introductions forum and present yourself to the community (if you didn't before, lol). happy coding!
  • 1

#6 EtaCarinae

EtaCarinae

    CC Lurker

  • New Member
  • Pip
  • 3 posts
  • Programming Language:C++
  • Learning:C++, Python

Posted 06 October 2012 - 09:00 PM

No wasn't in this forum. in 3 different forums I've tried.
So there's no way to output without moving the cursor?


Anyway thank you for the help, like in my second post here, I said the first time someone cares :D.


---After try----
If I make such function it calculates the input after pressing 'Enter'.
In the code you have I made a pseudo function that does exactly what you suggested. This 'C' that is global variable. It's value increases every time the user enters text. The code at the end is meant to be C-2; And to be at the start of the page. And if you input for example A4 - the cursor will move waiting for the next answer.
But you got to be faster than 1 sec, because the timer disrupts you. The program still counts the A4 and does what it has to do, but the A is where it has to be, the timer ticks, and moves the cursor again on the place of the A. And when you put the '4' it replaces the A. Thats my problem. (You can make the timer ticking more than 5 secs - 300 as it has to be :) ) and try it. If there is a function that counts every time you press a button on the keyboard,a new problem will appear - if someone mistakes presses backspace and here you have +2 and everything goes to nowhere...

P.S. May be I really have to use openGL for the timer - the openGL window is separate from the console one, can be postioned where it has to go and resized all from the code from your own program. But ;) always a but, outputting text in openGL is a nightmare :( . But I can't see openGL section here :(.
  • 0

#7 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 07 October 2012 - 03:19 AM

Another option can be using getchar() or similar ( even keyboard interruption ) to take the input instead of scanf, and in fact, it wouldn't be bad if you print the input history in one line, and gets the actual input in another fixed place ;)
you can also use getche that does not echoe the input on the screen ;)
  • 1

#8 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 08 October 2012 - 09:42 AM

Have you tried using WriteConsoleOutputCharacter() instead of printf()? I'm pretty sure that doesn't move the cursor. However, it won't accept format specifiers, so you'll have to use sprintf() to format your output and then pass that to WriteConsoleOutputCharacter(). Since you're using C++, I would recommend using a string buffer instead, like this:

#include <windows.h>
#include <sstream>

std::ostringstream outbuf;
COORD cWhere;
DWORD dwCharsWritten; // <-- this might not be necessary

outbuf << myvar << " blah blah " << -193 << endl;

cWhere.x = 5;
cWhere.y = 9;

// You might be able to pass in NULL if you don't want to keep track of the
// number of characters written. Some functions allow you to do this, others
// don't. I'm not 100% sure about this one, the documentation doesn't say.
WriteConsoleOutputCharacter(hStdout, outbuf.str(), outbuf.str().length(),
        cWhere, &dwCharsWritten);

  • 1

sudo rm -rf / && echo $'Sanitize your inputs!'






Also tagged with one or more of these keywords: console

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download