Your instructor wants a program that will read in a set of grades from a file called “grade.txt” (see below and is available on blackboard under assignments) and report the total number of letter grades i.e. A, B, C, D and F in the file. You program must use case logic and the “switch” statement. See section 2.10. Call your program “HW3b.cpp”.

“grade.txt”

ABADACAFABCDFFFACDCCBBACBACCCBBAAA
AADBACAFFBBCCDAABBFFAACCBBAACCCCBB

anyone that helps... i'd send a thank you via paypal... thanks.

The char type in C/C++ can be viewed as an int so this:

char ch;
cin >> ch;
switch(ch)
{
case '~':
//do something;
break;
}

is the equivalent to this:

char ch;
cin >> ch;
if(ch == '~')
{
//do something;
}

Each additional case in the switch statement is the same as adding an else if statement to the list of if/else ifs.

You need something like the following...expanding on the previous code.

#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
  int grades[5];
  char ch; 

  for (int i = 0; i < 5; i++)
    grades[i] = 0;

  cout << "Enter q to exit, or enter grades" << endl;
  while(ch != 'q')
    {
      cin >> ch;
      switch(ch)
	{
	case 'A':
	  grades[0] += 1;
	  break;
	case 'B':
	  grades[1] +=1;
	  break;
	case 'C':
	  grades[2]+=1;
	  break;
	case 'D':
	  grades[3]+=1;
	  break;
	case 'F':
	  grades[4]+=1;
	  break;
	}
    }

  for(int i = 0; i < 5; i++)
    cout << "grades[" << i << "] = " << grades[i] << endl;

  return 0; 
}

This code will work for inputing grades from the command line. You now need to edit it to read in from a file.

This article has been dead for over six months. Start a new discussion instead.