Read_My_Feels991 Read_My_Feels991 - 1 month ago 11
C Question

Why won't a chunck of this program run in Linux but will on Windows?

So I had to build a tiny little game that asks players to pick a number and see if they guess what the computer picks. It has 2 players and each can pass their turn to the other.
One of the stipulations is that my professor is running a linux OS and has said if it doesn't work on that then it's a wash. When I run the code in codeblocks is works find and does exactly what it's supposed to do. If a player passes and output message is displayed and the turn switches.

When I run the code on the linux server it always evaluates the

turnInput
of
playTurn()
as something other than "PASS" which returns the
if(strcmp(turnInput, passValue) == 1)
as false and moves to
int turnInputNum = atoi(turnInput);


When I run it on codeblocks on windows it's fine and does exactly what it should. when I compiled it I didn't get any warnings and linux in and of itself is an OS I've really never dealt with before. Is there a reason why it would be passing over that code?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <stdbool.h>

struct player{
int passNum;
int currentPassNum;
bool myTurn;
};
struct player player1;
struct player player2;



int turn = 1; //holds the games current turn
int input; //holds the number input by player to indicate which player it is
int RN; //holds Random Number
int actualNum = -1; //signals completion of game by ending while loop

void playTurn(void);
int main() {

player1.currentPassNum = 0;
player1.passNum = 0;
player1.myTurn = true;
player2.currentPassNum = 0;
player2.passNum = 0;
player2.myTurn = false;

RN = generateRandom();


while(RN != actualNum){
printf("\nEnter Player Number: ");
scanf("%d", &input);
if(input == turn){
playTurn();
if(turn == 1 && player1.myTurn == false){
turn = 2;
}
else{
turn = 1;
}
}
else{
printf("You have to wait your turn");
}

}



return 0;
}

int generateRandom(){
int n;

srand(time(NULL));

n = rand() % 100 + 1;

return n;
}

void playTurn(){
char turnInput[4];
char passValue[4] = "PASS";
int passes;

printf("Enter your guess: ");
scanf("%s", &turnInput);
if(strcmp(turnInput, passValue) == 1){ <---- not running when PASS
if(input == 1){
if(player1.currentPassNum > 0){
player1.currentPassNum = 0;
player1.myTurn == true;
printf("You may not pass!");
return;
}
else{
passes = player1.passNum;
player1.passNum = player1.passNum + 1;
player1.currentPassNum = 1;
player1.myTurn = false;
printf("Player 1 Passes to Player 2");
return;
}
}
if(input == 2){
if(player2.currentPassNum > 0){
player2.currentPassNum = 0;
printf("You may not pass!");
return;
}
else{
passes = player2.passNum;
player2.passNum++;
player2.currentPassNum++;
printf("Player 2 Passes to Player 1");
return;

}
}
if(passes > 3){
printf("You can't pass anymore");
return;
}
}
int turnInputNum = atoi(turnInput); <---- Jumping to here every time
if(turn == 1){
player1.currentPassNum = 0;
player1.myTurn = false;
}
else{
player2.currentPassNum = 0;
player1.myTurn = true;
}
if(turnInputNum > RN){
printf("Too High");
return;
}
if(turnInputNum < RN){
printf("Too Low");
return;
}
if(turnInputNum == RN){
printf("CORRECT");
actualNum = RN;
return;
}

}

Answer

strcmp has 3 possible return codes:

  1. <0
  2. ==0
  3. >0

There is no specific ==1, and so it is not safe or portable to assume you can say ==1. Use >0 instead.

This is actually a pretty good example of portability and porting issues - from the question, apparently ==1 works on windows, but port that code to Linux and it stops. I'm not sure if windows documents "-1, 0, 1" or whether it was just an assumption that happened to work, but as OP found out: "Gotcha!!"

Comments