/* This program converts grades from letters to numbers and back
  Inputs: 
   number or letter
  Outputs:
    number or letter the inverse of the input
  Logic:
    First decide if it is a number or letter, then convert it.
  Test Cases:
  1) A, 4.0
  2) 1.5, D
  3)  >, Error
*/
 
#include <iostream>
#include <iomanip>
#include <math.h>
#include <string>
using namespace std;
int main()
{
	char grade = 0;
	float num_grade = 0;
 
	cout << "Enter your Grade"<< endl;
	cin >> num_grade; // First check for a numeric grade
	if (cin.fail()) {  // It must be a Letter
		cout << "Converting from Letter Grade to number" << endl;
		cin.clear();
		cin >> grade;
		if (cin.fail()) {
			cout << "Bad Cin Input Value" << endl;
			system("pause");
			return(-1);
		}
		if (grade == 'A') {
			cout << "Your grade is 4.0" << endl;
		}
		switch(grade) { // Lookup the numeric value
			case 'A': num_grade = 4.0; break;
			case 'B': num_grade = 3.0; break;
			case 'C': num_grade = 2.0; break;
			case 'D': num_grade = 1.0; break;
			default: num_grade = -1; break;  // Anything else is an error
		}
		if(num_grade > 0) {
			cout << "Your grade is " << num_grade << endl;
		} else {
			cout << "Invalid Grade Entered";
		}
	} else {
		cout << "Converting from number to Letter" << endl;
		if((num_grade >= 1) && (num_grade < 2)) grade = 'D';
		if((num_grade >= 2) && (num_grade < 3)) grade = 'C';
		if((num_grade >= 3) && (num_grade < 4)) grade = 'B';
		if(num_grade >= 4) grade = 'A';
	}
	cout << "Grade is "<<grade<<endl;
 
	system("pause");
	return(0);
}
cs-142/grades.txt · Last modified: 2014/09/16 15:19 by kseppi
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0