#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0

    Getchar() / while loop trouble in C


    Hello everyone, I am working on an assignment in C and I am almost done. The program needs to calculate the scrabble score of an entered word. My code does this fine, I am having trouble finishing the while loop.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int letter=1;
    int points = 0;
    
    int main()
    {
        printf("Please enter a scrabble word: ");
        while(letter != '.')
        {
           letter = getchar();
           letter = toupper(letter);
    
           switch(letter)
           {
                case 'A' : points += 1;  break;
                case 'B' : points += 3;  break;
                case 'C' : points += 3;  break;
                case 'D' : points += 2;  break;
                case 'E' : points += 1;  break;
                case 'F' : points += 4;  break;
                case 'G' : points += 2;  break;
                case 'H' : points += 4;  break;
                case 'I' : points += 1;  break;
                case 'J' : points += 8;  break;
                case 'K' : points += 5;  break;
                case 'L' : points += 1;  break;
                case 'M' : points += 3;  break;
                case 'N' : points += 1;  break;
                case 'O' : points += 1;  break;
                case 'P' : points += 3;  break;
                case 'Q' : points += 10; break;
                case 'R' : points += 1;  break;
                case 'S' : points += 1;  break;
                case 'T' : points += 1;  break;
                case 'U' : points += 1;  break;
                case 'V' : points += 4;  break;
                case 'W' : points += 4;  break;
                case 'X' : points += 8;  break;
                case 'Y' : points += 4;  break;
                case 'Z' : points += 10; break;
            }
        }
    printf("Scrabble value: %d", points);
    return 0;
    }
    Currently you are required to type a '.' after the word. I am having trouble figuring out how to have the while loop finish when the end of the word has been typed so that no period is required. Any advice would be much appreciated. Thanks!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,894
    Rep Power
    481
    If you'd rather have newline indicate end of word,
    replace '.' with '\n' .

    Please modify your program to account for double and triple letter scores, same for word scores.
    For instance,
    2*c could mean double letter score
    3*q triple q
    2#e double word score
    3#r triple word score

    or whatever.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Canada
    Posts
    185
    Rep Power
    0
    If you're making Scrable, might as well make it reaslistic for the player. A word is defined as a character sequence before whitespace, newline or tabs so....
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int letter=1;
    int points = 0;
    
    int main(void){
    
        printf("Please enter a scrabble word: ");
        while( (letter = getchar()) !=EOF && (letter !='\n') && (letter != ' ') && (letter != '\t')){
           letter = toupper(letter);
    I'd also check to make sure it is an actual word...but that may be a little involved at this point ;)
    "In theory, there is no difference between theory and practice.
    But, in practice, there is."

  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    62
    Rep Power
    4
    It might be an idea to copy the word to a buffer and work out each character from the buffer for the score, as this method will still add to the score if a letter is deleted.

    Actually, my mistake, it doesn't.

    Regards,

    Shaun.
    Last edited by Shaun_B; March 5th, 2013 at 05:35 AM.

IMN logo majestic logo threadwatch logo seochat tools logo