Quinn
Quinn

Reputation: 25

Int Variable in C Changing When Not Called

Apologies in advance, I'm very new to this.

I am attempting to score "scrabble words" according to a POINTS array with associated score values.

  1. I first get the length of the string of word1.
  2. I then create a for loop that takes the value of the lower case letter minus 97 (to get to the 0 index) and adds it to the array.

You can ignore the rest of the code, as this is where the issue lies. While the stringlength variable is only defined once, I've found that it somehow changes during the second cycle of the for loop. A four letter word will originally have a stringlength of 4, however the for loop changes it to 1 on the second run.

#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 main(void) {
    // Get input words from both players
    string word1 = get_string("Player 1: ");
    string word2 = get_string("Player 2: ");

    int stringlength = strlen(word1);

    int array[] = {};
    int array2[] = {};

    //Create an array with each letter
    for (int index = 0; index < stringlength; index++) {
        //THE BELOW LINE IS WHERE "STRINGLENGTH" CHANGES TO "1" ON THE SECOND LOOP
        array[index] = word1[index] - 97;
        array2[index] = POINTS[array[index]];
    }

    int score = 0;

    for (int index2 = 0; index2 < stringlength; index2++)
        score += array2[index2];

    printf("%i", stringlength);

    printf("\n");
}

I know I could just create a secondary stringlength variable but I know that's bad programming and I'd love to know what I'm doing wrong.

Any help would be greatly appreciated. Thanks!

Upvotes: 1

Views: 89

Answers (1)

chqrlie
chqrlie

Reputation: 144695

There are multiple problems in the code:

  • int array[] = {}; is a syntax error. There must be at least one initializer for an array definition. In your case, you must define array with a length of stringlength with:

    int array[stringlength];
    

    your compiler seems to accept the definition and create an empty array, which is not long enough to store the letter scores: you have undefined behavior in both for loops, accessing array elements beyond their boundaries. Undefined behavior means anything can happen, including what you observe.

  • in word1[index] - 97, you hard code the ASCII value of 'a'. This is bad practice, you should write this instead:

    word1[index] - 'a'
    

    note however that you also make 2 more silent assumptions in this expression:

    • you assume that word1 only contains lowercase letters, which should be tested as the user can type any string.
    • you assume that the lowercase letters form are contiguous in the execution character set, which is correct for ASCII but not guaranteed by the C Standard. Since all modern systems use ASCII for the standard characters, this assumption is OK.
  • printf("%i", stringlength); outputs the string length, not the score.

Here is a modified version without additional arrays:

#include <cs50.h>
#include <stdio.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 main(void) {
    // Get input words from both players
    string word1 = get_string("Player 1: ");
    string word2 = get_string("Player 2: ");

    //Compute the score for player 1
    int score1 = 0;
    for (int index = 0; word1[index] != '\0'; index++) {
        unsigned char c = word1[index];
        if (c >= 'a' && c <= 'z') {
            // assuming ASCII character set
            score1 += POINTS[c - 'a'];
        }
    }

    //Compute the score for player 2
    int score2 = 0;
    for (int index = 0; word2[index] != '\0'; index++) {
        unsigned char c = word2[index];
        if (c >= 'a' && c <= 'z') {
            // assuming ASCII character set
            score2 += POINTS[c - 'a'];
        }
    }
    printf("Player 1: %d\n", score1);
    printf("Player 2: %d\n", score2);
    return 0;
}

If you know about functions, you could write a function int compute_score(const char *word) to avoid duplicating the code and improve readability.

Upvotes: 1

Related Questions