-3

So I was trying to solve the FizzBuzz challenge in various languages using the string method to improve the code. I got stuck in C because things work differently here. This is my code, I'm getting errors, can anyone explain them to me and help to get the correct code.

#include<stdio.h>
#include<string.h>
int main()
{
    int i,n;
    char output;
    printf("Enter Range: ");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        if(i%3==0)
            strcat(output,"Fizz");
        if(i%5==0)
            strcat(output,"Buzz");
        if(output=="\0")
            strcat(output,i);

        printf("\ni");
    }
    printf("\nEnd.\n");
    return 0;
}

Thanks.

G. Sliepen
  • 7,637
  • 1
  • 15
  • 31
Ashwini
  • 15
  • 6
  • 1
    What errors are you getting? – dunck Sep 23 '18 at 06:46
  • 1
    `output` is a `char` i.e. one character, not a string. None of your strcat calls are good. You should go back to a good C book or tutorial on strings and printing, one of your printfs is suspicious too. – Mat Sep 23 '18 at 06:49
  • 1
    Your compiler should warn about `char output; ... strcat(output,"Fizz");` when fully enabled. What compiler are you using? – chux - Reinstate Monica Sep 23 '18 at 06:50
  • 1
    GCC. errors: FizzBuzz_Modified.c:16:18: warning: result of comparison against a string literal is unspecified (use strncmp instead) [-Wstring-compare] if(output=="\0") ^ ~~~~ FizzBuzz_Modified.c:17:27: warning: incompatible integer to pointer conversion passing 'int' to parameter of type 'const char *' [-Wint-conversion] strcat(output,i); ^ /usr/include/secure/_string.h:131:33: note: expanded from macro 'strcat' __builtin___strcat_chk (dest, __VA_ARGS__, __darwin_obsz (dest)) ^~~~~~~~~~~ – Ashwini Sep 23 '18 at 07:03
  • when I try to run, I get the output as: i I i and abort trap:6 – Ashwini Sep 23 '18 at 07:04
  • 1
    Don't try to run this code until you fix the warnings. You need pass the address of a character array to `strcat`. You're not. So, declare an array, and pass it. Look at the man page for `strcat`, and fix your arguments. – Tom Karzes Sep 23 '18 at 07:11
  • You cannot compare string literal with char `if(output=="\0")` is wrong. Try `if(output=='\0')` – kiran Biradar Sep 23 '18 at 07:12

1 Answers1

0

@Ashwini Singh

There are some mistakes in your code ,

1) You declared the output variable as char datatype & concatenate string in Fizz/Buzz in char . so how can be value of string(which is a array of character) placed in character output.

2) You are concatenate the integer value i with character output e.g strcat(output,i) . we need to first typecast the integer value i in to char/string datatype & then concatenate with output.

The condition of the FizzBuzz programs are,

1) If number is multiple of 3 then add Fizz in the resultant string

2) If number is multiple of 5 then add Buzz in the resultant string

3) If number is neither multiple of 3 nor multiple of 5 then add number in the resultant string

Code :

#include<stdio.h>
#include<string.h>
int main()
{
int i,n;
char output[100]=" ";
printf("Enter Range: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
    char s[] = {'0' + i, '\0'};
    if(i%3==0)
        strcat(output,"Fizz ");
    else if(i%5==0)
        strcat(output,"Buzz ");
    else
        strcat(strcat(output,s)," ");
}
puts(output);
return 0;
}

Output :

Enter Range: 10                                                                  
 1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 
Usman
  • 1,983
  • 15
  • 28