2

I am trying to create a program that computes the value of a word by summing the values of its letters.

These are the letters and their corresponding value:

  • 1 Point: A, E, I, L, N, O, R, S, T, U
  • 2 Points: D, G
  • 3 Points: B, C, M, P
  • 4 Points: F, H, V, W, Y
  • 5 Points: K
  • 8 Points: J, X
  • 10 Points: Q, Z

Here's the program:

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

int main(void)
{
    char ch; int sum=0;

    printf("Enter a word: ");

    while ((ch=getchar())!='\n') {
        toupper(ch);
        if (ch== 'A' || ch== 'E' || ch== 'I' || ch== 'L' || ch== 'N' || ch== 'O' || ch== 'R' || ch== 'S' || ch== 'T' || ch== 'U')
            sum+=1;

        else if (ch== 'D' || ch== 'G')
            sum+=2;

        else if (ch=='B' || ch== 'C' || ch== 'M' || ch== 'P')
            sum+=3;

        else if (ch=='F' || ch== 'H' || ch== 'V' || ch== 'W' || ch== 'Y')
            sum+=4;

        else if (ch=='K')
            sum+=5;

        else if (ch=='J' || ch== 'X')
            sum+=8;

        else if (ch=='Q' || ch== 'Z')
            sum+=10;
    }
    printf("\nScrabble value: %d",sum);

    return 0;
}

When I run the program it prints "Scrabble value: 0". Seems like the program is skipping the while loop entirely, maybe I am not using getchar properly, but I can't figure out what's wrong.

5
  • The points are the same points for each letter in the game scrabble.Is it so you are creating some program in connection with scrabble? Commented Jun 23, 2014 at 14:56
  • Yes, I am reading a book and creating this program was an exercise. Commented Jun 23, 2014 at 15:03
  • Thanks guys! I wasn't expecting answers so fast. Commented Jun 23, 2014 at 15:05
  • 1
    Then mark an answer as accepted by pressing the green tick mark beside the answer Commented Jun 23, 2014 at 15:08
  • Note that getchar() returns int, and might return EOF rather than an actual character. I/O is tricky. Commented Jun 23, 2014 at 15:19

5 Answers 5

7

It should be ch = toupper(ch);

toupper does not modify it's argument.

And, while not in any way related to the issue, I would recommend using switch for code like this.

Sign up to request clarification or add additional context in comments.

Comments

1

Your problem is in toupper(). The function does not alter your char directly, it returns the altered char.

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

int main(void)
{
    char ch; int sum = 0;

    printf("Enter a word: ");

    while ((ch = getchar()) != '\n') {
        ch = toupper(ch);
        if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'L' || ch == 'N' || ch == 'O' || ch == 'R' || ch == 'S' || ch == 'T' || ch == 'U')
            sum += 1;
        else if (ch == 'D' || ch == 'G')
            sum += 2;
        else if (ch == 'B' || ch == 'C' || ch == 'M' || ch == 'P')
            sum += 3;
        else if (ch == 'F' || ch == 'H' || ch == 'V' || ch == 'W' || ch == 'Y')
            sum += 4;
        else if (ch == 'K')
            sum += 5;
        else if (ch == 'J' || ch == 'X')
            sum += 8;
        else if (ch == 'Q' || ch == 'Z')
            sum += 10;
    }
    printf("\nScrabble value: %d", sum);
    return 0;
}

Comments

0

toupper(ch); computes the uppercase of ch but does nothing with it. Your code works fine with UPPERCASE input. With lowercase input you just need to change

toupper(ch);

to

ch = toupper(ch);

Comments

0

Unless you store the converted character to a variable it remains in the case in which it had been entered. 'toupper' is a function and to re-use the value it returns you must store a copy.

Comments

0

Sentences may not be "loose" in a program, must be associated with others, in general with the assignment sign (=). Change ch = toupper(ch) by ch = toupper(ch)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.