This program is for a scrabble board. You enter a sentence and it counts the number of a,g,m,f,k,j letters used in the sentence then tallys the total points. The code works fine as long as one of the specified letters aren't used first in the sentence and I'm not sure why. I've tried everything I could think of.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
#include <iostream>
using namespace std;
int main()
{
char scrabble;
int a = 0,g = 0,m = 0,f = 0,k = 0,j = 0;
int A = 0,G = 0,M = 0,F = 0,K = 0,J = 0;
cout << "Enter Text:"<<endl;
cin >> scrabble;
while (cin >> scrabble && scrabble !='.' && scrabble !='!')
{
if ((scrabble == 'a') || (scrabble == 'A'))
{
a++;
}
else if ((scrabble == 'g') || (scrabble == 'G'))
{
g++;
}
else if ((scrabble == 'm') || (scrabble== 'M'))
{
m++;
}
else if ((scrabble == 'f') || (scrabble == 'F'))
{
f++;
}
else if ((scrabble == 'k') || (scrabble == 'K'))
{
k++;
}
else if ((scrabble == 'j') || (scrabble == 'J'))
{
j++;
}
else if ((scrabble == '.') || (scrabble == '!'))
{
break;
}
}
cout << "Number of a's (worth 1 point each): " <<a<< endl;
cout << "Number of g's (worth 2 point each): " <<g<< endl;
cout << "Number of m's (worth 3 point each): " <<m<< endl;
cout << "Number of f's (worth 4 point each): " <<f<< endl;
cout << "Number of k's (worth 5 point each): " <<k<< endl;
cout << "Number of j's (worth 8 point each): " <<j<< endl;
cout <<""<<endl;
cout << "Total score: "<<(a*1)+(g*2)+(m*3)+(f*4)+(k*5)+(j*8)<< endl;
return 0;
}
|
Example:
When the sentence:
Multiple characters now work, too.
the output should be:
Number of a's (worth 1 point each) : 2
Number of g's (worth 2 points each) : 0
Number of m's (worth 3 points each) : 1
Number of f's (worth 4 points each) : 0
Number of k's (worth 5 points each) : 1
Number of j's (worth 8 points each) : 0
Total score: 10
my output is
Number of a's (worth 1 point each) : 1
Number of g's (worth 2 points each) : 0
Number of m's (worth 3 points each) : 1
Number of f's (worth 4 points each) : 0
Number of k's (worth 5 points each) : 1
Number of j's (worth 8 points each) : 0
Total score: 7