C++ Average grade Calculation Error
Jan 31, 2010 at 5:07am UTC
Hi all, i have this codes, but i came across some problem. let say i input 4 modules. and i have like A,A,F,F. by right the ans should be (4+4+0+0)/4 = 2
But program output my ans as 4.
Anyone can help me check where is the error and replace the error with the correct one ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
char name [100];
int n, ta, tb, tc, td, tf;
ta=tb=tc=td=tf=0;
char array[100];
string s;
cout <<"This is an Average Point Calculator. Just put in your 1-5 modules grade" << endl;
cout<<"What is your name?" << endl;
cin.getline (name,100);
cout<<"Hello, " << name << "." << endl;
cout << "How many modules total ? " ;
cin >> n;
for (int i=1; i<=n; i++)
{
do
{
cout << "Enter Module" << i << "'s grade in LETTER : " ;
cin >> s;
if (s.length()>1)
break ;
else array[i] = s[0];
if (isalpha(array[i]))
{
if (tolower(array[i])=='a' ){ ta++; break ; }
if (tolower(array[i])=='b' ){ tb++; break ; }
if (tolower(array[i])=='c' ){ tc++; break ; }
if (tolower(array[i])=='d' ){ td++; break ; }
if (tolower(array[i])=='f' ) break ;
}
} while (1);
}
cout << "Average grade of " << n << " subjects is : " << (ta*4+tb*3+tc*2+td)*1.0/(ta+tb+tc+td+tf) <<endl;
}
Jan 31, 2010 at 5:20am UTC
You're never incrementing tf, but you're including it as your divisor when printing.
Wouldn't it be easier to just divide by n, anyway?
Jan 31, 2010 at 5:35am UTC
thanks helios. got it.
Topic archived. No new replies allowed.