Hello, whoever can help. I'm studying the Pike language out of curiosity. I noticed that when two integer values are entered, the decision that occurs right after the inputs is processed correctly. However, when there is only one input, a flow error occurs, always directing the decision as false, even when it's true.
This program works correctly:
int main()
{
int X = 0;
int Y = 0;
sscanf(Stdio.stdin.gets(), "%d", X);
sscanf(Stdio.stdin.gets(), "%d", Y);
if (X <= Y)
write("%d\n", X);
else
write("%d\n", Y);
return 0;
}
This program does not work:
int main()
{
int X = 0;
int Y = 0;
sscanf(Stdio.stdin.gets(), "%d", X);
if (X >= 10)
Y = pow(X, 2);
else
Y = pow(X, 3);
write("%d\n", Y);
return 0;
}
Does anyone have knowledge of this type of occurrence?
The second program, when given an integer value greater than or equal to 10, should display its cube, and for values that do not meet this condition, it should return the square. However, values less than 10 are returning the cube instead of the square.
After the request for help, I continued researching and conducting tests. I discovered that, despite the apparent optional use of braces "{}" after "if" and "else" when there is only one operation, they are actually necessary for any use. Therefore, the indicated codes should be written as follows:
int main()
{
int X = 0;
int Y = 0;
sscanf(Stdio.stdin.gets(), "%d", X);
sscanf(Stdio.stdin.gets(), "%d", Y);
if (X <= Y) {
write("%d\n", X);
} else {
write("%d\n", Y);
}
return 0;
}
int main()
{
int X = 0;
int Y = 0;
sscanf(Stdio.stdin.gets(), "%d", X);
if (X >= 10) {
Y = pow(X, 2);
} else {
Y = pow(X, 3);
}
write("%d\n", Y);
return 0;
}