I am not getting the average on this program why? my output is 0. what do i have wrong? please be specific with an answer - i am just learning C++ thanx! 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++?
I am not sure I understand your question.
Reply:Travers N for the win. I agree.
Reply:You're not adding 'grades' to 'totalGrades'. Also, your average calculation is wrong -- it should be:
average = totalGrades / gradesRead;
Why would you divide the sum of grades by the section number??
Your inner loop should look like:
totalGrades = 0;
gradesRead = 0;
while (grades %26gt; 0) {
cout %26lt;%26lt; "Next grade for Section " %26lt;%26lt; section %26lt;%26lt; ":";
cin %26gt;%26gt; grades;
totalGrades += grades;
gradesRead++;
}
average = totalGrades / gradesRead;
Reply:Who knows what that is suppose to be doing.
But you only assign totalGrades at the first to 0.0 .
Then you try to do this
average = totalGrades / section
which doesn't make sense because you are 0/section? that doesn't help.
marigold
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment