Simple Loop not blocking

Write your question here.

I am trying to do a simple loop where the user can enter a command and the application fires off an event. My problem is that cin isn't blocking and the loop just keeps going until it crashes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  void loop()
    {
        std::string selection;
        
        cout << "Initializing Manual Sampler Thread" << endl;
        while(true)
        {
            cout << "Command:" << endl;
            //std::string input;
            //cout << "Enter command>" << endl;
            //cin >> input;
            //C//ommandsFSM(input);
            if ( cin >> selection ) {
                cout << selection;
            } else {
                std::cerr << "cin is borked - do something!" << std::endl;
                break;  // no more while(true)
            }
            cout << "<<selection" << selection << endl;
        }
         
            
        
    }
    
What are you talking about?? The loop you made will loop forever. If you're getting input from a file, you should make the if condition based on whether or not you've read till the end of the file.
You have a loop which receives strings as input. It will always be true so there is no reason to break. Similarly line 13 will always be true

You'll have to work out what sequence of key strokes produces an error which seems pretty unlikely to me.

Better to have a selection == "quit" or similar loop breaker.

Beware of while(true), it's often a bad move.

Maybe this is worth considering:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

void loop()
{
    std::string selection;
    std::string prompt{"Command: "};
    
    std::cout << "Initializing Manual Sampler Thread" << '\n';
    
    while(std::cout << prompt and std::cin >> selection and selection != "Quit")
    {
        std::cin.ignore(1000,'\n');
        std::cout << "<<" << selection << '\n';
    }
}

int main()
{
    loop();
    
    return 0;
}
Standard streams will enter an error state (not only) when the end-of-input is reached.

At a terminal, the end-of-input can be signaled by the user by pressing Ctrl-D (on Unix systems) or Return followed by Ctrl-Z followed by Return (on Windows).
I think that this code will work properly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
 *   http://www.cplusplus.com/forum/beginner/266806/
 */

#include <iostream>

// function to convert your command in a case value for a switch statement
int selection(std::string str){
  int s = 100;
  if(str == "command1") return 1;
  if(str == "command2") return 2;
  if(str == "q") return 99;
  return s;
}

int main(){
  std::string com;
  while(com != "q"){
    std::cout << "Enter a command (q to quit): ";
    std::cin >> com;
    switch(selection(com)){
    case 1:
      std::cout << "Do this shit..." << '\n';
      break;
      
    case 2:
      std::cout << "Do the other shit..." << '\n';
      break;
      
    case 99:
      std::cout << "Good bye!" << '\n';
      continue;
      
    default:
      std::cout << "Unknown command. Try again." << '\n';
      break;
    }
  }
  return 0;
}



$ ./test
Enter a command (q to quit): command1
Do this shit...
Enter a command (q to quit): command2
Do the other shit...
Enter a command (q to quit): whatever
Unknown command. Try again.
Enter a command (q to quit): q
Good bye!
I tried this:

void loop()
{
std::string com;
std::cin.clear();
std::cin >> com;
}
where loop is a thread over a function call
there was no blocking on cin >> com;
What do you mean "blocking"? What behavior are you expecting?
"blocking". Where a process or thread stops execution until a certain condition is met.

In this case, it shouldn't execute until it gets input from cin. Instead, it just skips over the cin stream operator like its not there.
So I took it off the thread and executed it as normal and it worked. But why? Why can't I use cin on a thread?
For a thread you might decide to call 'thread_xyz' in main (say), after you start it make the next line thread_xyz.join() if you want to wait for the cin to 'work'.

It does in fact stop and wait for the thread to complete.

https://www.geeksforgeeks.org/multithreading-in-cpp/ is my reference and simply shift the join to suit. (That creates more challenges but appears to solve the immediate issue.)

Your while looping still needs to be reviewed though - 2 alternatives avid runaway's
Topic archived. No new replies allowed.