Hello all,
yet again im trying to work out some c++, but this time I am doing pass by referencing. I am trying to allow each of my functions to pass down the edited reqchange as they each make modifications.
The program compies, and the 50c function works, and so does the 20c function, but unfortunately the 10c function reverts back to the value stored by the 50c function and thenuses that instead of the edited 20c function. Hence this also will not pass onto the 5c function.
If any1 could point me in the right direction or have any weblinks on pass by referencing that htey think could help would be great.
Code:
/*****************************************************************
Author: Michael R
Date: 8th April 2005
Purpose: Change in Coins
*****************************************************************/
#include <stdio.h>
void userinput (int &reqchange)
{
printf("Welcome to the change calculator Program\n");
printf("Enter how much change is needed to be distributed to the client...");
scanf("%d", &reqchange);
printf("\n");
return;
}
void fiftycents (int &reqchange)
{
if (reqchange >= 50)
{
reqchange = (reqchange - 50);
printf("\n");
printf("Required number of 50cent pieces = 1");
}
else
{
printf("\n");
printf("Required number of 50cent pieces = 0");
}
return;
}
void twentycents (int &reqchange, int twenty_cents, int reqtwenty_cents)
{
if (reqchange >= 20)
{
reqtwenty_cents = reqchange;
reqchange = ((reqtwenty_cents % 20) * 20);
twenty_cents = (reqtwenty_cents / 20);
printf("\n");
printf("Required number of 20cent pieces = %d", twenty_cents);
}
else
{
printf("\n");
printf("Required number of 20cent pieces = 0");
}
return;
}
void tencents (int &reqchange, int ten_cents, int reqten_cents)
{
if (reqchange >= 10)
{
reqten_cents = reqchange;
reqchange = ((reqten_cents % 10) * 10);
ten_cents = (reqten_cents / 10);
printf("\n");
printf("Required number of 10cent pieces = %d", ten_cents);
}
else
{
printf("\n");
printf("Required number of 10cent pieces = 0");
}
return;
}
void fivecents (int &reqchange)
{
if (reqchange == 5)
{
printf("\n");
printf("Required number of 5cent pieces = 1 ");
}
else
{
printf("\n");
printf("Required number of 5cent pieces = 0");
}
return;
}
int main ()
{
int reqchange, twenty_cents, reqtwenty_cents, ten_cents, reqten_cents;
userinput(reqchange);
fiftycents(reqchange);
twentycents(reqchange, twenty_cents, reqtwenty_cents);
tencents(reqchange, ten_cents, reqten_cents);
fivecents(reqchange);
return(0);
}
Anyway any help/ reference/direction steering would be great.
Cheers,
Spitfire