Switch quantity not an integer C++

Summary

The issue at hand is a switch statement with a non-integer switch quantity, which is causing a compilation error. The switch quantity is a std::string variable named result, but it is not being assigned a value before the switch statement. This is a critical error because the switch statement in C++ requires an integer or enumeration type as its switch quantity.

Root Cause

The root cause of this issue is:

  • The switch quantity is a std::string variable, which is not allowed in a switch statement.
  • The switch quantity is not being assigned a value before the switch statement.
  • The case labels are using boolean expressions, which are not allowed in a switch statement.

Why This Happens in Real Systems

This issue can happen in real systems when:

  • Developers are not careful with the type of variable used in a switch statement.
  • Developers are not aware of the limitations of switch statements in C++.
  • Code is not thoroughly reviewed or tested before deployment.

Real-World Impact

The real-world impact of this issue can be:

  • Compilation errors that prevent the code from building.
  • Runtime errors that cause the program to crash or produce unexpected results.
  • Security vulnerabilities that can be exploited by attackers.

Example or Code

#include 

int main() {
    int result = 1;
    switch (result) {
        case 1:
            std::cout << "Today's winner is Shirogane Kei.";
            break;
        case 2:
            std::cout << "Today's loser is, Shinomiya Kaguya.";
            break;
        default:
            std::cout << "Today's winner is...both";
            break;
    }
    return 0;
}

How Senior Engineers Fix It

Senior engineers fix this issue by:

  • Using an integer or enumeration type as the switch quantity.
  • Assigning a value to the switch quantity before the switch statement.
  • Using if-else statements instead of switch statements when the case labels are boolean expressions.
  • Thoroughly reviewing and testing the code before deployment.

Why Juniors Miss It

Juniors may miss this issue because:

  • They are not familiar with the limitations of switch statements in C++.
  • They are not careful with the type of variable used in a switch statement.
  • They do not thoroughly review or test the code before deployment.
  • They are not aware of the potential security vulnerabilities that can be caused by this issue.