Search code examples
c++stringtranslatefizzbuzz

Is there a shorthand way to print a different variable if one variable is empty in C++?


I'm trying to write a minified FizzBuzz program in C++ as I am just now learning it. I wondering if there's a shorthand way to say "If this string exists, return this string, otherwise, return this next part"

In JavaScript, using the || operator works the way I'm thinking of:

function fizzbuzz() {
  const maxNum = 20; // Simplified down to 20 just for example
  for (let i = 1; i <= maxNum; i++) {
    let output = "";
    if (i % 3 == 0) output += "Fizz";
    if (i % 5 == 0) output += "Buzz";
    console.log(output || i); // If the output has something, print it, otherwise print the number
  }
}

fizzbuzz();

When I try this in C++,

#include <iostream>
#include <string>
using namespace std;

int main() {
    int maxNum = 100;
    string output;
    for (int i = 1; i <= maxNum; i++) {
        output = "";
        if (i % 3 == 0) output += "Fizz";
        if (i % 5 == 0) output += "Buzz";
        cout << output || i << endl; // I've also tried the "or" operator instead of "||"
    }
    return 0;
}

I get this error:

main.cpp:12:32: error: reference to overloaded function could not be resolved; did you mean to call it?
        cout << output || i << endl;
                               ^~~~

I know that you can just say this before the cout (and change the cout line):

if (output == "") output += to_string(i);

But my question is just is there a shorthand way to do this in C++, like there is in JavaScript, or do I have to have something similar to the if statement above?


Solution

  • In C++, you can just use a ternary operator.

    Ternary operators work like this (very similar to ternary in JavaScript):

    condition   ? "truthy-return" : "falsey-return"
    ^ A boolean    ^ what to return   ^ what to return
      condition      if the condition   if the condition
                     is truthy          is falsey
    

    That ternary example is equivalent to this (assume being in a function that returns a string):

    if (condition) {
      return "truthy-return";
    } else {
      return "falsey-return";
    }
    

    C++ does have its quirks since it is a statically typed language:

    1. std::string with a value of "" (an empty string) is not considered false, boolean wise. Finding the length of the string through stringName.length() will return a 0 if the length is 0.
    2. The return type of both sides of the : must be the same, so you must convert i into a string with std::to_string(i)
    3. Finally, this ternary operator must be inside of its own parentheses, like this: (output.length() ? output : to_string(i))

    The code:

    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
        int maxNum = 100;
        string output;
        for (int i = 1; i <= maxNum; i++) {
            output = "";
            if (i % 3 == 0) output += "Fizz";
            if (i % 5 == 0) output += "Buzz";
            cout << (output.length() ? output : to_string(i)) << endl;
        }
        return 0;
    }