dimanche 25 avril 2021

Switch Statement Called Inside Of A Failed If Statement

Sorry if the title is misleading or generic. I literally do not know what else to name the title. I have created a function that converts a vector of any type into a string. I have template<typename K> prefacing the function code, which is used as a placeholder for the type of the vector. My main problem is the nested switch statements inside of if statements used to check the type of the vector. Even when the if statement checks fail, the code still gives this error: a switch expression of type 'float' is not valid. For example, if I had a vector of type float declared in main, then I used the function to convert the vector to a string, it would give me two errors. One for the switch statement nested inside the int check, and the other for the switch statement nested inside of the char check. This is very frustrating as these if checks should fail due to the vector not being an int or char. Furthermore, they can't be both int and char at the same time either! I've been trying to dissect my code and figure this problem out, but I've come to no solution. Maybe it's because I'm new to templates and the std::is_same function.

Here is my header file code (it's kinda long sry):

#include <string>
#include <vector>

//Converts a vector of any type into a string like this: 2, 5, 6, 3, (Yes the comma stays at the end like that. That can be fixed but it will require huge amounts of code)
template<typename K>
std::string convertToString(std::vector<K>& V) {

    std::string result_str{""};

    for (auto s : V) {

        if (std::is_same<K, bool>::value) {

            if (s == true) {

                result_str += "true, ";

            }
            else if (s == false) {

                result_str += "false, ";

            }

        }
        else if (std::is_same<K, int>::value) {

            switch (s) {

            case 1:
                result_str += "1, ";
                break;

            case 2:
                result_str += "2, ";
                break;

            case 3:
                result_str += "3, ";
                break;

            case 4:
                result_str += "4, ";
                break;

            case 5:
                result_str += "5, ";
                break;

            case 6:
                result_str += "6, ";
                break;

            case 7:
                result_str += "7, ";
                break;

            case 8:
                result_str += "8, ";
                break;

            case 9:
                result_str += "9, ";
                break;

            default:
                result_str += "Not a digit, ";

            }

        }
        else if (std::is_same<K, float>::value) {

            //Because switch statements cannot take floating point values (For some reason I thought this would fix my problem but it didn't)
            if (s == 1) {

                result_str += "1, ";

            }
            else if (s == 2) {

                result_str += "2, ";

            }
            else if (s == 3) {

                result_str += "3, ";

            }
            else if (s == 4) {

                result_str += "4, ";

            }
            else if (s == 5) {

                result_str += "5, ";

            }
            else if (s == 6) {

                result_str += "6, ";

            }
            else if (s == 7) {

                result_str += "7, ";

            }
            else if (s == 8) {

                result_str += "8, ";

            }
            else if (s == 9) {

                result_str += "9, ";

            }
            else {
                result_str += "., ";
            }

        }
        else if (std::is_same<K, char>::value) {

            switch (s)
            {
            case 'a': 
                result_str += "a, ";
                break;

            case 'A':
                result_str += "A, ";
                break;

            case 'b': 
                result_str += "b, ";
                break;

            case 'B':
                result_str += "B, ";
                break;

            case 'c': 
                result_str += "c, ";
                break;

            case 'C':
                result_str += "C, ";
                break;

            case 'd': 
                result_str += "d, ";
                break;

            case 'D':
                result_str += "D, ";
                break;

            case 'e': 
                result_str += "e, ";
                break;

            case 'E':
                result_str += "E, ";
                break;

            case 'f': 
                result_str += "f, ";
                break;

            case 'F':
                result_str += "F, ";
                break;

            case 'g': 
                result_str += "g, ";
                break;

            case 'G':
                result_str += "G, ";
                break;

            case 'h':
                result_str += "h, ";
                break;

            case 'H':
                result_str += "H, ";
                break;

            case 'i':
                result_str += "i, ";
                break;

            case 'I':
                result_str += "I, ";
                break;

            case 'j': 
                result_str += "j, ";
                break;

            case 'J':
                result_str += "J, ";
                break;

            case 'k': 
                result_str += "k, ";
                break;

            case 'K':
                result_str += "K, ";
                break;

            case 'l': 
                result_str += "l, ";
                break;

            case 'L':
                result_str += "L, ";
                break;

            case 'm': 
                result_str += "m, ";
                break;

            case 'M':
                result_str += "M, ";
                break;

            case 'n': 
                result_str += "n, ";
                break;

            case 'N':
                result_str += "N, ";
                break;

            case 'o': 
                result_str += "o, ";
                break;

            case 'O':
                result_str += "O, ";
                break;

            case 'p': 
                result_str += "p, ";
                break;

            case 'P':
                result_str += "P, ";
                break;

            case 'q': 
                result_str += "q, ";
                break;

            case 'Q':
                result_str += "Q, ";
                break;

            case 'r': 
                result_str += "r, ";
                break;

            case 'R':
                result_str += "R, ";
                break;

            case 's': 
                result_str += "s, ";
                break;

            case 'S':
                result_str += "S, ";
                break;

            case 't': 
                result_str += "t, ";
                break;

            case 'T':
                result_str += "T, ";
                break;

            case 'u': 
                result_str += "u, ";
                break;

            case 'U':
                result_str += "U, ";
                break;

            case 'v': 
                result_str += "v, ";
                break;

            case 'V':
                result_str += "V, ";
                break;

            case 'w': 
                result_str += "w, ";
                break;

            case 'W':
                result_str += "W, ";
                break;

            case 'x':
                result_str += "x, ";
                break;

            case 'X':
                result_str += "X, ";
                break;

            case 'y': 
                result_str += "y, ";
                break;

            case 'Y':
                result_str += "Y, ";
                break;

            case 'z': 
                result_str += "z, ";
                break;

            case 'Z':
                result_str += "Z, ";
                break;
            }

        }

    }

    return result_str;

}

Here is my cpp file code:

#include "Helper.h"
#include <iostream>

std::vector<float> nums{ 2.4, 65.2, 56.3, 4.6 };

int main() {

    std::cout << convertToString(nums);

}

Aucun commentaire:

Enregistrer un commentaire