0

I want to take these as input

3
abc def
deg fgh
ghdfete fdgtr dhjgg

The output should be

abc def
deg fgh
ghdfete fdgtr dhjgg

The code that I have written

#include <stdio.h>
int main(){
        int t;
        scanf("%d",&t);
        char a[100];
        while(scanf("%[^\n]%*c",a) == 1){
            printf("%s\n",a);
            --t;
            if(t == 0)
                break;
        }
        return 0;
}

isn't printing anything. Please help.

1
  • How to fix this? Commented May 26, 2018 at 19:53

1 Answer 1

1

consider using fgets for all input.

#include <stdio.h>
int main(){
    char value[50] = "";
    int t;
    int result = 0;
    char a[100];

    do {
        printf ( "enter an integer\n");
        if ( fgets ( value, sizeof value, stdin)) {
            if ( 1 != ( result = sscanf ( value, "%d", &t))) {
                if ( EOF == result) {
                    fprintf ( stderr, "found EOF\n");
                    return 0;
                }
            }
        }
        else {
            fprintf ( stderr, "problem fgets\n");
            return 0;
        }
    } while ( result != 1);

    printf ( "enter text\n");
    while( fgets ( a, sizeof a, stdin)) {
        printf("%s\n",a);
        --t;
        if(t == 0)
            break;
    }
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.