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....


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <iostream>

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: "<<endl;
		cin>>cr3;
		cout<<endl;


		gpa=(grade1*cr1+grade2*cr2+grade3*cr3)/(cr1+cr2+cr3);

		cout<<grade1*cr1<<endl;
		cout<<"Your GPA is: "<<gpa<<endl;



	system("pause");
	return 0;


  }
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).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    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):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    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:
1
2
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:
1
2
3
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:
1
2
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] ?
Yes, that looks about right.

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:
1
2
3
4
    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.