-
April 18th, 2010, 03:24 PM
#1
I'm a noob, HELP!
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.
-
April 18th, 2010, 03:27 PM
#2
Re: I'm a noob, HELP!
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.
-
April 18th, 2010, 04:07 PM
#3
Re: I'm a noob, HELP!
You can't use if? What about use a switch?
-
April 18th, 2010, 04:14 PM
#4
-
April 18th, 2010, 04:37 PM
#5
Re: I'm a noob, HELP!
Originally Posted by ZainAsad
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.
Code:
char grade = cin.get();
cout << ++grade;
??? That was pretty easy.
-
April 18th, 2010, 04:37 PM
#6
Re: I'm a noob, HELP!
This might help
Code:
#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.
-
April 18th, 2010, 04:55 PM
#7
Re: I'm a noob, HELP!
Pointer arithmetic is hardly required. Simply realizing that chars are basically integers is enough.
-
April 18th, 2010, 07:01 PM
#8
Re: I'm a noob, HELP!
Oh my God, I feel stupid as hell now.
-
April 19th, 2010, 02:05 PM
#9
Re: I'm a noob, HELP!
Originally Posted by yellowMonkeyxx
This might help
You can substitute the pointer arithmetic what the user above me has written
The user above you didn't use pointer arithmetic, and please don't do somebody's homework for them.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|