**This is an old revision of the document!**


<code cpp> /* 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

  • /
  1. include <iostream>
  2. include <iomanip>
  3. include <math.h>
  4. 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);

} <\code>

cs-142/grades.1410880767.txt.gz · Last modified: 2014/09/16 09: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