Static cast assignment help

Hello everyone,
I was assigned a homework assignment where I have to calculate the weighted average for a student. However, the problem is that my total average comes out as a 0. I was thinking that maybe it is an issue with the multiplication of integers and the floating numbers and that I should probably use static cast, but I do not know if I need to convert the double to an integer or vice versa, or where exactly I should be using it. Any help or hints would be greatly appreciated! :)


/*This program takes the user's name, course, semester, and grades, and creates a report containing the
student's name, course semester, weighted average for each category, and the letter grade.*/

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

void gradeReport(string, string, string, string); // Prototype
int main()
{
//Define variables
double examOne, examTwo, examThree, labOne, labTwo, labThree, projectOne, quizOne, quizTwo, quizThree;
double examGrades, labGrades, projectGrade, quizGrades;
string firstName, lastName, courseName, semester;
// Get user input (The student's full name, course, semester, and grades.
cout << "Enter your first and last name, leave a space between the names." << endl;
cin >> firstName;
cin >> lastName;
cout << "Enter the name of your course:" << endl;
cin >> courseName;
cout << "Enter the semester you are currently in:" << endl;
cin >> semester;
cout << "Please enter the grade for your first exam (Must be 0-100)" << endl;
cin >> examOne;
cout << "Please enter the grade for your second exam (Must be 0-100)" << endl;
cin >> examTwo;
cout << "Please enter the grade for your third exam (Must be 0-100)" << endl;
cin >> examThree;
cout << "Please enter the grade for your first Lab (Must be 0-100)" << endl;
cin >> labOne;
cout << "Please enter the grade for your second Lab (Must be 0-100)" << endl;
cin >> labTwo;
cout << "Please enter the grade for your third Lab (Must be 0-100)" << endl;
cin >> labThree;
cout << "Please enter the grade for your project (Must be 0-100)" << endl;
cin >> projectOne;
cout << "Please enter the grade for your first quiz (Must be 0-100)" << endl;
cin >> quizOne;
cout << "Please enter the grade for your second quiz (Must be 0-100)" << endl;
cin >> quizTwo;
cout << "Please enter the grade for your third quiz (Must be 0-100)" << endl;
cin >> quizThree;


gradeReport(firstName, lastName, courseName, semester); //Calling
system("pause");
return 0;
}
void gradeReport(string firstName, string lastName, string courseName, string semester)

{
int examOne{}, examTwo{}, examThree{}, labOne{}, labTwo{}, labThree{}, projectOne{}, quizOne{}, quizTwo{}, quizThree{};
int examAvg, labAvg, projectAvg, quizAvg, finalAverage;
int examGrades{}, labGrades{}, projectGrade{}, quizGrades{};
double examWeight = .10, labWeight = .10, projectWeight = .30, quizWeight = .10;


//The formula below adds up the grades for each individual category.
examGrades = (examOne + examTwo + examThree) / 3;
labGrades = (labOne + labTwo + labThree) / 3;
projectGrade = (projectOne) / 1;
quizGrades = (quizOne + quizTwo + quizThree) / 3;

//The formula below takes the sum of each category above and multiplies it to the weight percentage.

examAvg = examGrades * .30;
labAvg = labGrades * .30;
projectAvg = projectGrade * .30;
quizAvg = quizGrades * .10;

//This formula adds up all of the weighted averages above in order to get the final average.
finalAverage = examAvg + labAvg + projectAvg + quizAvg;


//Display Results
/*The code below prints out the weight percentages per category, the average of each category,
the final weighted average, and the letter grade*/


cout << "Below will be the weight categories and the grades for each category." << endl;
cout << "Exams: 30% (3 @ 10% each)" << endl;
cout << "Labs: 30% (3 labs @ 10% each)" << endl;
cout << "Project: 30% (1 project)" << endl;
cout << "Quizzes: 10% (3 quizzes)" << endl;
cout << "The average grade for exams is a: " << examAvg << endl;
cout << "The average grade for labs is a: " << labAvg << endl;
cout << "The average grade for the project is a: " << projectAvg << endl;
cout << "The average grade for quizzes is a: " << quizAvg << endl;
cout << "The Final Average for " << firstName << " " << lastName << " in " << courseName << " for the " << semester << " semester is: " << finalAverage << endl;
if (finalAverage <= 100)
cout << "Your letter grade is an A." << endl;
else if (finalAverage <= 89)
cout << "Your letter grade is a B. " << endl;
else if (finalAverage <= 79)
cout << "Your letter grade is a C. " << endl;
else if (finalAverage <= 69)
cout << "Your letter grade is a D. " << endl;
else if (finalAverage <= 59)
cout << "Your letter grade is an F. " << endl;
else if (finalAverage < 0)
cout << "Invalid Data. " << endl;
else
cout << "Invalid Data. " << endl;
}
Hello BitterStrawberry,

While I sort out your program I offer this:

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



Also adding a vew blank lines to break up the code helps.

Andy
The problem is most likely integer division. You can avoid that by making one part floating point, like so:

examGrades = (examOne + examTwo + examThree) / 3.0;
Hello BitterStrawberry,

In "main" you define all your numeric variables as "double"s. This is good.

In the function "gradeReport" you define all your numeric variables as "int"s. This is a problem.

As coder777 has pointed out the function is doing integer division and even when you are not you are storing the result of the calculation into and "int" loosing the decimal portion.

The next problem is that you are defining local variables
examOne{}, examTwo{}, examThree{}, labOne{}, labTwo{}, labThree{}, projectOne{}, quizOne{}, quizTwo{}, quizThree{};. These variables do not see the values given to the variables back inn "main". All these variables need to be passed from "main" to the function, so the function has something to work with and they should be defined as "double"s not "int"s.

Something like this should work better for you:
1
2
3
4
5
6
7
8
9
10
11
12
void gradeReport(string firstName, string lastName, string courseName, string semester
	double examOne, double examTwo{}, double examThree{}, 
	double labOne{}, double labTwo{}, double labThree{}, 
	double projectOne{},
	double quizOne{}, double quizTwo{}, double quizThree{})

{
	constexpr double EXAMWEIGHT = .10, LABWEIGHT = .10, PROJECTWEIGHT = .30, QUIZEWEIGHT = .10;

	//int examOne{}, examTwo{}, examThree{}, labOne{}, labTwo{}, labThree{}, projectOne{}, quizOne{}, quizTwo{}, quizThree{};
	double examAvg, labAvg, projectAvg, quizAvg, finalAverage;
	double examGrades{}, labGrades{}, projectGrade{}, quizGrades{};

The line in bold is a better way of doing this. The later in the function the line:
examAvg = examGrades * .30; // <--- I believe you want to use EXAMWEIGHT and not ".30" which should be "0.30".
would change to:examAvg = examGrades * EXAMWEIGHT;. This way the weights can not be changed in the function.

In your "cout" statements you end every line with "endl". This is OK, but the new line "\n" will work just as well and save the "endl" for the last line. Most of the time the "\n" will flush the output buffer when it is encountered at the end of a string or "cout" statement.

Beyond that I have not yet loaded the program and tried to compile or run it. When I do if I find anything else I will let you know.

Hope that helps,

Andy
Hello BitterStrawberry,

This does not fit in one message, so I may have to put it into two or three parts. Sorry about. This is due to the code being larger than what you originally posted.

I have been working on your program and made some changes to help bypass the input part and make checking the the function easier. Instead of trying to explain everything I will just show you the code.

End part 1.
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
#include <iostream>
#include <iomanip>
#include <string>

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

// <--- A better alternative
using std::cout;
using std::cin;
using std::endl;
using std::string;

// Prototypes
void gradeReport(string firstName, string lastName, string courseName, string semester,
	double examOne, double examTwo, double examThree,
	double labOne, double labTwo, double labThree,
	double projectOne,
	double quizOne, double quizTwo, double quizThree);

//  ********** Used for testing. Can be removed later. ******************
void DisplayInput(string firstName, string lastName, string courseName, string semester,
	double examOne, double examTwo, double examThree,
	double labOne, double labTwo, double labThree,
	double projectOne,
	double quizOne, double quizTwo, double quizThree);

int main()
{
	//   Define variables
	//double examOne, examTwo, examThree, labOne, labTwo, labThree, projectOne, quizOne, quizTwo, quizThree;
	//string firstName, lastName, courseName, semester;
	////double examGrades, labGrades, projectGrade, quizGrades; // <--- These variables not used in "main".

//  ********** Used for testing. Can be removed later. ******************
	double examOne{ 90 }, examTwo{ 95 }, examThree{ 99 },
		labOne{ 100 }, labTwo{ 100 }, labThree{ 100 },
		projectOne{ 96 },
		quizOne{ 90 }, quizTwo{ 95 }, quizThree{ 98 };
	string firstName{ "John" }, lastName{ "Smithe" }, courseName{ "CSI 101" }, semester{ "Summer" };

	DisplayInput(firstName, lastName, courseName, semester,
		examOne, examTwo, examThree,
		labOne, labTwo, labThree,
		projectOne,
		quizOne, quizTwo, quizThree);
//  ********************************************************

	std::cout << std::fixed << std::showpoint << std::setprecision(2);  // <--- Leave this line.

//  Uncomment when done testing
	// Get user input (The student's full name, course, semester, and grades.
	//std::cout << "Enter your first and last name, leave a space between the names. ";
	//std::cin >> firstName;
	//std::cin >> lastName;

	//std::cout << "Enter the name of your course: ";
	//std::cin >> courseName;

	//std::cout << "Enter the semester you are currently in: ";
	//std::cin >> semester;

	//std::cout << "Please enter the grade for your first exam (Must be 0-100): ";
	//std::cin >> examOne;

	//std::cout << "Please enter the grade for your second exam (Must be 0-100): ";
	//std::cin >> examTwo;

	//std::cout << "Please enter the grade for your third exam (Must be 0-100): ";
	//std::cin >> examThree;

	//std::cout << "Please enter the grade for your first Lab (Must be 0-100): ";
	//std::cin >> labOne;

	//std::cout << "Please enter the grade for your second Lab (Must be 0-100): ";
	//std::cin >> labTwo;

	//std::cout << "Please enter the grade for your third Lab (Must be 0-100): ";
	//std::cin >> labThree;

	//std::cout << "Please enter the grade for your project (Must be 0-100): " << std::endl;
	//std::cin >> projectOne;

	//std::cout << "Please enter the grade for your first quiz (Must be 0-100): ";
	//std::cin >> quizOne;

	//std::cout << "Please enter the grade for your second quiz (Must be 0-100): ";
	//std::cin >> quizTwo;

	//std::cout << "Please enter the grade for your third quiz (Must be 0-100): ";
	//std::cin >> quizThree;


	gradeReport(firstName, lastName, courseName, semester,
		examOne, examTwo, examThree,
		labOne, labTwo, labThree,
		projectOne,
		quizOne, quizTwo, quizThree); //Calling

	// A fair C++ replacement for "system("pause");".
	// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
	//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue: ";
	std::cin.get();

	return 0;
}

void gradeReport(string firstName, string lastName, string courseName, string semester,
	double examOne, double examTwo, double examThree,
	double labOne, double labTwo, double labThree,
	double projectOne,
	double quizOne, double quizTwo, double quizThree)

{
	constexpr double EXAMWEIGHT{ 0.10 }, LABWEIGHT{ 0.10 }, PROJECTWEIGHT{ 0.30 }, QUIZEWEIGHT{ 0.10 };

	//int examOne{}, examTwo{}, examThree{}, labOne{}, labTwo{}, labThree{}, projectOne{}, quizOne{}, quizTwo{}, quizThree{};
	double examAvg{}, labAvg{}, projectAvg{}, quizAvg{}, finalAverage{};
	double examGrades{}, labGrades{}, projectGrade{}, quizGrades{};


	//The formula below adds up the grades for each individual category.
	examGrades = (examOne + examTwo + examThree) / 3.0;  // <--- Changed.
	labGrades = (labOne + labTwo + labThree) / 3;
	projectGrade = (projectOne) / 1; // <--- () not needed, but OK if you leave them. Also the "/ 1" is not needed.
	quizGrades = (quizOne + quizTwo + quizThree) / 3;

	//The formula below takes the sum of each category above and multiplies it to the weight percentage.
	//examAvg = examGrades * .30; // <--- I believe you want to use EXAMWEIGHT and not ".30" which should be "0.30"
	examAvg = examGrades * EXAMWEIGHT;
	labAvg = labGrades * .30;
	projectAvg = projectGrade * .30;
	quizAvg = quizGrades * .10;

	//This formula adds up all of the weighted averages above in order to get the final average.
	finalAverage = examAvg + labAvg + projectAvg + quizAvg;


	//Display Results
	/*The code below prints out the weight percentages per category, the average of each category,
	the final weighted average, and the letter grade*/


	std::cout << "\n\n Below will be the weight categories and the grades for each category.\n\n";

	std::cout << " Exams: 30% (3 @ 10% each)" << '\n';
	std::cout << " Labs: 30% (3 labs @ 10% each)" << '\n';
	std::cout << " Project: 30% (1 project)" << '\n';
	std::cout << " Quizzes: 10% (3 quizzes @ 3.33% each)" << '\n';

	std::cout << "\n The average grade for exams is a: " << examAvg << '\n';
	std::cout << " The average grade for labs is a: " << labAvg << '\n';
	std::cout << " The average grade for the project is a: " << projectAvg << '\n';
	std::cout << " The average grade for quizzes is a: " << quizAvg << '\n';

	std::cout << "\n The Final Average for " << firstName << " " << lastName << " in " << courseName << " for the " << semester << " semester is: " << finalAverage << '\n' << std::endl;

	if (finalAverage <= 100.0)
		std::cout << " Your letter grade is an A." << std::endl;
	else if (finalAverage <= 89.0)
		std::cout << " Your letter grade is a B. " << std::endl;
	else if (finalAverage <= 79)
		std::cout << " Your letter grade is a C. " << std::endl;
	else if (finalAverage <= 69)
		std::cout << " Your letter grade is a D. " << std::endl;
	else if (finalAverage <= 59)
		std::cout << " Your letter grade is an F. " << std::endl;
	else if (finalAverage < 0)
		std::cout << "    Invalid Data. " << std::endl;
	else
		std::cout << "    Invalid Data. " << std::endl;

	//if (finalAverage == 100.0)
	//	std::cout << " Your letter grade is an A. Extra message" << std::endl;
	//else if (finalAverage < 100.0 && finalAverage >= 90.0) // <--- Catches anything between 90.0 and 99.99.
	//	std::cout << " Your letter grade is an A." << std::endl;
	//else if (finalAverage < 90.0 && finalAverage >= 80.0) // <--- Catches anything between 80.0 and 89.99.
	//	std::cout << " Your letter grade is a B. " << std::endl;
	
}


End part 2.
Part 3.

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
//  ********** Used for testing. Can be removed later. ******************
void DisplayInput(string firstName, string lastName, string courseName, string semester,
	double examOne, double examTwo, double examThree,
	double labOne, double labTwo, double labThree,
	double projectOne,
	double quizOne, double quizTwo, double quizThree)
{
	std::cout << "\n Enter your first and last name, leave a space between the names. " << firstName << ' ' << lastName << '\n';

	std::cout << "\n Enter the name of your course: " << courseName << '\n';

	std::cout << " Enter the semester you are currently in: " << semester << '\n';

	std::cout << "\n Please enter the grade for your first exam (Must be 0-100): "<< examOne << '\n';

	std::cout << " Please enter the grade for your second exam (Must be 0-100): " << examTwo << '\n';

	std::cout << " Please enter the grade for your third exam (Must be 0-100): " << examThree << '\n';

	std::cout << "\n Please enter the grade for your first Lab (Must be 0-100): " << labOne << '\n';

	std::cout << " Please enter the grade for your second Lab (Must be 0-100): " << labTwo << '\n';

	std::cout << " Please enter the grade for your third Lab (Must be 0-100): " << labThree << '\n';

	std::cout << "\n Please enter the grade for your project (Must be 0-100): " << projectOne << '\n';

	std::cout << "\n Please enter the grade for your first quiz (Must be 0-100): " << quizOne << '\n';

	std::cout << " Please enter the grade for your second quiz (Must be 0-100): " << quizTwo << '\n';

	std::cout << " Please enter the grade for your third quiz (Must be 0-100): " << quizThree << '\n';
}

This gives your variables values and allows you to skip the input section each time the program eliminating the need to enter the same information over and over. Also it allows you to use the same information to see if your numbers come out right.

For the function "gradeReport" you have some problems there that need corrected.

Line 116 you have started with the right idea, but it works better if you make the variables constants. Also some of the values are not correct.

The reason line 118 is commented is because they are now defined in the function parameters.

Lines 124 - 127 do the job. The magic number of (3) should be (3.0) since all the rest of the variables are defined as "double"s. Although the compiler will promote this "int" to a "double" before the calculation, but it is more proper to use the (3.0) to start with.

Line 131 shows the way these lines should be done, but since two of the variables are wrong the first two calculations produce the wrong result. Later it occured to me that since you only have two different values you could create the constant variables something like "THIRTYPER" and "TENPER" you can spell out "percent" if you like and maybe "NUNTESTS" for whrer you divide by (3.0) so you can replace the magic number. Just a thought.

Lines 145 - 157 I not only broke this up to make it easier to read the use of the "\n"s in the "cout" statements breaks up the output. I think you may like the way the output looks now.

Starting at line 159 the if statements do not work the way you are thinking. First you are trying to compare a "double" to an "int". The compiler may promote the "int" to a double" before the comparison, but it is more proper to use (100.0" to start with. Then if you want to use the if/else if statements the way you ar they need to be reversed checking "finalAverage < 0" first and "finalAverage <= 100.0" last. As it is when "finalAverage" has a value of say "77.70" it prints a letter grade of "A" since "77.70" is less than 100.0 and the rest is bypassed to end the function.

Generally in something like this checking for a range is the more prudent way as demenstrated by the commented code.

Once you get everything fixed up it will produce the output desired and make more sense.

End part 3. Sorry, I just realized this may throw off the line numbers. Then again maybe not. I hope it is not to confusing.

Hope that helps,

Andy
Last edited on
Topic archived. No new replies allowed.