1

I have the folowing code in C++:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

int main(int argc, char **argv)
{
    int N;
    string s;
    char str[100];
    scanf("%d",&N);

    for(int i=0; i<N; i++)
    {
        fflush(stdin);
        fgets(str,100,stdin);
        s = str;
        cout << s << endl;
    }

    return 0;
}

The code is save in an archive test.cpp.

Using the terminal on Linux Ubuntu for compile:

g++ -c hello.cpp
g++ -o hello hello.o

I have an archive test.in that will be the input:

5
God of War
Grand Theft Auto
The Smurfs
Final Fantasy
Call of Duty

Runing the command in terminal:

./test < test.in

The output will be:

rogerio@rogerio-Aspire-5741Z:~/Documentos$ ./teste < teste.in


God of War

Grand Theft Auto

The Smurfs

Final Fantasy

rogerio@rogerio-Aspire-5741Z:~/Documentos$

Because the line "Call of Duty" is jumped?

Deanie
  • 2,316
  • 2
  • 19
  • 35

1 Answers1

3

The

scanf("%d",&N);

doesn't skip beyond end of line, therefore the first fgets(str,100,stdin); just reads the remainder of the line where 5 is given. Note the extra blank line in the output as you have posted it?

laune
  • 31,114
  • 3
  • 29
  • 42