Monday, May 24, 2010

C++ help please!?

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 gradesRead = 0;


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 / gradesRead;





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

C++ help please!?
#include %26lt;iostream%26gt;





using namespace std;





int main()


{


//declare variables


int section = 1;


int gradesRead = 0;


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;








if( grades %26gt; 0 ){


float iRunningTotal = 0;





//read them in and store them (could use a running total)


for( int i = 0 ; i %26lt; grades ; i++ ){


cout %26lt;%26lt; "Next grade for Section " %26lt;%26lt; section %26lt;%26lt; ": " ;


cin %26gt;%26gt; gradesRead;





iRunningTotal += gradesRead;


}





average = iRunningTotal / (float)grades;


}





cout %26lt;%26lt; endl %26lt;%26lt; "Section " %26lt;%26lt; section %26lt;%26lt; " Average: " %26lt;%26lt; average %26lt;%26lt; endl %26lt;%26lt; endl;





section += 1;


average = 0.0;





} //end while





cout %26lt;%26lt; "End of the program" %26lt;%26lt; endl;


}
Reply:I believe the answer to your problem is that you declare totalGrades to be zero at the beginning of the program, and you never change that value, or have the user change that value. you also declare gradesRead as zero. If you are not a big math person, zero divided by anything, is zero, and anything divided by zero is undefined. also, zero devided by zero is not zero, nor is it undefined: it is undetermined. you must have the user changed both of those values to something other than zero. Also, what is the difference between totalGrades, and grades? if there is a certain amount of tests that the user took, maybe ask for the grades one at a time?





e.g.: add these variables





double grade1;


double grade2;





while (grades %26gt; 0)


{


cout%26lt;%26lt;"What was the score on your first test? \n";


cin %26gt;%26gt; grade1;


cout%26lt;%26lt;"What was the score on your second test? \n";


cin%26gt;%26gt; grade2;





average = (grade1 + grade2) / 2





I'm not sure how much that helps or if that is even what your looking for, but good luck. Sorry, it has been a while since I have programmed in C++.
Reply:you calculate the average with this line:


average = totalGrades / gradesRead;





but then you assign the value of zero to average in this line:


average = 0.0;


Try commenting out this line and see what happens.


No comments:

Post a Comment