Cout and char

Sep 4, 2019 at 9:28pm
Im trying to print a single letter grade in this program. But I keep running into issues with cout not printing the contents of my char variables. How should I go about printing the letter grade to screen?

Thank you.

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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;

void srand(unsigned int);

int main()
	{
		/*							SEED RAND							*/
		srand (time(NULL));
		/*							Declarations							*/
		char grade1, grade2, grade3, grade4, grade;
		/*								TEST1								*/
		cout<<"Generating score for test 1...\n\n";
		float test1=rand()%100;
		cout<<"Score for test 1 is "<<test1<<"\n\n";
		if (test1>=90)
			{
				char grade1='A';
			}
		else
			{
				if((test1>=80)&&(test1<=89))
					{ 
						char grade1='B';
					}
				else
					{
						if((test1>=70)&&(test1<=79))
							{
								char grade1='C';
							}
						else
							{
								if((test1>=60)&&(test1<=69))
									{
										char grade1='D';
									}
								else
									{
										if((test1>=0)&&(test1<=59))
											{
												char grade1='E';
											}
									}
							}
					}
			}
		cout<<"The grade letter for test 1 is "<<char(grade1)<<"\n\n";
		/*								TEST 2								*/
		cout<<"Generating score for test 2...\n\n";
		float test2=rand()%100;
		cout<<"Score for test 2 is "<<test2<<"\n\n";
				if (test2>=90)
			{
				char grade2='A';
			}
		else
			{
				if((test2>=80)&&(test2<=89))
					{ 
						char grade2='B';
					}
				else
					{
						if((test2>=70)&&(test2<=79))
							{
								char grade2='C';
							}
						else
							{
								if((test2>=60)&&(test2<=69))
									{
										char grade2='D';
									}
								else
									{
										if((test2>=0)&&(test2<=59))
											{
												char grade2='E';
											}
									}
							}
					}
			}
		cout<<"The grade letter for test 2 is "<<grade2<<"\n\n";
		/*								TEST 3								*/
		cout<<"Generating score for test 3...\n\n";
		float test3=rand()%100;
		cout<<"Score for test 3 is "<<test3<<"\n\n";
				if (test3>=90)
			{
				char grade3='A';
			}
		else
			{
				if((test3>=80)&&(test3<=89))
					{ 
						char grade3='B';
					}
				else
					{
						if((test3>=70)&&(test3<=79))
							{
								char grade3='C';
							}
						else
							{
								if((test3>=60)&&(test3<=69))
									{
										char grade3='D';
									}
								else
									{
										if((test3>=0)&&(test3<=59))
											{
												char grade3='E';
											}
									}
							}
					}
			}
		cout<<"The grade letter for test 3 is "<<grade3<<"\n\n";
		/*								TEST 4								*/
		cout<<"Generating score for test 4...\n\n";
		float test4=rand()%100;
		cout<<"Score for test 4 is "<<test4<<"\n\n";
				if (test4>=90)
			{
				char grade4='A';
			}
		else
			{
				if((test4>=80)&&(test4<=89))
					{ 
						char grade4='B';
					}
				else
					{
						if((test4>=70)&&(test4<=79))
							{
								char grade4='C';
							}
						else
							{
								if((test4>=60)&&(test4<=69))
									{
										char grade4='D';
									}
								else
									{
										if((test4>=0)&&(test4<=59))
											{
												char grade4='E';
											}
									}
							}
					}
			}
		cout<<"The grade letter for test 4 is "<<grade4<<"\n\n";
		/*					Calculate Average						*/
		float average=(test1+test2+test3+test4)/4;
				if (average>=90)
			{
				char grade='A';
			}
		else
			{
				if((average>=80)&&(average<=89))
					{ 
						char grade='B';
					}
				else
					{
						if((average>=70)&&(average<=79))
							{
								char grade='C';
							}
						else
							{
								if((average>=60)&&(average<=69))
									{
										char grade='D';
									}
								else
									{
										if((average>=0)&&(average<=59))
											{
												char grade='E';
											}
									}
							}
					}
			}
		cout<<"The grade average is "<<average<<"\n";
		cout<<"The letter average is "<<grade<<"\n";
		return 0;
	}
Sep 4, 2019 at 9:30pm
Grad1 is deleted before you cout it.

Edit: just declare grade1 outside the scope of the if statements and then make it equal to whatever also thanks for using code tags

So example:

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
cout<<"Score for test 1 is "<<test1<<"\n\n";
char grade1;
		if (test1>=90)
			{
				grade1='A';
			}
		else
			{
				if((test1>=80)&&(test1<=89))
					{ 
						grade1='B';
					}
				else
					{
						if((test1>=70)&&(test1<=79))
							{
								grade1='C';
							}
						else
							{
								if((test1>=60)&&(test1<=69))
									{
										grade1='D';
									}
								else
									{
										if((test1>=0)&&(test1<=59))
											{
												 grade1='E';
											}
									}
							}
					}
			}
		cout<<"The grade letter for test 1 is "<< grade1 <<"\n\n";
Last edited on Sep 4, 2019 at 9:37pm
Sep 6, 2019 at 6:54pm
Hello Arcad31a,

Since you have not marked this as being finished I will add this that might be helpful.

Looking at your code I have the feeling that you do not have a good understanding of scope. These are worth reading:
http://www.cplusplus.com/doc/tutorial/namespaces/
https://www.learncpp.com/cpp-tutorial/4-3a-scope-duration-and-linkage-summary/
and the main page of https://www.learncpp.com/

When you write:
1
2
3
4
if (test1 >= 90)
{
    grade1 = 'A';
}

The {}s define a new scope. So when you write:
1
2
3
4
if (test1>=90)
{
    char grade1='A';
}

This is different from what you defined on line 13 and because it is defined as a local variable to the if statement using the same name is not a problem. The problem is that what you defined on line 13 is not being used because of the local variable and when you reach the closing } of the if statement the variable and it value are lost.

As highwayman has pointed out by not defining "grade1" and the othere inside the scope of the if statement you will be using the variables defined on line 13.

Some things I have found over the time I have been here:

1
2
using namespace std;  // <--- Best not to use.
// A recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/ 


Be careful about naming your functions the same as functions that already exist, like "srand". I think the only reason it is working is that there is no function definition for "srand()" that you wrote and that the prototype is the same as the function from "cstdlib".

Speaking of "srand this is a better way to write the function call: srand(static_cast<size_t>(time(nullptr)));. As noted in your prototype "srand takes an "unsigned int", but "time" does not return an unsigned number. Somewhere I read this and now am not sure where I found it, but for the parameter for time zero(0) or (NULL) may work, but (nullptr) is the better choice.

If for no other reason than to find out the problems of using "rand" this video is helpful: https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful

For actually using "rand" you may find it useful to call "rand" before your first actual use to generate a better random number. Something I once read mentioned doing this when using the "Windows" operating system.
1
2
rand();
float test1 = rand() % 100;

Two things about this bit of code:
"rand()" returns an "int" not a "float". Although not a problem there is no need for the float. Also "double" is the preferred floating point type. Now if you are looking for a floating point answer you will need to do some division on the rhs of the = to get that kind of an answer.

Second using "% 100" will give you a return value between zero(0) and (99). If you add one it will allow a result of (100), but you will loose (0). If you want (0) - (100) you will need to use "% 101" here.

Your if statements:
1
2
3
4
if (test1 >= 90)
{
	grade1 = 'A';
}

Are repeated five different times, with a slight variation, and screams for a function. The code can very easily be changed to make it generic and the function can be called every time you need a letter grade.

If you are not up to writing separate functions yet that is OK as the code you have will work, but could be shortened. If you need some help writing a function let me know.

Near the end of the program you have float average = (test1 + test2 + test3 + test4) / 4;. First off it would be better to write this as float average = (test1 + test2 + test3 + test4) / 4.0; so the compiler does not have to promote the (4) to (4.0). Just a small thing, but it helps. And if by chance you change the (test) variables to "int"s to work with "rand()" you would basically be doing integer division which would leave you with only a whole number in "average".

But sometimes this line of code will return a number with a decimal value, so it does have its use on occasion.

Of the testing I did when there was a decimal part to the "average" it was only two numbers. I do not know if you have learned about this yet, but if you include the header file "<iomanip>" and this line of code:
std::cout << std::fixed << std::showpoint << std::setprecision(2); your output will always be rounded to two decimal places and it will print ".00" if needed. It tends to look better when the output is consistent.

The above line of code only needs to be done once, so I would put it after the call to "srand". This will affect every "cout" statement until it is changed.

Hope that helps,

Andy
Sep 18, 2019 at 11:36pm
Thanks alot to both Andy and Highwayman. You're answers are the type of thoroughness I need. I haven't had a chance to review this and implement it. As I'm studying C++ in school while working full-time. But I will as my professor seems to encourage the use of forums and google to learn the ins and outs of C/C++.

I'll review the code and submit it again here to review within the next week.

Thanks again, Guys!
Topic archived. No new replies allowed.