Thread: Dividing stats in C

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    14

    Dividing stats in C

    I'm trying to create a statistic showing a percentage by dividing two other statistics, but the code I've tried doesn't seem to work. The relevant code is below. The line giving me the problem is player->hits_to_miss=((*player)).number_hits/(player->total_shots)*100:
    Code:
    void update_stats (Stats *player, Boolean hit_status)
    {
    	if (hit_status == TRUE)
    	{
    		((*player).number_hits)++;
    	}
    	else
    	{
    		(player->number_misses)++;
    	}
    	(player->total_shots)++;
    	player->hits_to_miss=((*player)).number_hits/(player->total_shots)*100;
    }
    void print_stats (Stats player)
    {
    	printf ("Hits: %d, Misses: %d, Total Shots: %d, Accuracy: %.1lf percent\n", player.number_hits, player.number_misses, player.total_shots, player.hits_to_miss);
    }
    I've tried various iterations such as
    Code:
    	player->hits_to_miss=(player->number_hits/(player->total_shots)*100
    Code:
    	player->hits_to_miss=((*player).number_hits/((*player).total_shots)*100
    , but basically I'm shooting in the dark. Is there a way to do this?

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    but the code I've tried doesn't seem to work.
    Hmmmm
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Dividing ints will get you integer results. Check your line again with that in mind.

    but basically I'm shooting in the dark
    Pun intended?
    Consider this post signed

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    14
    Quote Originally Posted by bernt View Post
    Dividing ints will get you integer results. Check your line again with that in mind.


    Pun intended?
    /facepalm

    Durr. Sorry, it's been a few months since I wrote code.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    14
    Thanks, bernt! That hint was exactly what I needed. Converted the integer values to double values, and now the program works like a champ!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to build a baseball stats database
    By Programmer3922 in forum C Programming
    Replies: 1
    Last Post: 08-01-2008, 11:01 PM
  2. Whoa... tough one here.
    By nickodonnell in forum Game Programming
    Replies: 10
    Last Post: 10-08-2005, 12:40 PM
  3. Switching CS to Stats?
    By Extrovert in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-20-2004, 02:24 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. Old Board stats
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 08-22-2001, 11:54 AM