-1
#include <iostream>

using namespace std;

int main()
{
    string input; //User input
    char a; //Individual characters
    int score(0); //Final score
    int numa(0); //Number of a
    int numg(0); //Number of g
    int numm(0); //Number of m
    int numf(0); //Number of f
    int numk(0); //Number of k
    int numj(0); //Number of j

    cout << "Enter Text: "; //Requesting and storing user input
    cin >> input;
    cout << endl;

    a = input[0];
    cout << a;
    if(a != '!' || a != '.')
    {
        int num(0);
        while(num<=input.length())
        {
            a = input[num];
            switch(a)
            {
                case 'a':
                case 'A':
                    score += 1;
                    numa++;
                    break;

                case 'g':
                case 'G':
                    score += 2;
                    numg++;
                    break;

                case 'm':
                case 'M':
                    score += 3;
                    numm++;
                    break;

                case 'f':
                case 'F':
                    score += 4;
                    numf++;
                    break;

                case 'k':
                case 'K':
                    score += 5;
                    numk++;
                    break;

                case 'j':
                case 'J':
                    score += 8;
                    numj++;
                    break;

                default:
                    break;
            }
            num++;
        }
    }
    cout << "Number of a's (worth 1 point each): " << numa << endl;
    cout << "Number of g's (worth 2 point each): " << numg << endl;
    cout << "Number of m's (worth 3 point each): " << numm << endl;
    cout << "Number of f's (worth 4 point each): " << numf << endl;
    cout << "Number of k's (worth 5 point each): " << numk << endl;
    cout << "Number of j's (worth 8 point each): " << numj << endl;
    cout << endl;
    cout << "Total score: " << score;
}

This program takes a user input and gives it a score based on what is contained in it. The letter a gives 1 point, g gives 2, m gives 3, f gives 4, k gives 5, and j gives 8. However, it only goes until there is a "!" or ".". So, while aaa! and aaa. gives 3 points, aaa!aa and aaa.aa should also only give 3 points for both since everything after "!" and "." is disregarded. It also shouldn't run if the string doesn't end with "!" or ".", aaaa! and aaaa. returns 4 points for both while aaaa returns nothing. Trouble is, if there is a space or say letter t which isn't defined in the switch statements, the program just doesn't run. The end goal is for this statement, "she made a familiar attempt Just right." to return a score of 28.

INeedHelp101
  • 55
  • 1
  • 7
  • I think you need to read some more in your favourite book about what `>>` does with whitespace. – molbdnilo Feb 13 '17 at 09:25
  • Hi molbdnilo, I'm still very new to C++. Does >> handle whitespace differently from a regular character? – INeedHelp101 Feb 13 '17 at 09:29
  • It does, yes. Whatever material you're learning from should teach this, but if it doesn't there is a list of good books [here](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Feb 13 '17 at 09:42

1 Answers1

0

Try moving the default action of the switch to the bottom of your other cases and instead of using continue as an action rather use break

switch(a){
    case 'a':
    case 'A':
     .....
    default:
         break;
}

Also the else if is not needed, you don't have to test for the other condition. You can replace it with a normal else clause

  • Alright, that fixed my issue with other characters, but it seems whitespace is still causing something to break. Another comment said >> handled whitespace differently. But I'm a newbie and don't fully understand it. – INeedHelp101 Feb 13 '17 at 09:31