Home > Net >  How to use a string entered in a txt file as an if condition
How to use a string entered in a txt file as an if condition

Time:04-06

A function containing the function of a vector is included in a class called Vector. I will determine which function to use in the main function by the string entered in the txt file.

class Vector
{
public: // private?
    double x, y, z;

public:
    Vector() {
        x = 0;
        y = 0;
        z = 0;
    }
    Vector(int _x, int _y, int _z) {
        x = _x;
        y = _y;
        z = _z;
    }
    Vector Add(Vector v) {
        Vector output;
        output.x = x   v.x;
        output.y = y   v.y;
        output.z = z   v.z;
        return output;
    }
    double Dot(Vector v) {
        double output;
        output = (x * v.x)   (y * v.y)   (x * v.y);
        return output;
    }
};

This is the main function. I want to use the string that I want to receive in the txt file, but it doesn't work well. A detailed example is below this code.

int main()
{
    FILE* fpInput;
    FILE* fpOutput;
    int vectorAx, vectorAy, vectorAz, vectorAdim;
    int vectorBx, vectorBy, vectorBz, vectorBdim;
    char VecFun[10];

    fpInput = fopen("input.txt", "r");

    fscanf(fpInput, "%d %d %d", &vectorAx, &vectorAy, &vectorAz);
    fscanf(fpInput, "%d %d %d", &vectorBx, &vectorBy, &vectorBz);

    fclose(fpInput);

    fpInput = fopen("input.txt", "r");
    fgets(VecFun, sizeof(VecFun), fpInput);
    fclose(fpInput);

    Vector vectorA(vectorAx, vectorAy, vectorAz);
    Vector vectorB(vectorBx, vectorBy, vectorBz);

    if (VecFun == "Add") {
        Vector vectorO = vectorA.Add(vectorB);
        fpOutput = fopen("output.txt", "w");
        fprintf(fpOutput, "%.f %.f %.f", vectorO.x, vectorO.y, vectorO.z);
        fclose(fpOutput);
    }
    else if (VecFun == "Dot") {
        fpOutput = fopen("output.txt", "w");
        fprintf(fpOutput, "%.f", vectorA.DotProduct(vectorB));
        fclose(fpOutput);
    }
    else {
        printf("Invalid input.. (%s)\n", VecFun);
    }

    return 0;
}

input.txt:

Add
1 2 3
4 5 6

ouput.txt:

5 7 9

input.txt:

Dot
1 2 3
4 5 6

ouput.txt:

32

However, the if condition did not work, so I tried debugging with:

printf("Invalid input.. (%s)\n", VecFun);

and found this result:

enter image description here

Why are these results coming out? Also, how can I modify the if condition to work?

CodePudding user response:

You can't compare C-style strings for equality using the == operator. In code like if (VecFun == "Add"), both the VecFun variable (the name of a char array) and the "Add" (a string literal) decay to pointers to their first elements; and, since they are different items in different memory locations, that test will always result in a false value.

In C, you would need to use the strcmp function, as shown in the answer(s) to the question linked above. However, in C (since C 14), you can specify your literals as std::string objects, using the s suffix; then, tests like VecFun == "Add"s will work as expected.1

Here's a short demo:

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

int main()
{
    char test[10] = "Add";
    // Wrong - comparing two different pointers ...
    if (test == "Add") {
        std::cout << "Raw comparison succeeded!\n";
    }
    else {
        std::cout << "Raw comparison failed!\n";
    }
    // Plain "C" way, using the strcmp function ...
    if (strcmp(test, "Add") == 0) {
        std::cout << "strcmp comparison succeeded!\n";
    }
    else {
        std::cout << "strcmp comparison failed!\n";
    }
    // C   way, using std::string literals ...
    if (test == "Add"s) {
        std::cout << "std::string comparison succeeded!\n";
    }
    else {
        std::cout << "std::string comparison failed!\n";
    }
    return 0;
}

1 If your compiler doesn't support the C 14 Standard or string literal operators, you could construct a std::string object explicily from the literal(s), using expressions like std::string("Add") in place of the "Add"s.

  • Related