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:
Why are these results coming out? Also, how can I modify the if condition to work?