Comp Sci Why Is My C++ Program Outputting Zeroes Instead of Averages?

  • Thread starter Thread starter Joe_K
  • Start date Start date
  • Tags Tags
    C++ Calculations
AI Thread Summary
The C++ program is outputting zeroes instead of averages due to integer division, which occurs when both the numerator and denominator are integers. To resolve this, all input variables should remain as integers, but the calculations should cast them to doubles during division to ensure proper floating-point arithmetic. The user should modify the calculation lines to include casting, such as using static_cast<double>(total_program_points) for accurate results. This adjustment will allow the program to compute and display the correct averages. Implementing these changes should resolve the issue of receiving zeroes in the output.
Joe_K
Messages
32
Reaction score
0

Homework Statement



I am in a beginner c++ class and this is my second program. I am having trouble getting the calculations to output correctly. I'm not sure what I'm doing wrong here, maybe somebody can help.

Homework Equations




The program takes user input data for points in a class, like quiz points, test points, etc and calculates averages .


The professor gave us a sample run of the program, which SHOULD output like this, but mine isn't:

Enter total program points: 526
Enter max number of points available: 580

Enter the total quiz points earned: 73
Enter the number of quizzes taken: 12
Enter the sum of the two lowest quiz scores: 1

Enter the total test points that have been earned(0 if no tests taken): 109
Enter the max test points available: 300

So that is the sample input data she gave us, and this is the sample output she gave us:
*******
Course Results

Program average: 90.69
Quiz Average: 72.00
Test average: 45.25
Course average: 63.43



However, when I run my program with the sample input data, my output is all zeroes. Why would this happen?



The Attempt at a Solution



Here is my source code



#include <iostream>
#include <iomanip>

using namespace std;

int total_program_points,
max_program_points,
total_quiz_points,
quizzes_taken,
sum_lowest_quizzes,
total_test_points,
max_test_points;


float program_average,
quiz_average,
test_average,
course_average;


int main()
{










cout<<"*******CSCI 240 Course Average Calculator*******"<<endl
<<"This program will calculate the course average "<<endl
<<"for a student in CSCI 240 after a few weeks of "<<endl
<<" the semester have passed. "<<endl<<endl<<endl<<endl;



cout<<"Enter the total program points that have been earned: ";
cin>> total_program_points;

cout<<endl
<<"Enter the maximum number of program points available: ";
cin>> max_program_points;

cout<<endl<<endl
<<"Enter the total quiz points that have been earned: ";
cin>> total_quiz_points;

cout<<endl
<<"Enter the number of quizzes that have been taken: ";
cin>> quizzes_taken;

cout<<endl
<<"Enter the sum of the two lowest quiz scores: ";
cin>> sum_lowest_quizzes;

cout<<endl<<endl
<<"Enter the total test points that have been earned (0 if no test have been taken): ";
cin>> total_test_points;

cout<<endl
<<"Enter the maximum test points available: ";
cin>> max_test_points;



//CALCULATIONS SECTION

program_average = ((total_program_points/max_program_points)*100);

quiz_average = ((total_quiz_points - sum_lowest_quizzes)/ (quizzes_taken*10)*100);

test_average = ((total_test_points + total_quiz_points - sum_lowest_quizzes)/(max_test_points + (quizzes_taken -2)*10)*100);








cout<<endl<<endl<<endl<<endl
<<"Course results"<<endl<<endl<<endl
<<"Program average: "<<program_average<<endl
<<"Quiz average: "<<quiz_average<<endl
<<"Test average: "<<test_average<<endl
<<"Course average: "<<course_average<<endl;






system ("pause");
return 0;
}
 
Physics news on Phys.org
First of all, make all of your variables doubles.
 
IntegrateMe said:
First of all, make all of your variables doubles.

Normally I would make them all float or double, but the professor specifically said that all of the user input data must be stored as integers.
 
Then cast them as doubles when you divide.
 
IntegrateMe said:
Then cast them as doubles when you divide.

Ah, that was it. Thank you!
 
Sure thing!
 

Similar threads

Replies
6
Views
3K
Replies
23
Views
3K
Replies
13
Views
2K
Replies
4
Views
3K
Replies
2
Views
2K
Replies
17
Views
5K
Back
Top