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?