1 Replies - 200 Views - Last Post: 30 April 2019 - 12:31 AM Rate Topic: -----

#1 j_score2000   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 29-April 19

Coin Change Display in C

Posted 29 April 2019 - 10:57 PM

Hi, I am currently trying to work out how to write a code that will accept an integer value in the range of 5-95 and as a multiple of 5 representing the number of cents to give to a customer in their change. The program should calculate how many coins of each denomination and display this to the user. I'm struggling in particular with the whole concept of pass by reference which is especially important in this task, as well as modularisation.

I also feel using the calculation I have done might be slightly too complicated so if there are any other suggestions out there I would really appreciate it.

Currently, the error messages I'm receiving are 'Declaration Terminated Incorrectly' for lines 28, 74 and 99.

I have tried changing the ampersand and using pass by reference and just having nothing at all. I'm still new to C and everything about it so it's quite confusing.

#include <stdio.h>
#include <ctype.h>

/***********************************************************
	Get the Users Data
***********************************************************/

	int GetData(void)
{
		int num;
		
		printf("Please enter your change: ");
		scanf("%d%*c", &num);
		
	return(num);
}


/************************************************************
	Get the Calculation for Finding Change
*************************************************************/

	int GetChange(int &change, int &Fifty, int &Twenty, int &Ten, int &Five);
{
		Change = GetData()
		int Fifty, Twenty, Ten, Five;
	
	{
		Fifty = Change/50;
		Change %= 50;
	}
		
	{
		Twenty = Change/20;
		Change %= 20;
	}
	
	{
		Ten = Change/10;
		Change %= 10;
	}
		
	{
		Five = Change/5;
		Change %= 5;
	}
	
	return;
}

/***********************************************************************
	Finds if Value is a Multiple of 5, Greater then 5 and Less then 95.
***********************************************************************/
	int ErrorMessage(int num)
	{
		int amount;
		
		amount = GetData(num);

		if((amount < 5)     ||     (amount > 95)     ||     (amount%5 = 0))
			printf("Change must be between 5 - 95 and be a factor of 5, Try Again.");
		
		return(&amount);
	}

/*****************************************************************
	Calculates the coins required and outputs result.
*****************************************************************/
	int CalculateChange(int &Fifty, int &Twenty, int &Ten, int &Five);
{
		int Fifty, Twenty, Ten, Five;
		
		Fifty = GetChange(int &Fifty);
		Twenty = GetChange(int &Twenty);
		Ten = GetChange(int &Ten);
		Five = GetChange(int &Five);
/*****************************************************************************
	Calculates from highest to lowest where the change can be divided up
******************************************************************************/
			if((Fifty > 1)     ||     (Fifty == 1))
				printf("50c Required = %d.", &Fifty);
				if((Twenty > 1)    ||     (Twenty == 1))
					printf("20c Required = %d.", &Twenty);
					if((Ten > 1)     ||    (Ten == 1))
						printf("10c Required = %d.", &Ten);
						if((Five > 1)     ||     (Five == 1))
							printf("5c Required = %d.", &Five);
	return;
}
/************************************************************************************
	Takes the amount of change entered and the amount of coins needed and outputs
*************************************************************************************/
	int main(int amount, int Fifty, int Twenty, int Ten, int Five);
{9
	do
	{
		GetData(num);
		
		GetChange(int &Change, int &Fifty, int &Twenty, int &Ten, int &Five);
		
		CalculateChange(&amount, &Fifty, &Twenty, &Ten, &Five);
		
		ErrorMessage(num)
		
	}while(FinalAmount = 0);
	
	return(0);
}


Is This A Good Question/Topic? 0
  • +

Replies To: Coin Change Display in C

#2 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2344
  • View blog
  • Posts: 4,460
  • Joined: 30-May 10

Re: Coin Change Display in C

Posted 30 April 2019 - 12:31 AM

It looks like you wrote all 100+ lines at once, and then wondered why it doesn't compile.

Then posted it here for us to fix.

> int GetChange(int &change, int &Fifty, int &Twenty, int &Ten, int &Five);
C doesn't have references, and you don't have a ; at the end for a function definition.

> 073 Fifty = GetChange(int &Fifty);
You don't mention types when calling a function.

> 093 int main(int amount, int Fifty, int Twenty, int Ten, int Five);
Read your book again as to what parameters main expects.

TBH, you should delete the whole code and start again, so you can practice a better approach to solving the problem.
Which is to write the code one step at a time.

Small steps, compile and test frequently is the way you write programs.

Eg
Version 1 looks like this.
#include <stdio.h>
int getAmount() {
    int amount;
    scanf("%d",&amount);
    return amount
}

int main() {
    int amount;
    amout = getAmount();
    printf("The amount entered=%d\n",amount);
}


Make sure it compiles.
If it doesn't, fix the errors.
When it compiles, run it to make sure it does what you expect.
If it doesn't work, FIX it.


Version 2 looks like this.
#include <stdio.h>
int getAmount() {
    int amount;
    scanf("%d",&amount);
    return amount;
}

void getChange(int amount,int *fifties) {
    // not the right answer, but it proves
    // you can compile and set a value
    *fifties = amount;
}

int main() {
    int amount;
    int fifties;
    amout = getAmount();
    printf("The amount entered=%d\n",amount);
    getChange(amount,&fifties);
    printf("The number of fifties in %d is %d\n", amount, fifties);
}




The next versions of getChange would
- make a proper calculation for fifties.
- add parameters for twenties, tens, fives.

Then add whatever else you like, but make sure you plan your incremental steps.

Never write more code than you can cope with the compiler just throwing it back at you, or the functionality isn't what you expect.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1