So I'm writing an exercise/check-for-comprehension program. I have to write a program that asks, "What letter grade do you deserve?" Then, the user replies by entering in a letter (A,B, or C; D and F are left out because of the gap between the letters, and this is a novice program, so assume the user types A, B, or C). The program then says,"This is the grade you are going to get:" and displays one letter grade lower than the user entered. In other words, if I enter an A, I get a B, and if I enter a B, I get a C, and finally, if put C, I get a D.
I'm not allowed to use the IF and ELSE techniques. I'm guessing, by the nature of this program, I have to use pointer arithmetic methods.
Well... although strictly speaking it is not portable, in practice, you will be using an ASCII based character set, so the characters 'A', 'B', 'C' and 'D' are in sequence such that 'A' + 1 == 'B', 'B' + 1 == 'C' and 'C' + 1 == 'D'. You can make use of this to compute the answer.
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar
So I'm writing an exercise/check-for-comprehension program. I have to write a program that asks, "What letter grade do you deserve?" Then, the user replies by entering in a letter (A,B, or C; D and F are left out because of the gap between the letters, and this is a novice program, so assume the user types A, B, or C). The program then says,"This is the grade you are going to get:" and displays one letter grade lower than the user entered. In other words, if I enter an A, I get a B, and if I enter a B, I get a C, and finally, if put C, I get a D.
I'm not allowed to use the IF and ELSE techniques. I'm guessing, by the nature of this program, I have to use pointer arithmetic methods.
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
int main()
{
char letterGrade;
cout <<"What letter grade do you deserve? ";
cin >> letterGrade;
letterGrade = toupper(letterGrade);
// changes the letter if it is lower case to uppercase
while( int(letterGrade) < 65 || int(letterGrade) > 70) // (A-F) from 65-70
{
cout <<"You did not enter a valid choice." << endl;
cout <<"Enter a letter from A-F :";
cin >> letterGrade;
letterGrade = toupper(letterGrade);
}
//subract one letter grade
switch(letterGrade)
{
case 'A':
cout <<"The grade you deserve is a B!" << endl;
break;
case 'B':
cout <<"The grade you deserve is a C!" << endl;
break;
case 'C':
cout <<"The grade you deserve is a D!" << endl;
break;
case 'D':
cout <<"The grade you deserve is an F!" << endl;
break;
case 'F':
break;
cout <<"The grade you deserve is an F!" << endl;
}
return 0;
}
You can substitute the pointer arithmetic what the user above me has written
Last edited by yellowMonkeyxx; April 18th, 2010 at 04:40 PM.
Bookmarks