Input example:
Notes: The following is an even better and more flexible approach - it will:
  • see white space characters (space, tab and return),
  • will only react to valid  grade characters (a, b, c, d or f),
  • allow both upper and lower case versions of those characters, and
  • will stop processing the line when the return charater (\n) is encountered.

Code:

// File: inputTests.cpp
// Date: Sep 10, 2014
// Author: Nicholas Duchon
// Purpose: test cin input handling with char and string

#include <stdio.h>   // for printf
#include <iostream>  // for cin and cout
#include <string>    // for string type
using namespace std;

int main () {
   int count = 0;
   double sum = 0;
   double average;
  
   string st;
   char ch;
   cout << "input string: ";
   cin >> st;
   cout << "String: >" << st << "<" << endl;
  
   while (true) {
      cin >> noskipws >> ch;
      if (ch == '\n') break;
      count ++;
      switch (ch) {
         case 'A':
         case 'a': sum += 4; break;
         case 'B':
         case 'b': sum += 3; break;
         case 'C':
         case 'c': sum += 2; break;
         case 'D':
         case 'd': sum += 1; break;
         case 'F':
         case 'f': break;
         default: count --; // not a valid grade
      } // end switch
//       cout << ">" << ch << "<" << endl;
   } // end while true
   average = sum / count;
   cout << "Average is: " << average << " of " << count << " grades." << endl;
   printf ("Average is: %.2f of %d grades.\n", average, count);
//    do {
//       cin >> noskipws >> ch;
//       cout << ">" << ch << "<" << endl;
//    } while (ch != '\n'); // single quotes!
   cout << "Bye" << endl;
   // sytem ("Pause"); // Visual C++ pause
} // end main

Testing:

input string: Shirley a b aadef a aabbc fjjsie abdcc
String: >Shirley<
Average is: 2.66667 of 18 grades.
Average is: 2.67 of 18 grades.
Bye


By: Nicholas Duchon

end.