c++ home work percentage average always is 1%

October 21, 2011 at 05:26:42
Specs: Windows 7
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


void avg (int testScore[], int testNum) // declare average function, testNum = for number of tests
{
double index = 0; //integer for index to do the for loop
double avg;
double total = 0; // zero out

for (index = 0; index < testNum; index++); // loop
{
total += testScore[testNum];

}

avg = total / testScore[testNum];

cout << "Your current average on " << testNum << " tests is " << avg << "% " << endl;

};

int _tmain(int argc, _TCHAR* argv[])

{

const int SZ = 5; //constant integer for the array//
int testScore[SZ]; //array to hold test scores//
int testNum;// integer for number of tests
int index; //counter
int sub = 0;
testNum = 0;

while (sub == 0)
{
cout<< "What is Number of tests you have taken in this course<1 through 5> : "; //number of tests input statement//
cin >> testNum;

if(testNum < 1 || testNum > SZ) // check to make sure test number value is valid
{
cout <<" Invalid Entry! please try again" << endl;
}
else
sub = 1;
}
for(index = 0; index < testNum; index++) // collect test scores using counter
{
cout << "Enter in the score you received on test " << index+1 << " : " ;
cin >> testScore[index];
}

avg (testScore, testNum); // call function

system ("pause.exe"); //pause to enable view of output//
return 0;
}


See More: c++ home work percentage average always is 1%

Report •


#1
October 21, 2011 at 10:41:35
Few things going wrong here, so let's start from the top of avg() and work our way down.

double index = 0; //integer for index to do the for loop
This needs to be int. Otherwise, it won't compile when you fix a later mistake.

for (index = 0; index < testNum; index++); // loop
Get rid of the last semicolon. Having one here at the end of the for tells the compiler, "No, I don't have a loop body. Just do index++ until index < testNum."

total += testScore[testNum];
Change this to index instead of testNum. Otherwise, you're reading invalid data testNum number of times (assuming you performed the previous fix).

avg = total / testScore[testNum];
Unless you want to divide your total by a garbage number (and you don't), I suggest you divide by testNum instead.

How To Ask Questions The Smart Way


Report •
Related Solutions


Ask Question