### Converting ASCII

Please point out the errors, and it seems that the letter variables that the user inputs are all converting to decimal numbers according to ASCII, like a=97,b=98,etc....

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114`` ``````#include using namespace std; int main() { char grade1,grade2,grade3; grade1=0, grade2=0, grade3=0; int cr1,cr2,cr3; double gpa; cout<<"Enter First Grade: "; cin>>grade1; switch(grade1) { case 'A': case 'a': grade1 += 4; break; case 'B': case 'b': grade1 += 3; break; case 'C': case 'c': grade1 += 2; break; case 'D': case 'd': grade1 += 1; break; case 'F': case 'f': grade1 += 0; break; } cout<<"Credits: "; cin>>cr1; cout<<"Enter Second Grade: "; cin>>grade2; switch(grade2) { case 'A': case 'a': grade2 += 4; break; case 'B': case 'b': grade2 += 3; break; case 'C': case 'c': grade2 += 2; break; case 'D': case 'd': grade2 += 1; break; case 'F': case 'f': grade2 += 0; break; } cout<<"Credits: "; cin>>cr2; cout<<"Enter Third Grade: "; cin>>grade3; switch(grade3) { case 'A': case 'a': grade3 += 4; break; case 'B': case 'b': grade3 += 3; break; case 'C': case 'c': grade3 += 2; break; case 'D': case 'd': grade3 += 1; break; case 'F': case 'f': grade3 += 0; break; } cout<<"Credits: "<>cr3; cout<
`Please point out the errors`

What errors?

 it seems that the letter variables that the user inputs are all converting to decimal numbers according to ASCII, like a=97,b=98,etc....

char variables are, basically, integers. Each ASCII character has a corresponding numerical integer value, as specified by the standard. You can perform numerical operations on them like any other integer, except that the maximum value a single unsigned char can hold is 255.

is there a way to capture the variable value when user input "A", the variable becomes 4, and when input "B", the variable becomes 3, and etc ?
Yes. Just use a normal variable. Declare it before the switch statement, and then in each case, set it to the value you want it to be.
can you show me ?
How to declare a variable? Or how to set its value? Because you've successfully done both in your code already, so I'm not sure why this is a problem.
Last edited on
I think you should be using separate varuables for the characters (such as 'A' and the numeric values such as 4).
 ``1234567891011121314151617`` `````` char grade; int score1, score2, score3; cout<<"Enter First Grade: "; cin>>grade; switch(grade) { case 'A': case 'a': score1 = 4; break; case 'B': case 'b': score1 = 3; break; // etc. }``````

Though I think for repetitive code such as this you should use arrays and a loop.
Example(incomplete):
 ``123456789101112131415161718192021222324`` `````` char grade; int score[3]; int cr[3]; for (int i=0; i<3;i++) { cout << "Enter Grade " << (i+1) << " : "; cin >> grade; switch(grade) { case 'A': case 'a': score[i] = 4; break; case 'B': case 'b': score[i] = 3; break; // etc. } cout << "Credits: "; cin >> cr[i]; }``````

thx chervil, i never thought of creating another variable for the characters....

and because im new to C++, what does it do by putting a number in side brackets like:
 ``12`` ``````int score[3]; int cr[3];``````
Perhaps you want to declare the variables grade{1, 2, 3} as integer, then have a one char variable that reads input.

E: looks like my post was 6 posts late :s
Last edited on
americanxo, have you read through the tutorial on this site?
 americanxo, have you read through the tutorial on this site?

I did, but still getting confused sometimes
You can declare three separate variables like this:
 ``123`` ``````int a; int b; int c;``````

Here's how to declare an array of three variables:
`int score[3];`
Using an array is often simpler.
To access each element, use a subscript in the range 0 to 2.
These two lines do more or less the same thing:
 ``12`` ``````cin >> b: cin >> score[1];``````

There's section of the tutorial on arrays.Though I'd recommend going through the tutorial from the start.
http://www.cplusplus.com/doc/tutorial/arrays/
Last edited on
and by the way, if i use the looping, how do i calculate the weighted score ? like grade1 * cr1 = weighted score
do i use score[1]*cr[1] ?
 do i use score[1]*cr[1] ?

Instead of this: `(cr1+cr2+cr3)` you would use `(cr[0]+cr[1]+cr[2])`
and so on.
wow, thanks a ton, everything works perfectly now, and one last questions

 `` `` `` cout << "Enter Grade " << (i+1) << " : ";``

the `(i+1)` is optional right ? it just adds the number or order after the words within the double quotations right ?
It just helps the user to keep track of which value they are meant to be entering next. Yes, it's optional, but the user needs some sort of guidance.

You could even go for a de-luxe version like this:
 ``1234`` `````` string label[3] = {"First", "Second", "Third"}; for (int i=0; i<3; i++) cout << "Enter " << label[i] << " Grade: " << endl;``````

Output:
 ```Enter First Grade: Enter Second Grade: Enter Third Grade: ```
oh wow, thanks alot, i owe you a beer.....
Topic archived. No new replies allowed.