4

Writing a program for class, restricted to only scanf method. Program receives can receive any number of lines as input. Trouble with receiving input of multiple lines with scanf.

#include <stdio.h>
int main(){
    char s[100];
    while(scanf("%[^\n]",s)==1){
        printf("%s",s);
    }
    return 0;
}

Example input:

Here is a line.
Here is another line.

This is the current output:

Here is a line.

I want my output to be identical to my input. Using scanf.

John
  • 139
  • 2
  • 3
  • 10
  • possible duplicate of [Reading multiple lines of input with scanf()](http://stackoverflow.com/questions/13592875/reading-multiple-lines-of-input-with-scanf) – user2284570 Mar 24 '15 at 21:23

4 Answers4

10

I think what you want is something like this (if you're really limited only to scanf):

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

The %*c is basically going to suppress the last character of input.

From man scanf

An optional '*' assignment-suppression character: 
scanf() reads input as directed by the conversion specification, 
but discards the input.  No corresponding pointer argument is 
required, and this specification is not included in the count of  
successful assignments returned by scanf().

[ Edit: removed misleading answer as per Chris Dodd's bashing :) ]

Michael
  • 3,151
  • 2
  • 22
  • 27
  • +1 for the correct first answer, -1 for the totally wrong and misleading second answer. Why to people keep recomming `while(!feof)`? A sure-fire indicator that the programmer doesn't know what they're doing. – Chris Dodd Jan 24 '13 at 05:32
  • How can I eliminate the extra linebreak (\n) on the last line of output? – John Jan 24 '13 at 06:34
  • @ChrisDodd, yeah you're correct, I've edited my answer to remove the misleading bit. – Michael Jan 24 '13 at 06:49
  • @Michael : Ok, and what would be the regex if I need to read an integer or a float? – user2284570 Mar 24 '15 at 23:47
  • You should probably also add a maximum length to the scanf string specifier to prevent buffer overflows when input is greater than 99 characters: `scanf("%99[^\n]%*c",s)`. – Dan Bechard Aug 20 '23 at 06:00
6

try this code and use tab key as delimeter

#include <stdio.h>
int main(){
    char s[100];
    scanf("%[^\t]",s);
    printf("%s",s);

    return 0;
}
Puneet Purohit
  • 1,243
  • 6
  • 23
  • 42
1

I'll give you a hint.

You need to repeat the scanf operation until an "EOF" condition is reached.

The way that's usually done is with the

while (!feof(stdin)) {
}

construct.

Thrill Science
  • 408
  • 4
  • 14
  • 1
    -1: `while(!feof(..))` is pretty much always wrong and always gets the last line wrong unless you go to heroic efforts to post-process it. – Chris Dodd Jan 24 '13 at 05:32
0

Try this piece of code.. It works as desired on a GCC compiler with C99 standards..

#include<stdio.h>
int main()
{
int s[100];
printf("Enter multiple line strings\n");
scanf("%[^\r]s",s);
printf("Enterd String is\n");
printf("%s\n",s);
return 0;
}