Assigning char to an array

Hi, i want to assign a char to an array inside an if statement after the user has input the grade as an integer, but it has to fill an array with characters, such as:

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
char grades[5];
int grade;
char A, B, C, D, F;

cout << "Enter da grade" << endl;
cin >> grade;
if (grade < 59)
		{
			grade[0] = F;
		}
		else if (60 < grade < 69)
		{
			grade[0] = D;
		}
		else if (70 < grade < 79)
		{
			grade[0] = C;
		}
		else if (80 < grade < 89)
		{
			grade[0] = B;
		}
		else if ( 90 < grade <= 100)
		{
			grade[0] = A;
		}

A, B, C, D, and F won't transfer to the array, thus giving me the uninitialized variable error in microsoft visual studio 2010. Can anyone help me? Thanks.
char A, B, C, D, F;

The A, B, C, D and F are uninitialized char variables. They are not the same as the char literals 'A', 'B', 'C' etc.

Modify your code as follows:
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
char grades[5];
int grade;
char letter_grade;

cout << "Enter da grade" << endl;
cin >> grade;
if (grade < 59)
		{
			letter_grade = 'F';
		}
		else if (60 < grade < 69)
		{
			letter_grade = 'D';
		}
		else if (70 < grade < 79)
		{
			letter_grade = 'C';
		}
		else if (80 < grade < 89)
		{
			letter_grade = 'B';
		}
		else if ( 90 < grade <= 100)
		{
			letter_grade = 'A';
		}
grade[0] = letter_grade;
wow,
thanks, my code makes alot more sense now
One other thing you want to fix is inside all your if/else if statements you're using < when you want to be using <=. Right now if you enter 60,69,70,79,80,89, or 90 you're program wont assign anything to letter_grade.

else if ( 80 <= grade <= 89)
Is the compiler not giving a warning on the comparisons in the if statements?

 warning: comparisons like 'X<=Y<=Z' do not have their mathematical meaning 


You can flip the order of the grades and do something like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
if (grade >= 90)
{
	letter_grade = 'A';
}
else if (grade >=80)
{
	letter_grade = 'B';
}
else if (grade >= 70)
{
	letter_grade = 'C';
}
else if (grade >= 60)
{
	letter_grade = 'D';
}
else 
{
	letter_grade = 'F';
}


line 27 - your array was called grades, not grade
Topic archived. No new replies allowed.