-1

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();
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
David
  • 49
  • 3
  • 1
    What are you expecting? A "console app" to create it's own window to do IO? That isn't how it works... – John3136 Oct 09 '15 at 02:26
  • What steps did you take to run the program on the linux machine? – R Sahu Oct 09 '15 at 02:28
  • Your question is not clear. For example, you have said a problem occurs when you are not running the program from the command line. So how exactly are you running it and what exactly is the problem that occurs? Please show how you compiled the program, exactly how you are running it and what the exact error is. – kaylum Oct 09 '15 at 02:29
  • I compile it in a unix environment. I'm not trying to run the exe file. I'm trying to run the spit out file by gcc after I compile it. Let's call it game so I can run ./game and its perfect. But when I double click game from a gui it does not run. It doesn't do anything. Now on windows if you double click the exe it opens cmd and runs the file. How can I make it act like windows? (Open terminal and run the game) – David Oct 09 '15 at 02:29
  • 3
    You need to set up a launcher for your program. https://askubuntu.com/questions/475081/how-to-create-a-launcher-to-execute-a-terminal-command. Note that the terminal window will close when your program exits. So you'll need some sort of pause code at the end of your program if you want the terminal window to remain open. – kaylum Oct 09 '15 at 02:37
  • Your program is a console program, it's normal that you can't run it by double click on the executable. In OS when you double click, the porgram should be a GUI program. A console program will open the terminal, run your program and close the terminal when your application is finished. If you want a GUI program try to use some GUI Frameworks like GTK+. – Captain Wise Oct 09 '15 at 02:39
  • 2
    Note also that this question has nothing much to do with C (applies to all console programs written in any language). This is more a "how do I use my OS" type question. – kaylum Oct 09 '15 at 02:41
  • So, in Unix, when you click an executable that is command line based, it will not run? As opposed to in windows when you click an executable that is command line based it will open terminal for you and run the program. Command line based program meaning one that does not have a GUI. – David Oct 09 '15 at 02:59
  • 3
    Unlike Windows, Linux systems can be run with different Window Managers (e.g. KDE, Gnome, etc). A WM essentially manages all aspects of the GUI environment. Read [here](https://superuser.com/questions/41830/what-is-gnome-kde-etc) for an explanation of how a Linux system is composed. So back to your question - whether it can be done and how it can be done is dependent on the WM you are using. But essentially there should be a way to set how to open certain files. Try right click on your program and explore the settings there. Good luck! – kaylum Oct 09 '15 at 03:10
  • If every utility program (`cp`, `mv`, `ifconfig` etc) running in background popped its own terminal window, you'd go crazy. – el.pescado - нет войне Oct 09 '15 at 05:56
  • Yeah, I know, but I was more so thinking of a windows like functionality where if you double click an exe that is cmd based, it pops open a cmd window until the program terminatez – David Oct 09 '15 at 05:58
  • You configure your program icon with an option "run in terminal" or similar. Normally you need to create a new icon (desktop shortcut) for it. Exact details depend on which WM/DE you have. – n. m. could be an AI Oct 09 '15 at 06:00

1 Answers1

0

But if I go through the GUI to the game file and double click it, it doesn't do anything

How do you know that it doesn't run? I bet it does run, but as it is not run in context of any terminal (command line window), you just can't see its output. That's how Linux (and Unix in general) work.

Windows differentiates GUI and commandline applications, and in the latter case automatically brings a console window. Unfortunately (fortunately?), that's not the case in Unix.

If you want to achieve Windows behavior, you could:

  1. Create a launcher for your application. That will allow you to specify custom icon etc.

  2. Create a shell script that will invoke teminal and your application inside of it:

game.sh:

#!/bin/bash
gnome-terminal -e "./game"

Please note that not everyone will have gnome-temrinal installed, so you may have to adjust your script to support more terminal emulators (xterm, rxvt, maybe konsole for KDE users).

Community
  • 1
  • 1