0

My scrabble program gets broken when I put numbers into it.

#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 word1);

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);
    
    if(score1>score2)
    printf("Player 1 wins!");
    else if(score1<score2)
    printf("Player 2 wins!");
    else
    printf("Tie!");

    // TODO: Print the winner
}

int compute_score(string word)
{
    // TODO: Compute and return score for string
    int sum = 0;
    for(int i = 0, n = strlen(word); i < n; i++)
    
    sum = sum + (POINTS[(int)(toupper(word[i]))- 65]);
    return sum;
}

How can I screen out any sort of number input, or negate it so a user can't enter numbers whatsoever to the string?

I'd be particularly interested in learning how I could reject any values within the compute_score function that aren't 0-25 (Values that fall outside of my array length).

Thanks for the help!

1 Answer 1

0

Try validating the input, one char at a time, using a while loop.

HINT: isalpha() is your friend.

1
  • Thank you! Appreciate it Cliff.
    – Multiplify
    Commented Apr 2, 2021 at 19:44

You must log in to answer this question.

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