tags:

views:

158

answers:

3

hi I would like to ask how I would modify this code for the question: (It only accepts one input then prints it out. I want it to keep going until I hit enter (\n) twice.

#include <stdio.h>

#define MAXLENGTH 1000
int main(void) {
    char string[MAXLENGTH];

    fgets(string, MAXLENGTH, stdin );
    printf("%s\n", string);

    return  0;
}

I'm confused at the fgets(string, MAXLENGTH, stdin ); line, what does stdin mean/do?

EDIT: Chris, I've tried your way:

    #include <stdio.h>

#define MAXLENGTH 1000
int main(void) {
    char string[MAXLENGTH];


    do {
    if (!fgets(string, MAXLENGTH, stdin ))
        break;
    printf("%s", string);
    }
} while (string[0] != '\n');


    return  0;
}

It prints after i hit enter but i want to type the whole list first then allow it to print the list after I press enter twice.

+2  A: 
do {
    if (!fgets(string, MAXLENGTH, stdin ))
        break;
    printf("%s", string);
} while (string[0] != '\n');

will keep reading input and printing it until it sees a blank line (hitting enter twice in a row) or until EOF.

stdin refers to the program's standard input, which is whatever input source it is connected to when you run it. If you're just running it at the command line with no extra shell redirections, that will be the keyboard.

Chris Dodd
Chris, I've edited my post it doesn't printf anything but fgets works fine.Also any other methods? I can't use any break (schoolwork), and what does !fgets mean?
wello horld
@wello horld: you've put the `printf` inside the `if` body. Do it exactly how Chris has written.
codaddict
Oh I see now, thanks.But I want it to print after I type the whole list... any thoughts?I've tried putting putting the printf after the while line, but no luck..
wello horld
+1  A: 

If you want to make entire input to be printed after the return key is pressed twice you can do:

char string[MAXLENGTH];     // to hold a single input line.
char strings[MAXLENGTH]=""; // to hold the entire input lines.
do {

    if (fgets(string, MAXLENGTH, stdin ) == NULL)
        break;
    strcat(strings,string);
} while (string[0] != '\n');
printf("%s", strings);
codaddict
I got an "incompatible implicit declaration of built-in function 'strcat' for that...
wello horld
Add `#include <string.h>` at the top after `#include<stdio.h>`
codaddict
bloody awesome, it works now! the last 3 hours of my life finally have meaning, thanks!btw, I'm going to lose marks if I use break... any way to replace this?
wello horld
@wello - Anyone who deducts points for using a `break` statement to correctly and cleanly control flow is out of his/her mind and adhering blindly to rules he/she doesn't understand. You can do it if you have to, but honestly it's cleaner and better code if you just leave it in.
Chris Lutz
yeh, its my uni :(
wello horld
+1  A: 

Try this:

#include <stdio.h>
#include <string.h>

#define MAXLENGTH 1000
int main(void) 
{
    char string[MAXLENGTH];

    int i = 0;
    for(;;++i)
    {
        string[i] = getchar();
        if (i > 0 && string[i] == '\n' && string[i-1] == '\n') break;                
    }

    string[i] = 0;

    printf("Print it again:\n%s",string);

    return  0;
}
Michael Buen
thanks, this works too.Which is method is better though?, I now need to modify the list so it prints with a number in front.
wello horld
This will overflow the buffer if the full input exceeds MAXLENGTH...
Chris Dodd
@Chris Dodd: Yeah I know, just like all other answers here
Michael Buen