It might be my code, but I can compile and run this program via command line. In windows the exe file is also runnable. However, I cannot run the compiled code outside of the terminal on any unix systems (ubuntu or osx) I am pretty new to C so any help would be appreciated. Thank you!
Clarification:
In Ubuntu I run
gcc game.c -o game
./game
And then it runs perfectly. But if I go through the GUI to the game file and double click it, it doesn't do anything.
I'm used to on Windows it would bring up a command and run the program as if you ran it from cmd
.
Code (it is a simple number guessing game):
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int guessed = 0;
int guesses = 0;
int total_guesses = 0;
int games_played = 0;
int range_top = 10;
int generate_random_number()
{
srand(time(NULL));
return (rand() % range_top);
}
void take_guess(int num)
{
int guess;
printf("what is your guess: ");
scanf(" %i",&guess);
if(guess == num)
{
guessed = 1;
}
else if(guess>num)
{
printf("Your guess was too high,\n");
}
else
{
printf("Your guess was too low.\n");
}
}
void print_stats()
{
printf("\n\n\nGames Played: %i\nTotal Guesses: %i\nAverage Guesses Per Game: %f\n\n\n\n",games_played,total_guesses,(double)total_guesses/games_played);
int i = 5;
while(i>0)
{
printf("exiting in %is\n",i);
i--;
sleep(1);
}
}
int main(void)
{
printf("This is a game in C\n");
printf("Would you like to play? (y/n): ");
char play;
scanf("%c",&play);
while(play == 'y')
{
printf("I am thinking of a number between 0 and %i\n",range_top);
int num = generate_random_number();
while(guessed ==0)
{
take_guess(num);
guesses++;
}
games_played++;
total_guesses+=guesses;
printf("It took you %i guesses to win.\n",guesses);
printf("Would you like to play again? (y/n): ");
scanf(" %c",&play);
guessed = 0;
guesses = 0;
}
print_stats();
}