-1

I tried to solve a problem yesterday in c++. The problem was a famous one called "Fizzbuzz". It will take a number as input. For each multiple of 3, print "Fizz". For each multiple of 5, print "Buzz". For numbers which are multiples of both 3 and 5, print "FizzBuzz" instead of the number. If a number which is not multiples of 3, 5 or both it will print the number instead.

An example: If I give 5 as input it will show output like this: 12Fizz4Buzz. But the problem is this judge has a template code like the following:

#include<bits/stdc++.h>
using namespace std;
int FizzbuzzFunc(int num)
{

return 0;
}
int main()
{
int number;
cin>>number;
cout<<FizzbuzzFunc(number);
return 0;
}

Whenever I tried to solve it by putting my code into that "FizzbuzzFunc" It is printing an extra 0 at the end of the output. An example if my code seems like this:

#include<bits/stdc++.h>
using namespace std;
int FizzbuzzFunc(int num)
{

for(int i = 1; i <= num; i++)
{
if (i % 3 == 0 && i % 5 == 0)
{
cout<<"FizzBuzz";
}
else if(i % 3 == 0)
{
cout<<"Fizz";
}
else if(i % 5 == 0)
{
cout<<"Buzz";
}
else
{
cout<<i;
}
}

return 0;
}
int main()
{
int number;
cin>>number;
cout<<FizzbuzzFunc(number);
return 0;
}

and take input as 5 then the output will look like this: 12Fizz4Buzz0. How can I remove that extra 0 from the output?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 3
    A debugger is your friend. – doug Sep 23 '19 at 04:00
  • @doug I already debugged and understand it is printing as a result of "cout" statement but I can't find any way to escape it. – Muhammad Ariful Islam Sep 23 '19 at 04:15
  • Call `exit(0);` immediately before the `return 0;`. That avoids the output in `main()`. It's a cheat, but expecting `cout << 0` (where the `0` is the value returned by the function) to do anything other than print `0` is fairly futile — and exiting instead of returning avoids that. – Jonathan Leffler Sep 23 '19 at 04:53
  • @JonathanLeffler [Why is using exit() considered bad?](https://stackoverflow.com/questions/25141737/) – Remy Lebeau Sep 23 '19 at 07:12
  • @RemyLebeau the linked question assumes you want to write sensible and maintainable code. The OP merely wants to satisfy some kind of convoluted automated code judging system here. – tangrs Sep 23 '19 at 07:55
  • @RemyLebeau — I said using `exit()` was a cheat. It’s only merit is that it prevents the zero being printed. – Jonathan Leffler Sep 23 '19 at 14:07

2 Answers2

3

If the function is declared to return a value, it must return a value.

Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function.

But the reason your code is printing the extra 0 is because you told it to:

cout<<FizzbuzzFunc(number);

Don't use cout, just call the function.

FizzbuzzFunc(number);

Also, if the function doesn't need to return a value, you can set its return type to void.

void FizzbuzzFunc(int num)

from which you can with return;

Humphrey Winnebago
  • 1,512
  • 8
  • 15
  • I can't change the judge template code as I told earlier. I already tried what you told me. But the problem is that template and I have to call like that. – Muhammad Ariful Islam Sep 23 '19 at 04:20
  • Well that sounds poorly written on the part of the driver code. Can you change the signature of your function? To a `char` maybe? `char FizzbuzzFunc(int num)` – Humphrey Winnebago Sep 23 '19 at 04:23
  • No, I can't do anything. That's the main problem. If I change something it didn't accept the code. – Muhammad Ariful Islam Sep 23 '19 at 04:26
  • Are you sure you can't change the template code? Most code judging systems I've encountered only care about the stdout output. You could always tack a exit(0) at the end of your FizzbuzzFunc if you really wanted to work around it. – tangrs Sep 23 '19 at 04:27
  • What does the system say exactly when it rejects your code? It could be rejecting it for other reasons. For example, perhaps they want newline separators rather than the space separator you used (totally shooting in the dark here). – tangrs Sep 23 '19 at 04:28
  • It's actually a test on a site and it replies submission failure. – Muhammad Ariful Islam Sep 23 '19 at 04:30
  • What it says exactly depends on the compiler and also how the driver code is expecting your function to be declared (if it is). It would likely come out as a linker error – Humphrey Winnebago Sep 23 '19 at 04:31
  • Try changing the return type to `char`, then return a space or newline – Humphrey Winnebago Sep 23 '19 at 04:34
  • Oh my bad, I pasted code that I practice myself. In here space isn't an issue in the output formate. I already edit please see it and i can't change the function type. – Muhammad Ariful Islam Sep 23 '19 at 04:41
  • If the return type has to be `int`, then you have to return an `int`. You can try @tangrs suggestion and call `exit(0)` before the return. – Humphrey Winnebago Sep 23 '19 at 04:49
  • You might try `exit(0)` which will end the program without returning to main – doug Sep 23 '19 at 04:52
  • 2
    If a code judging site uses blatantly-faulty code to judge submissions, I would file a bug report about that. If the report is rejected, I would move on to a different site. – Remy Lebeau Sep 23 '19 at 15:48
1

Instead of cout<< FizzbuzzFunc(number); just do FizzbuzzFun(number);. This way you are not printing the value returned by the function. If you are not going to use the value returned by a function, then you should declare the function with a void return type.

Make your FizzbuzzFunc() function return a void return type like this: void FizzbuzzFunc(int num). This version of FizzbuzzFunc() doesn't return any value so you don't have to do return 0; from inside the function.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • I can't change the judge template code as I told earlier. I already tried what you told me. But the problem is that template and I have to call like that. – Muhammad Ariful Islam Sep 23 '19 at 04:21
  • I see what you mean, if you can s change add another cout statements after cout << FizzbuzzFunc(number); then you can use a backspace character to delete your last character and replace it with something else. if you can convert your call to cout << FizzbuzzFunc(number) <<'\b' <<" " to this version. –  Sep 23 '19 at 04:50
  • If you can't do as above, then you have to pre-maturely exit from your function. –  Sep 23 '19 at 04:51