Average and loops

Im trying to get the average score to print out from answer of loop. Here is my code as of now.

#include<iostream>
#include<cmath>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main()
{
char average, again;
int a, b, answer, guess ,count=1;

cout<< "Welcome! I hope you remember your multiplication tables:"<<endl;
cout<<endl;

do{
srand ((unsigned)time(NULL) );
a = rand() % 10;
b = rand() % 10;

cout << "\nWhat's " << a << "x" << b << "?: ";
cin >> guess;

answer=a*b;

{if(answer == guess)
cout << "Correct!"<<endl;

else
if(answer != guess)
cout << "wrong!" << endl;
}
count++;

} while(count<6);

average=((answer==guess)/5)*100; // (I want Correct/5)
cout<< average<<"%";

system("pause");
return 0;
}
Last edited on
hi,
why average is declared as char type???
use:
short int average;
you should initialize an variable to accumulate correct answers, "collect" for example:
 
float collect = 0; //must be float 

then initialize it only for correct answers:
1
2
3
4
if(answer == guess) {
cout << "Correct!"<<endl;
++collect;  //+1 for each correct answer
}

now calculate percent of correct answers divided by "count" variable:
1
2
3
4
if(collect) average=(collect/count)*100F;  // if collect is positive value then integral promotion will be done here!
// that's why collect must be float
else average = 0;  //if no correct answers so far than do not divide with 0
cout<< average<<"%";
Last edited on
Ohh! Thank you so much!!!
Topic archived. No new replies allowed.