Dividing stats in C

This is a discussion on Dividing stats in C within the C Programming forums, part of the General Programming Boards category; I'm trying to create a statistic showing a percentage by dividing two other statistics, but the code I've tried doesn't ...

  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
    Katy, Texas
    Posts
    2,309
    but the code I've tried doesn't seem to work.
    Hmmmm
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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

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