-2

This question is from HackerRank, I try to use %[^\n]s for a long word. But, the output keep on producing .0

How to replace %[^\n]s to something else for the string to receive the input ?

Here is the input :

12
4.0
is the best place to learn and practice coding!

Here is my output :

16
8.0
HackerRank  .0

This is the expected output :

16
8.0
HackerRank is the best place to learn and practice coding!

This is my full code, as you can see, it does not recognize %[^\n]s. How to solve this problem? Thank you.

Full code :

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

int main() {
    int i = 4;
    double d = 4.0;
    char s[] = "HackerRank ";

    // Declare second niteger, double, and String variables.
    int value1, sum1, value2;
    double e = 2.0, sum2;
    char t[30];

    // Read and save an integer, double, and String to your variables.
    scanf(" %d", &value1);
    scanf("%d", &value2);
    scanf("%[^\n]s", t); //** POINT OF INTEREST **

    // Print the sum of both integer variables on a new line.
    sum1 = value1 + i;
    printf("%d\n", sum1);

    // Print the sum of the double variables on a new line.
    sum2 = d * e;
    printf("%.1lf\n", sum2);

    // Concatenate and print the String variables on a new line
    // The 's' variable above should be printed first.
    printf("%s %s", s, t);

    return 0;
}
Dan Bechard
  • 5,104
  • 3
  • 34
  • 51
Alif Khair
  • 87
  • 1
  • 1
  • 7
  • Please clarify your question. Check [How to Ask](http://stackoverflow.com/help/how-to-ask). – Shubham Aug 19 '16 at 17:33
  • How to replace %[^\n]s to something else for the string to receive the input ? – Alif Khair Aug 19 '16 at 17:35
  • There's probably a newline character in the stream, from after the last number you read. This causes `%[^\n]` to fail since there are no characters to read before the next newline. Also, the `s` is not needed after the `%[]`. – Dmitri Aug 19 '16 at 17:42
  • I haven't downvoted, but here's a tip. When posting code, just paste it right into your question, indented by 4 or more spaces. The site will see it as code, and it will be much easier for everyone to read. – roderick young Aug 19 '16 at 17:49
  • Blindly reading user input of arbitrary length into a fixed-size buffer using `scanf` is just begging for a buffer overflow. `fgets` is probably what you are looking for: http://stackoverflow.com/questions/1252132/difference-between-scanf-and-fgets – Dan Bechard Aug 19 '16 at 18:00
  • `int value2;..scanf("%d", &value2);` --> `double value2;..scanf("%lf", &value2);` then `scanf(" %29[^\n]", t);` – BLUEPIXY Aug 19 '16 at 19:10
  • Also `char t[30];` is small for your example input. – BLUEPIXY Aug 19 '16 at 19:16
  • http://ideone.com/RlqwTn – BLUEPIXY Aug 19 '16 at 19:24

2 Answers2

1

The reason your scanf() is failing to read the string is most likely that there's a newline character still in the stream that wasn't read off after you scanned the last number. "%[^\n]" tries to read a string, containing anything except a newline, and stops when an invalid character is reached; since the next character is a newline, there are no valid characters to read and it fails to assign the field. All you need to do to fix it is read the newline character before you scan the string.

Also, the %[ specifier does not need an s at the end -- it's a different conversion specifier from %s, not a modifier for it.

And finally, it's recommended that you specify the width for %[ or %s so that a long input string won't overrun the buffer you read the string into. The width should be the maximum number of characters to read before the null, so one less than your buffer size.

Using scanf(" %29[^\n]",t) will read off whitespace (including that newline) before scanning the string, and then scan a string with up to 29 non-newline characters (for a 30-char buffer).

Dmitri
  • 9,175
  • 2
  • 27
  • 34
1

Considering your input-output examples, I amended your code like this:

char t[256]; // the string "is the best place to learn and practice coding!" MUST FIT!!!
...
scanf("%d", &value1);
scanf("%lf", &d); // NOT %d, %lf !!! &d or &e - I don't know - depends on you
scanf("\n%[^\n]", &t);
...
printf("%s%s", s, t); // you don't need a space, since your "s" already contains it.

Works fine for me.

UPD: Now it actually works fine.

RedRidingHood
  • 568
  • 3
  • 16