coin machine

I am being asked to make this program more modular by using fuctions.

Page 1 of 1

2 Replies - 2442 Views - Last Post: 02 March 2010 - 10:57 PM Rate Topic: -----

#1 Guest_Ctemp912*


Reputation:

coin machine

Posted 02 March 2010 - 08:59 PM

I was asked to make this more modular by using functions for easily modified revisions, I dont know where I can make this more modular its pretty simple as is. Program works but it backs out early..Can someone help? Thanks.. It must keep the variables also.

#include <stdio.h>
#include <string.h>
#include <math.h>


int main()

{

double dollar_value(1.00);
double quarter_value(0.25);
double dime_value(0.10);
double nickel_value(0.05);
double pennies_value(0.01);

int num_dollars;
int num_quarters;
int num_dimes;
int num_nickels;
int num_pennies;
int total_num_coins;

double total_dollar_val;
double total_quarter_val;
double total_dime_val;
double total_nickel_val;
double total_pennies_val;
double total_val_coins;
char full_name[32];


printf("Please Enter Your Name:");
scanf( "%d\n", &full_name);

printf("Enter the Number of Dollars:");
scanf( "%d\n", &num_dollars);

printf( "Enter the Number of Quarters:");
scanf( "%d\n", &num_quarters);

printf( "Enter the Number of Dimes:");
scanf( "%d\n", &num_dimes);

printf( "Enter the Number of Nickels:");
scanf( "%d\n", &num_nickels);

printf("Enter the Number of Pennies:");
scanf( "%d\n", &num_pennies);




total_num_coins= num_dollars+num_quarters+num_dimes+num_nickels+num_pennies;

total_val_coins = (dollar_value*num_dollars) + (quarter_value*num_quarters) + (dime_value*num_dimes) + (nickel_value*num_nickels) + (pennies_value*num_pennies);

printf("Name:", full_name);


printf("Total number of coins input:", total_num_coins);
printf("Number of Dollars:", num_dollars);
printf("Number of Quarters:", num_quarters);
printf("Number of Dimes:", num_dimes);
printf("Number of Nickels:", num_nickels);
printf("Number of Pennies:", num_pennies);

printf("Value of Coins:");
printf("Dollars:", dollar_value*num_dollars);
printf("Quarters:", quarter_value*num_quarters);
printf("Dimes:", dime_value*num_dimes);
printf("Nickels:", nickel_value*num_nickels);
printf("Pennies:", pennies_value*num_pennies);
printf("Total Value of coins:",total_val_coins);


return 0;
}


This post has been edited by no2pencil: 02 March 2010 - 09:01 PM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0

Replies To: coin machine

#2 Ancient Dragon   User is offline

  • D.I.C Addict
  • member icon

Reputation: 82
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: coin machine

Posted 02 March 2010 - 09:29 PM

>>but it backs out early

What exactly does that mean?
Was This Post Helpful? 0
  • +
  • -

#3 taylorc8   User is offline

  • B&

Reputation: 150
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: coin machine

Posted 02 March 2010 - 10:57 PM

This looks like it could be a function:
printf("Total number of coins input:", total_num_coins);
printf("Number of Dollars:", num_dollars);
printf("Number of Quarters:", num_quarters);
printf("Number of Dimes:", num_dimes);
printf("Number of Nickels:", num_nickels);
printf("Number of Pennies:", num_pennies);

printf("Value of Coins:");
printf("Dollars:", dollar_value*num_dollars);
printf("Quarters:", quarter_value*num_quarters);
printf("Dimes:", dime_value*num_dimes);
printf("Nickels:", nickel_value*num_nickels);
printf("Pennies:", pennies_value*num_pennies);
printf("Total Value of coins:",total_val_coins);



and all those variable declarations can be packed neatly into a structure, then passed as a single argument to a function... but, it looks like a lot of editing to me, (not very fun).

struct CurrencyValues
{
double dollar_value(1.00);
double quarter_value(0.25);
double dime_value(0.10);
double nickel_value(0.05);
double pennies_value(0.01)
};



..Do your variable declarations even compile?!?! There could be some kind of bug, what forum am I in??
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1