how to store a letter variable ?

If i want users to input either (A,B,C,D,F), each letter will represent 4,3,2,1,0 respectively, so its like when user input A, then i want to show his input value is 4, what do i do ?

getting confused here, ps i'm still a newb...


What have you written so far?
nothing yet, but i want to get some ideas, or samples if possible
Do you know how to use a switch-case construct? If not, can you use if and else statements?
well, here is what i wrote so far


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;
int main()
{

char letter;
cout<<"enter a letter: "<<endl;

switch(letter)
{
case 'a':
case 'A': cout<<"4"<<endl; //however, i dont want to just show its 4, i want it to be stored, like a=4, can it be done ? 
break;
}


system("pause");
return 0;


}

Why not make an int variable named a on line 8?
the thing is, which letter variable should i make ? users have choices, not just "a", you know what i mean ?
I still don't understand what you want. Can you show sample input and output?
forgive my inexperience in c++ and lack of certain c++ terms....

what i meant was, even i make an int variable named a on line 8, what if user input B, or C, or D, or whatsoever ?

and switch function is only for int and char right ?

is there any better way like if...else if etc ?
What confuses me is that in your first post you asked for one thing and then in your third post you asked for something different.
im sorry to get you confused in the first place, what i want is get user to input few letters, and sum up their corresponding number values and get the average...but, only the part that need to store the letter value confuses me....
Make an int variable and add to it based on which letter was entered.
can you show me ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int total = 0;
switch(letter)
{
case 'a':
case 'A':
    total += 4;
    break;

case 'b':
case 'B':
    total += 3;
    break;

//etc...
}
oh, thanks, i will try that, because professor didnt teach that kinda stuff yet....

by the way, can i do multiple at a time ? or have to repeat it 3 times if i want user to input 3 letters
You can use a looping structure:
http://www.cplusplus.com/doc/tutorial/control/#loops
I recommend the for loop in this situation.
can anyone tell me whats wrong with the codes ?



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; //tried to use int, but when letter is enter, it skips to the end
	grade1=0, grade2=0, grade3=0;
	int cr1,cr2,cr3;
	double gpa;

	cout<<"Enter First Grade: "<<endl;
	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: "<<endl;
		cin>>cr1;
		

	cout<<"Enter Second Grade: "<<endl;
	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: "<<endl;
		cin>>cr2;


	cout<<"Enter Third Grade: "<<endl;
	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<<"Your GPA is: "<<gpa<<endl;



	system("pause");
	return 0;


  }
Why didn't you use a looping structure?
because i am new to c++, and still exploring, thats why i got stuck on capturing letter variables
Topic archived. No new replies allowed.