i am not getting the average on this program why? my output is 0. what do i have wrong? this is my code:
#include %26lt;iostream%26gt;
#include %26lt;iomanip%26gt;
using std::cout;
using std::cin;
using std::endl;
using std::fixed;
using std::setprecision;
int main()
{
//declare variables
int section = 1;
int grades = 0;
double totalGrades = 0.0;
double average = 0.0;
while (section %26lt; 3)
{
cout %26lt;%26lt; "Grades for Section " %26lt;%26lt; section %26lt;%26lt; ": " ;
cin %26gt;%26gt; grades;
while (grades %26gt; 0)
{
average = totalGrades / section;
cout %26lt;%26lt; "Next grade for Section " %26lt;%26lt; section %26lt;%26lt; ": " ;
cin %26gt;%26gt; grades;
} //end while
cout %26lt;%26lt; endl %26lt;%26lt; "Section " %26lt;%26lt; section %26lt;%26lt; " Average: " %26lt;%26lt; totalGrades %26lt;%26lt; endl %26lt;%26lt; endl;
section += 1;
average = 0.0;
} //end while
cout %26lt;%26lt; "End of the program" %26lt;%26lt; endl;
} //end of main function
Can someone please help me with C++?
Totalgrades is never being set, but you are setting average by the line:
"average = totalGrades / section;".
so:
average = 0 / section
Therefore average will always be 0.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment