My C code compiles but won't execute, and I don't know why

92 Views Asked by At

My code is below:

#include <ctype.h>
#include <cs50.h>
#include <stdio.h>
#include <string.h>

int points[] = {1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10};

int compute_score(string word);

int main(void)
{
    string word1 = get_string("Player 1: ");
    string word2 = get_string("Player 2: ");

    int score1 = compute_score(word1);
    int score2 = compute_score(word2);

    if (score1 > score2)
    {
        print("Player 1 wins! \n");
    }
    else if (score2 > score1)
    {
        print("Player 2 wins! \n");
    }
    else
    {
        print("Tie! \n");
    }
}

int compute_score(string word)
{
    int score = 0;
    for (int i = 0, len = strlen(word); i < len; i++)
    {
        if isupper(word[i])
        {
            score += points[word[i] - "A"];
        }
        else
        {
            score += points[word[i] - "a"];
        }
    }
    return score;
}

I expected it to execute, and I still don't know why it didn't. It keeps saying "Permission denied" when I try to run the file, although the "make" function works fine. Any ideas?

1

There are 1 best solutions below

1
XeuVon On

It's because you don't have Execution Permission for the file.

If you do ls -la you see somthing like this:

drwxr-xr-x 2 hexatech hexatech 4096 Feb 14 09:01 .
drwxr-xr-x 4 hexatech hexatech 4096 Feb 13 13:51 ..
-rwxr-xr-x 1 hexatech hexatech  511 Feb 13 14:45 Makefile
-rw-r--r-- 1 hexatech hexatech  347 Feb 14 09:00 main.c

The left part drwxr-xr-x tell information about the file.

The d at start is for Directory, and rwx is for read, write & execution.

You have 3 time's rwx, in order is for User, group, other.

You can change the file Permission with chmod command.

For more info use chmod --help or/and man chmod.

when you use make, that call a Makefile and he set that permission for you.