Bad printf()!

This is a discussion on Bad printf()! within the C Programming forums, part of the General Programming Boards category; This is the code that I'm struggling with: Code: typedef struct { char surname[20]; char name[15]; int addLevel; int subLevel; ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    19

    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?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    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 );

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    19
    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems reading entered race times C
    By loopymoo26 in forum C Programming
    Replies: 12
    Last Post: 05-23-2009, 07:38 AM
  2. Help with a C Programing Quiz project
    By dilemma in forum C Programming
    Replies: 12
    Last Post: 05-15-2009, 03:35 PM
  3. saying hello and 1st question
    By darksys in forum C Programming
    Replies: 12
    Last Post: 10-31-2008, 02:58 PM
  4. I need help on this particular Linked List problem
    By sangken in forum C Programming
    Replies: 11
    Last Post: 08-06-2006, 12:26 AM
  5. Double to Int conversion warning
    By wiznant in forum C Programming
    Replies: 15
    Last Post: 09-19-2005, 09:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21