10 Replies - 559 Views - Last Post: 03 April 2010 - 11:33 PM Rate Topic: -----

#1 laura1256  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 45
  • Joined: 01-April 10

error in code?

Posted 03 April 2010 - 09:32 PM

the aim of the code is for a user to enter a change value in which the program tells them how much of each coin or note to give. the first setion (int ConvertDollar) seems to work as the corect amount of note are show
however the second part(ConvertCents)will give strange amounts. the second part needs to take the number after the decimal point and convert it to an integer the line cents = ((int......+0.5)); was given to the class by the lecture. i think this is the bit that is causeing the issue.

int ConvertDollar (int dollar, float change)
{	
	dollar = ((int) change);
	scanf("%d", & dollar);
	return (dollar);;
}	
int ConvertCents(int cents, int dollar, float change)
{
	cents = ((int) (((change - dollar)*100) + 0.5));
	scanf("%d", & cents);
	return (cents);
}




thanks for your help

Is This A Good Question/Topic? 0
  • +

Replies To: error in code?

#2 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: error in code?

Posted 03 April 2010 - 09:42 PM

I would chage that line to

cents = ((int) (((change - (float)dollar)*100.0) + 0.5));



This forces the entire computation to float for consistency before converting to an int result.
Was This Post Helpful? 1
  • +
  • -

#3 tauit_dnmd  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 129
  • Joined: 25-March 10

Re: error in code?

Posted 03 April 2010 - 09:55 PM

View Postlaura1256, on 03 April 2010 - 08:32 PM, said:

the aim of the code is for a user to enter a change value in which the program tells them how much of each coin or note to give. the first setion (int ConvertDollar) seems to work as the corect amount of note are show
however the second part(ConvertCents)will give strange amounts. the second part needs to take the number after the decimal point and convert it to an integer the line cents = ((int......+0.5)); was given to the class by the lecture. i think this is the bit that is causeing the issue.

int ConvertDollar (int dollar, float change)
{	
	dollar = ((int) change);
	scanf("%d", & dollar);
	return (dollar);;
}	
int ConvertCents(int cents, int dollar, float change)
{
	cents = ((int) (((change - dollar)*100) + 0.5));
	scanf("%d", & cents);
	return (cents);
}




thanks for your help

laura:
scanf("%d", & dollar);
and
scanf("%d", & cents);
??????(wrong)
---->i think you must delete it.
Was This Post Helpful? -1
  • +
  • -

#4 laura1256  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 45
  • Joined: 01-April 10

Re: error in code?

Posted 03 April 2010 - 10:03 PM

when i deleat them it just prints all the line of code and does not allow input from the user
Was This Post Helpful? -1
  • +
  • -

#5 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: error in code?

Posted 03 April 2010 - 10:12 PM

I can see what you are trying to do with this code, but does the user input the dollars and the parameter you are passing to the functions that you have called change is the exchange rate?
Was This Post Helpful? 0
  • +
  • -

#6 laura1256  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 45
  • Joined: 01-April 10

Re: error in code?

Posted 03 April 2010 - 10:15 PM

i have also found that if the user enters eg 50 instead of 50.00 the program just freazes is their a way for the prog to accept the 50 value??

Martyn.Rae : na this is that the user is dumb and needs to be told how many of each coin to give to a customer. it has nothing to do with exchange rate.

hear is the entire program
#include<stdio.h>
#include<ctype.h>
	
float GetChange (float change)
{
	printf("please enter change: ");
	return (change);
}
	
int ConvertDollar (int dollar, float change)
{	
	dollar = ((int) change);
	scanf("%d", & dollar);
	return (dollar);;
}	
int ConvertCents(int cents, int dollar, float change)
{
	cents = ((int) (((change -(float)dollar)*100) + 0.5));
	scanf("%d", & cents);
	return (cents);
}


/*Set Values for dollars*/
int hundredDollar(int valueD)
{	
	valueD = 100;
	return (valueD);
}

int fiftyDollar(int valueD)
{
	valueD = 50;
	return (valueD);
}

int twentyDollar(int valueD)
{
	valueD = 20;
	return (valueD);
}
int tenDollar(int valueD)
{
	valueD = 10;
	return (valueD);
}

int fiveDollar(int valueD)
{	
	valueD = 5;
return (valueD);
}

int twoDollar(int valueD)
{	
	valueD = 2;
	return (valueD);
}

int oneDollar(int valueD)
{	
	valueD = 1;
	return (valueD);
}

/*set values for cents*/
int fiftyCent (int valueC)
{
	valueC = 50;
	return (valueC);
}

int twentyCent (int valueC)
{
	valueC = 20;
	return (valueC);
}

int tenCent(int valueC)
{
	valueC = 10;
	return (valueC);
}
int fiveCent(int valueC)
{	
	valueC = 5;
	return (valueC);	
}

/*deturmin dollar amount*/
int calDollars (int amountD, int dollar, int valueD)
{		
	amountD = (dollar/valueD);
	
	dollar = (dollar -(amountD*valueD));
	printf("please give %d, %d dollars\n",amountD, valueD);
	return (dollar);
}
/*deturmine cents amount*/
int calCents(int amountC, int valueC, int cents)
{
	amountC = (cents/valueC);
	
	cents = (cents -(amountC*valueC));
	printf("please give %d, %d cents\n",amountC, valueC);
	return (cents);
}
int main()
{
	int amountC,amountD, dollar, cents, valueD, valueC;
	float change;
	
	change = GetChange(change);
	dollar = ConvertDollar(dollar, change);
	cents = ConvertCents(cents, change, dollar);
	
	do
	{
		valueD = hundredDollar(valueD);
		dollar = calDollars (amountD, dollar, valueD);
	}while (dollar >100);
	
	do
	{	
		valueD = fiftyDollar (valueD);
		dollar = calDollars(amountD, dollar, valueD);
	} while(dollar>50);
	
	do
	{
		valueD = twentyDollar(valueD);
		dollar = calDollars(amountD, dollar, valueD);
	}while (dollar>20);
	
	do
	{
		valueD = tenDollar(valueD);
		dollar = calDollars (amountD, dollar, valueD);
	}while (dollar>10);
	
	do
	{
		valueD = fiveDollar(valueD);
		dollar =  calDollars(amountD, dollar, valueD);
	}while(dollar>5);
	
	do
	{
		valueD = twoDollar(valueD);
		dollar = calDollars(amountD, dollar, valueD);
	}while (dollar>2);
	
	
	valueD = oneDollar(valueD);
	calDollars(amountD, dollar, valueD);
	
	do
	{
		valueC = fiftyCent(valueC);
		cents = calCents (amountC, valueC, cents);
	}while(cents>50);
	
	do
	{
		valueC = twentyCent(valueC);
		cents =  calCents (amountC, valueC, cents);
	}while (cents>20);
	
	do
	{
		valueC = tenCent(valueC);
		cents = calCents(amountC, valueC, cents);
	}while(cents>10);
	
	valueC = fiveCent(valueC);
	calCents(amountC, valueC, cents);
	
	return (0);
}



Was This Post Helpful? 0
  • +
  • -

#7 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: error in code?

Posted 03 April 2010 - 10:29 PM

ok, you have a number of issues with this code I am sorry to say. Let's take a look at the GetChange routine. Unless I am mistaken, the user has to enter the amount of change that needs to be given.

float GetChange (float change)
{
        printf("please enter change: ");
        return (change);
}



This code says that the caller of this routine is passing the change into the routine as a float value. You then ask the user to enter the change and then do not give them the chance to input a value. You then simply return what was provided by the caller of the routine. If you use the code below, it makes more sense.

float GetChange ()
{
    float change;
    printf("please enter change: ");
    scanf("%f", &change);
    return (change);
}



If you are happy with that, you need to look at the other routines ConvertDollar and ConvertCents in the same way.
Was This Post Helpful? 1
  • +
  • -

#8 laura1256  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 45
  • Joined: 01-April 10

Re: error in code?

Posted 03 April 2010 - 10:41 PM

thanks for you help it has stopped telling the user to give the change in both cent and dollars eg give one $50note and 200 50cent coins for $50 worth of change. but it still wont tell the user what change to give eg if $50.55 is entered
Was This Post Helpful? 0
  • +
  • -

#9 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: error in code?

Posted 03 April 2010 - 10:46 PM

That's because we have not finished yet. I just wanted to make sure you understood where I was going with this and what the problems were with your code. Let's look at the ConvertDollar and ConvertCents routines. I think you need to change those to

int ConvertDollar (float change)
{       
    int dollar = ((int) change);
    return (dollar);;
}       
int ConvertCents(float change, int dollar)
{
    int cents = ((int) (((change - dollar)*100) + 0.5));
    return (cents);
}



That with return the whole number of dollars and the whole number of cents for the rest of your program. I have checked that these routines work, and they do. There are still problems with your code when you attempt to split the dollas up into hundreds, tens etc.
Was This Post Helpful? 1
  • +
  • -

#10 laura1256  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 45
  • Joined: 01-April 10

Re: error in code?

Posted 03 April 2010 - 10:55 PM

thanks i will play around with the code a little more to see if i can getting working so much

thanks so much for you help :D
Was This Post Helpful? 0
  • +
  • -

#11 laura1256  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 45
  • Joined: 01-April 10

Re: error in code?

Posted 03 April 2010 - 11:33 PM

i found the problem,

in the line
cents = ConvertCents(dollar, change);



from the main fuction the dollar and change were in a different order to the actual fuction

lol

it finally works
thanks to all for your help
much appricated

:D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1