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

// Points assigned to each letter of the alphabet
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)
{
    // Get input words from both players
    string word1 = get_string("Player 1: ");
    string word2 = get_string("Player 2: ");
 // Score both words
    int score1 = compute_score(word1);
    int score2 = compute_score(word2);

    // TODO: Print the winner
{
    if (score1 > score2)
    {
        printf("%s\n", "Player 1 wins!");
    }
    else if (score1 < score2)
    {
        printf("%s\n", "Player 2 wins!");
    }
    else
    {
        printf("%s\n", "Tie!");
    }
}
}

int compute_score(string word)
{
    // TODO: Compute and return score for string
{
    int score = 0;
{
    for(int i = 0; i < strlen(word); i++);

        if (isupper (word[i]))
        {
            score = score + POINTS[word [i] - 65];
        }

        if (islower (word [i]))
        {
            score = score + POINTS[word [i] - 97];
        }
}
}
    return score;
}
2
  • Can you please edit the question and insert the code by pasting it in? It's difficult to read images and nobody wants to transcribe a program to test it.
    – Cliff B
    Commented Nov 4, 2022 at 20:30
  • I edited it you can check it out now
    – Almira
    Commented Nov 5, 2022 at 5:59

1 Answer 1

0

Look at your for loop statement. It ends with a semicolon. That creates two problems. First, the code that follows isn't part of the for statement loop structure. Also, there are no curly braces to contain the loop. Second, it ends the scope of i because i is declared as part of the for loop and ceases to exist at the end of the loop.

The proper structure of a for loop with a code block is this:

for(int i=0; i < something; i++)   // note no ending semicolon
{     // opening curly brace to contain loop code

    // all the loop code goes in here, between curly braces
    ...

}     // closing curly brace to end loop.

If you clean up the for loop structure, many of your problems will disappear. I suggest a review of the lecture material on for loops.

Also, the code has unnecessary curly brace pairs. That can have an adverse effect by limiting the scope of variables. (Scope is a later lesson.)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Thankyou sooo much for your help.
    – Almira
    Commented Nov 5, 2022 at 6:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .