0
votes

My problem is as follow: I need scanf to read multiple lines of user input. The user can input any combination of characters separated by whitespace or newline characters. My objective is to take each input that is separated by whitspace or newlines, and process it and output via printf. However, I am not allowed to store multiple inputs at once. My issues is this: every time the user presses enter, the scanf reads the entire line since I have scanf in a while loop != EOF. However, I don't want the scanf to read all lines of input until the user presses EOF. Is this possible? Here are some examples below:

Some pseudo code:

User Input:
cat dog mouse rabbit
snake dog
pink bob
joke*/

//Some pseudo code:

char input[100];
while (scanf("%s", input) != EOF) {
    printf("%s", input);
}

In summary, I don't want scanf to read when the user presses enter since if it does, then the printf will activate but still allow the user to input stuff in. Is there a way to bypass this? Note: I have to use scanf and I can't store multiple separate inputs (like dog or cat) in a single array. Thank you.

1
Using %s with scanf() says "I don't care about white space". If you do care about white space, you probably shouldn't be using scanf() at all and certainly shouldn't be using %s. You might be using %[…] (a scan set) or %[^…] (a negated scan set), but even that's problematic.Jonathan Leffler
Who says you have to use scanf? Is this some kind of homework, with constraints? I'm a professional C programmer, I've been a professional C programmer for 35 years, and not only would I not use scanf to solve this problem, I never use scanf at all. It's usually significantly harder to do any kind of interesting input processing with scanf than it is to use other, better techniques. (If this is a homework assignment and you have no choice, you have my sympathy.)Steve Summit
Per comments (on an answer I am about to delete), the task is to read all input before writing any output. Clearly, this is impossible without storing some data: At any point after the input is read and before the output, the program must contain the information necessary to produce the output. This information could be a copy of the input or some information produced from processing the input, but it must exist within the program state. If the program cannot store “multiple inputs,” then it must have what information is to be derived from it in order to produce the output.Eric Postpischil
Given the above, the problem statement should explain what is to be done with the input. E.g., if the task is to sum the length of the words, and it is allowable to store the length so far but not to store the words, then a solution can be found.Eric Postpischil

1 Answers

0
votes

Just use a 2-dimensional array, where each row stores a line of the input string. And as for accepting the input, ask the user to enter the number of lines which he/she would be giving and just loop through it.

#include<stdio.h>
void main(){
    char inp[10][50];
    int i,j,n;
    printf("\nenter the number of lines: ");
    scanf("%d",&n);
    for(i=0;i<n;i++){
        scanf(" %[^\n]s",inp[i]);
    }
    //for printing it out.. follow the same with printf
    printf("\nthe strings are:\n");
    for(i=0;i<n;i++){
        printf("%s\n",inp[i]);
    }
}