-
Bad printf()!
This is the code that I'm struggling with:
Code:
typedef struct
{
char surname[20];
char name[15];
int addLevel;
int subLevel;
int correct;
int wrong;
int correctSession;
int wrongSession;
} USER;
//I *know* I have the definitions correct. I've passed a USER struct to the function via pointer, in case that confuses anyone.
void mathAssess(USER *userPtr)
{
float hitRate = ((userPtr->correct)/(userPtr->correct + userPtr->wrong))*100;
float hitRateSession = ((userPtr->correctSession)/(userPtr->correctSession + userPtr->wrongSession))*100;
char pc = 37; //I'm trying to print out "%". This seemed the easiest way.
printf("Current Statistics for user %s %s:\n\n", userPtr->name, userPtr->surname);
printf("Total Correct Answers:%15d\n", userPtr->correct);
printf("Total Incorrect Answers:%13d\n", userPtr->wrong);
printf("Correct Answers this Session:%8d\n", userPtr->correctSession);
printf("Incorrect Answers this Session:%6d\n", userPtr->wrongSession);
printf("Total Hit Rate:%22.2f%c\n", hitRate, pc);
printf("Hit Rate This Session:%15.2f%c\n", hitRateSession, pc);
}
It compiles fine, but when I try to run the function, the program crashes. Can someone tell me what is wrong?
-
You still need to paste the code which calls this - I can't see anything wrong with it
And to print a single %, do this
printf("Hit Rate This Session:%15.2f%%\n", hitRateSession );
-
Actually, There's no bother. Looking at the code while pasting it made me realise what I was doing wrong - user.correct, user.wrong, user.correctSession and user.wrong session all equal 0.
And we all know what happens when you divide by 0...
No wonder I couldn't find anything wrong with the code!