Thread: Better Coding Way?

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    8

    Better Coding Way?

    Hi, in a book that is teaching me C, I have an exercise to where I need to code a change counter as long as the monitary value is under $1.00. If you look at the code, you can see what is going on. There is nothing wrong with the code, but I was wondering if there is a better way to do this. I can see already, the way I am going with this is going to become quite tedious. Do you have any suggestions on ways to make it more terse or efficient?

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
    	int total;    //users input total
    	int quarters; //teh amount of quarters
    	int dimes;    //the amount of dimes
    	int nickels;   //the amount of nickels
    	int pennies;  //the amount of pennies
    	char line[100];    //line fo' input data
    	
    	printf("Enter a monitary value less than $1.00: ");
    	fgets(line, sizeof(line), stdin);
    	sscanf(line, "%d", &total);
    
    	while (total > 99)
    	{
    		printf("Please enter a monitary value less than 1 dollar: ");
    		fgets(line, sizeof(line), stdin);
    		sscanf(line, "%d", &total);
    	}
    
    	if (total >= 75)
    	{
    		total = total - 75;
    
    		if (total < 5)
    		{
    			printf("The change is 3 quarters and %d pennies.\n", total);
    		}
    	
    		else if (total >= 5)
    		{	
    			
    		}
    	}
    
    		
    
    	return (0);
    }
    Any help is appreciated.

  2. #2
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Yes, there is a better way.

    Quarters have a value of 25.
    Dimes have a value of 10.
    Nickles have a value of 5.
    Pennies have a value of 1.


    Use the "/" operator on integers to figure out how many of the largest coin left.
    Use the "%" operator to find out how much is left over after using the largest coin.

    Do this for each coin size from quarter to nickle, and you'll have the number of each coin, what's left is pennies.
    Insert obnoxious but pithy remark here

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    8
    ahhh, thanks alot. That helped tremendously.

    EDIT: cool, thanks alot. This is what I end up with.

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
    	int total;    //users input total
    	int quarters; //teh amount of quarters
    	int dimes;    //the amount of dimes
    	int nickels;   //the amount of nickels
    	int pennies;  //the amount of pennies
    	char line[100];    //line fo' input data
    	
    	printf("Enter a monitary value less than $1.00: ");
    	fgets(line, sizeof(line), stdin);
    	sscanf(line, "%d", &total);
    
    	while (total > 99)
    	{
    		printf("Please enter a monitary value less than 1 dollar: ");
    		fgets(line, sizeof(line), stdin);
    		sscanf(line, "%d", &total);
    	}
    
    	quarters = total / 25;
    
    	total = total - (quarters * 25);
    	
    	dimes = total / 10;
    
    	total = total - (dimes * 10);
    
    	nickels = total / 5;
    
    	total = total - (nickels * 5);
    	
    	pennies = total;
    
    	printf("You need %d quarter(s), %d dime(s), %d nickel(s), and %d pennie(s).\n", quarters, dimes, nickels, pennies);
    
    	return (0);
    }
    Last edited by radiohead; 01-19-2006 at 05:18 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-20-2009, 05:22 PM
  2. Coding Guideline ....!!
    By imfeelingfortun in forum Tech Board
    Replies: 8
    Last Post: 10-08-2006, 07:09 AM
  3. Before Coding
    By cyberCLoWn in forum C++ Programming
    Replies: 16
    Last Post: 12-15-2003, 02:26 AM
  4. Coding Contest....
    By Koshare in forum A Brief History of Cprogramming.com
    Replies: 46
    Last Post: 10-14-2001, 04:32 PM