+ 4

Running two functions simultaneously... How to implement this?

I was creating a simple menu in a console window with just some colors and decorations. I then decided to add a marquee as a sub header which would run during the program. This was my function, which works perfectly... void DisplayMarquee(int y,string var,int frames) { string a = ""; a = var; int olen = a.length(); int time = static_cast<int>(1000.0/frames);; for(int i=0;i<80-olen;i++) a+=" "; while(true) { SetCursorAt(0,y); cout<<a; Sleep(time); char ch = a[a.length()-1]; a.resize(79); a = ch + a; } } Now, I wished to read the input option for the menu, along with the marquee running parallel in the menu, just like in HTML. So I tried using C++11 threads, in the following way: #include<iostream> #include<string> #include<thread> #include<windows.h> using namespace std; void SetCursorAt(int X,int Y) { COORD CONSOLECOORD; CONSOLECOORD.X=X; CONSOLECOORD.Y=Y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),CONSOLECOORD); } void DisplayMarquee(int y,string var,int frames) { string a = ""; a = var; int olen = a.length(); int time = static_cast<int>(1000.0/frames);; for(int i=0;i<80-olen;i++) a+=" "; while(true) { SetCursorAt(0,y); cout<<a; Sleep(time); char ch = a[a.length()-1]; a.resize(79); a = ch + a; } } void f2() { int a; SetCursorAt(15,20); cout<<"Enter - ";cin>>a; } main() { thread t1(DisplayMarquee,2,"Hello",20); thread t2(f2); t1.join(); t2.join(); } This code runs, but is unable to take input properly (reads just one digit) and prints messy output. So, what all do I need to add or change to make it work like I want it to?

17th Aug 2017, 1:02 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
10 Respostas
+ 6
@Kinshuk One way I can think of is to print character arrays instead of the entire string by specifying begin coord. It should be something like: void function() { int x = 0, y = 0; std::string str = "some text"; for (int i = 0; i < str.length(); i++) { set_coord(x, y); std::cout << str[i]; x++; } } // This way, every coordinate for every character in the string is specified. // This *may* fix the jumbled output problem, but the marquee animation may not be smooth as expected.
18th Aug 2017, 12:44 PM
Hatsy Rei
Hatsy Rei - avatar
+ 8
It won't work because your cursor can only be at one place at one time. The instructions execute simultaneously, so there's no way to predict where your cursor would be unless you specify every single output coordinate.
17th Aug 2017, 3:01 PM
Hatsy Rei
Hatsy Rei - avatar
+ 6
@Hatsy Rei So is there another way to implement this in C++? HTML can run marquees and take input at the same time, probably because it doesn't use continuous prints, but animations... Thank You!
18th Aug 2017, 12:24 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 4
Yes, thats what I wish to do. But I can't get it to work properly, as the outputs get mixed up.
17th Aug 2017, 1:22 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 4
@Hatsy Rei Thank You very much!
18th Aug 2017, 2:00 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
No, they are independent of each other...
17th Aug 2017, 1:27 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
How do you do the same in Java? If you can tell me that, I may try finding something similar which can be done in C++... Thank You!
17th Aug 2017, 1:32 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
implement mutithreading. fit whatever code you want to run concurrently in a separate thread. that's what you meant by running two functions simultaneously right? oh, I saw you had already done that. my knowledge of cpp isn't good. idk :(
17th Aug 2017, 1:09 PM
Gao Xiangshuai
Gao Xiangshuai - avatar
+ 1
does your 2nd thread depend on the input from the first thread?
17th Aug 2017, 1:25 PM
Gao Xiangshuai
Gao Xiangshuai - avatar
+ 1
hm bummer, my knowledge of my c++ is beginner. in Java, I can help debug a bit but c++ I have no clue.
17th Aug 2017, 1:30 PM
Gao Xiangshuai
Gao Xiangshuai - avatar