2

Dusting off my C cobwebs here using a site called HackerRank... the challenge here is to read 3 different inputs from stdin and then print out altered data.

Input

The first line contains an integer

The second line contains a double

The third line contains a string / sentence

Output

integer input + variable i

double input + variable d

variable s + string input

Seemed pretty straight forward, I'd use scanf for the integer and double then fgets for the string since scanf would terminate after the first space.

My problem is, doesn't seem like fgets is filling the buffer, but I'm unsure whether or not it could be the sites compiler or just my lack of knowledge.

int i = 4;
double d = 4.0;
char s[] = "HackerRank ";

// Declare second integer, double, and String variables.
int singleNum;
double doubleNum;
char buffer[256];
char outputString[300];

// Read and save an integer, double, and String to your variables.
scanf("%d", &singleNum);
scanf("%lf", &doubleNum);
fgets(buffer, 256, stdin);

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

// Print the sum of the double variables on a new line.
doubleNum += d;
printf("%.1f\n", doubleNum);

// Concatenate and print the String variables on a new line
strcat(outputString, s);
strcat(outputString, buffer);

printf("%s", outputString);
// The 's' variable above should be printed first.

However, when I do this, buffer is always empty. If I were to use scanf I would at least get the first word front the string input.

Not super concerned about memory usage here, just trying to complete the problem to work within fixed parameters.

So, my question is - am I doing something wrong here?

My Output:

Input (stdin)

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

Your Output (stdout)

16
8.0
HackerRank 

Expected Output

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

Compiler Message

Wrong Answer
Adjit
  • 10,134
  • 12
  • 53
  • 98

3 Answers3

2

However, when I do this, buffer is always empty. If I were to use scanf I would at least get the first word front the string input.

The problem is that white space ('\n' entered at the end of scanning double number ) into the buffer is getting consumed

instead consume white space using scanf(" "); before scanning in buffer

scanf(" ");
fgets(buffer, 256, stdin);

Is there a way to include the newline character in the scanf statement so I don't need an extra one?

  • yes you can further simplify above two statements into :

    scanf(" %255[^\n]",buffer); //consumes and scans into buffer
    
  • or you could also :

    scanf("%lf\n", &doubleNum); //consume at the end
    fgets(buffer, 256, stdin); //scan into buffer
    
Cherubim
  • 5,287
  • 3
  • 20
  • 37
  • 1
    That did it, thanks! Is there a way to include the `newline` character in the `scanf` statement so I don't need an extra one? – Adjit Jul 11 '16 at 15:25
1

One visible problem here is that the outputString is declared, but not initialized:

char outputString[300];

I assume, that it is declared in block scope, so it contains trash values, whatever is on the stack. This may confuse strcat, which expects it to be NUL terminated:

strcat(outputString, s);

The fix would be add following line before the strcat call:

outputString[0] = '\0'; 
Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
  • still doesn't help - `outputString` isn't what was giving me a problem. If I used `scanf` I would get the first word, so the output would be `HackerRank is` when I use `fgets` the ouptut is just plain `HackerRank` – Adjit Jul 11 '16 at 15:15
  • @Adjit: How do you know you don't have _multiple_ problems? – Lightness Races in Orbit Jul 11 '16 at 15:20
  • @Adjit: It might be also that `scanf` leaves the end of line character, so the `fgets` receives empty line. Try with `getchar()` between them to "eat-up" the newline character. – Grzegorz Szpetkowski Jul 11 '16 at 15:22
  • 1
    Yes, cherubim appears to have been correct. Is there a single line initializer for the `outputString`? would it have been better to use `malloc` to allocate the size of it? – Adjit Jul 11 '16 at 15:24
  • 1
    @Adjit: Yes, you could initialize it with `char outputString[300] = {0}`, so it has all zeros. There is no need to use `malloc` unless the size of buffer is significantly large or unknown at compile time. For small buffers like that, it pretty common to allocate them on the stack. – Grzegorz Szpetkowski Jul 11 '16 at 15:27
0
I have solved the Hackerrank C dataType First day challenge problem without using strcat function. Below is the solution to this problem.  

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

// Declare second integer, double, and String variables.`enter code here`    
     int i1=0;
     double d1=0.0;
     char name[100];

    // Read and save an integer, double, and String to your variables.

     scanf("%d",&i1);
     scanf("%lf",&d1);
     getchar(); // Used this function to remove the '\n'from double. 
     scanf("%[^\n]s",name);

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

    // Print the sum of the double variables on a new line.
        printf("%0.1lf\n",d+d1);   

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

    return 0;
}