C program - confusing output -
i trying create programme computes value of word summing values of letters.
these letters , 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, zhere's program:
#include <stdio.h> #include <ctype.h> int main(void) { char ch; int sum=0; printf("enter 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); homecoming 0; } when run programme prints "scrabble value: 0". seems programme skipping while loop entirely, maybe not using getchar properly, can't figure out what's wrong.
it should ch = toupper(ch);
toupper not modify it's argument.
and, while not in way related issue, recommend using switch code this.
c
No comments:
Post a Comment