0

I have to do this program for school were I read 3 ints check some condition and print the result. The problem is that the public test case shows something like this:(I just put some arbirtary numbers)

input:        ouput:      
  1 2 3       1
  7 1 2       0
  6 2 3       0

And some other test cases only show one line:

input:        ouput:  
  6 1 1       1

So, sometimes the input consists of a single line (one single output line) and sometimes of multiple lines (multiple output lines). The code its something as simple as this:

int main(){
   int a, b, c;
   cin << a << b << c;
   if(check(a, b, c)) cout << "1\n";
   else cout << "0\n";
   return 0;
}

I guess I'm supposed to put the whole input/ouput thing in a loop, so I can do it multiple times. But, when do I know I can stop recieving input and exit the program? How many inputs I'm expecting? How do I know the input is over?

Pau C
  • 773
  • 4
  • 20
  • 1
    Possible duplicate of [How do I know when one is done entering cin with \n? (loop)](https://stackoverflow.com/questions/3888197/how-do-i-know-when-one-is-done-entering-cin-with-n-loop) – wimh Sep 29 '17 at 10:23
  • You could use std::getline in a loop: `std::string line; while (std::getline(std::cin, line)) { parse input; do the check; [...] }` Also don't rely on the input correctness. You should always check if the input meets some given requirements. – ProXicT Sep 29 '17 at 10:23
  • I don't think this is possible. All the reading methods I know of will either keep reading from stdin or keep waiting for input. There is no EOF like character in stdin that you can check to determine input termination. That I think is why most programming competitions provide the number of inputs beforehand. – gautam1168 Sep 29 '17 at 10:26
  • The thing is the input is `a_b_c\n` multiple times, there isn't a "last line" or "empty line". – Pau C Sep 29 '17 at 10:30
  • 1
    @gautam1168 On the contrary, https://stackoverflow.com/a/3197123/4944425 – Bob__ Sep 29 '17 at 14:20
  • Hey then this should be as easy as checking for this EOF character, shouldn't it? – gautam1168 Sep 30 '17 at 15:04
  • Hey then this should be as easy as checking for this EOF character, shouldn't it? – gautam1168 Sep 30 '17 at 15:04
  • @Bob__ I did not understand your comment...from your link "So does it means that stdin don't have EOF and we have to insert them manually using Ctrl+Z or Ctrl+D? Actually -- yes. One may consider stdin (not redirected, but taken from the console) as infinite file -- no one can tell where does it end" you have EOF only if you redirect a file, as "myprogram < file" – n3mo Oct 02 '17 at 07:22
  • 1
    @n3mo Technically, typing Ctrl+Z or Ctrl+D sends EOT, resulting in end of stream. Futher readings with `operator>>` will fail as the eofbit flag is set. So, with `while ( std::cin >> a >> b >> c ){...}`, you will stop reading when the redirected input file (if it's the case) ends or (if interactive) when the user inputs something wrong or EOT. – Bob__ Oct 02 '17 at 08:18
  • ah ok @Bob__ it was a general answer, not related with the problem of the guy (if the test program does not send EOT). I understand it now. Interesting, thanks for the explanation ;) – n3mo Oct 02 '17 at 09:07

1 Answers1

-1

Since it seems the program is quick, and I suppose also the input are sent fast, I would use a timeout.

You can use file descriptor and a select() function instead of cin

Have a look here and the links therein. Hope it can be a suitable solution.

Documentation of select() function for linux and Windows

Example of code for Linux, inspired to the link above (not tested)

int main() {
    while (1) {
        fd_set fileDescriptor;
        struct timeval tv;
        tv.tv_sec = 10; // wait 10 secs
        tv.tv_usec = 0;
        FD_ZERO( &fileDescriptor ); // reset file descriptor
        FD_SET( fileno( stdin ), &fileDescriptor ); // connect the file descriptor to standard input
        int result = select( fileno( stdin )+1, &fileDescriptor, NULL, NULL, &tv ); // wait to read a character
        if( result > 0 ) {
            char c;
            read( fileno( stdin ), &c, 1 ); // read only one character
            //////////////////////
            // here your code:
            // take three characters
            // (you can use a buffer char* buffer = new char[length] instead of char - remember to delete it with delete[] buffer)
            // check if the character is '\n' to divide lines
            // check the three characters and write the output
            //////////////////////
        } if ( result < 0) {
            return -1; // select error
        } else { // result == 0
            break; // timeout expired, no more input supposed
        }
    }
    return 0;
}
n3mo
  • 663
  • 8
  • 23
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/17484884) – zedfoxus Sep 29 '17 at 14:58
  • The question is "How many inputs I'm expecting? How do I know the input is over?" and my (implicit) answer is "You could use the select() function with a timeout". What is wrong with that? (I edit the answer to make it clearer maybe) – n3mo Sep 29 '17 at 15:04
  • 1
    With your recent edit, you have done a good job of adding clarity. Thank you for doing so! Could you take some examples from the link you provided and form a more descriptive answer (if you have the time)? If that link goes away, your answer will help future readers. Also, I haven't downvoted you as I think you are really trying to help the OP well. – zedfoxus Sep 29 '17 at 15:51
  • Thanks. I will add an example as soon as possible ;) – n3mo Sep 30 '17 at 13:38