1

I am trying something like this.Its counting down seconds from 5-1.

 #include<iostream> #include<stdlib.h> #include<windows.h> #include<iomanip> using namespace std; int main() { for(int i=5;i>0;i--) { cout<<"\n\n\n\n\n\n"; cout<<setw(35); cout<<i; Sleep(1000); system("CLS"); } system("PAUSE"); } 

And i trying to figure out a way to break the loop using a user input(from keyboard) to break it while its running. I have no idea about how to do this.I have heard about multi-threading. I don't even know if multi-threading has any application in my situation or not .So any ideas on how to break it in middle while its executing the loop?

8
  • Use a while loop and check for cin at the end of the loop. Commented Feb 2, 2016 at 4:06
  • Could you please elaborate on checking cin? Commented Feb 2, 2016 at 4:17
  • 1
    This might be helpful to you. C++, Real-Time User Input, during While Loop Commented Feb 2, 2016 at 4:30
  • @Wai Yan Sorry but i can't understand what is going on in the answer of your provided reference. Commented Feb 2, 2016 at 5:21
  • Please be more specific in your question. Do you just want to check if the user pressed some key at the end of the loop and if so, break? Or do you want to interrupt your sleep, too? Commented Feb 2, 2016 at 5:33

2 Answers 2

1

I think you will need a thread to do it.

add a global variable in your main loop, use a thread to receive the command line input and modify your global variable.

declare a global variable

bool stop = false; 

create a thread to read stdin and modify 'stop'

DWORD WINAPI thread1(LPVOID pm) { //check the getchar value int a = getchar(); while (a != '0'){ a = getchar(); } stop = true; return 0; } 

in your main:

HANDLE handle = CreateThread(NULL, 0, thread1, NULL, 0, NULL); for(int i=5;i>0 && !stop;i--) { cout<<"\n\n\n\n\n\n"; cout<<setw(35); cout<<i; Sleep(1000); system("CLS"); } 
Sign up to request clarification or add additional context in comments.

4 Comments

I was able to create a thread by using suggestions thanks to that but i still wasn't able to break out of the loop.
can you successfully set the global variable? how's your loop condition?set a breakpoint to see where is the program halts.
I think the while loop in the thread1 function is not breaking and stop is not being updated.I tried some ways to break it when a is being updated but couldn't do .
sounds like your 'stop' is not declared as a global variable.Whatever, I think you should first study about threading, see tutorialspoint.com/cplusplus/cpp_multithreading.htm
0
  • Try this:

    #include<iostream> using namespace std; int main() { int i = 1; while(i) { // do your work here cin>>i; } cout<<"Terminated"<<endl; } 

    Continue scanning int i, when you want to break loop give 0 as input value and your loop will be terminatd.

3 Comments

The questions is about how to detect user input from the user while displaying values in a loop (and break out of the loop under some input condition), not how to collect input from the user in a loop.
I think anjanik012 inly wants to break loop as he mentioned in comment
Yes, he wants to break the loop while displaying i, not while getting input for i (or even halting for user input, hence why threading was mentioned). Very different.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.