Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hello All I am working on a C# console application where I have to represent money and display how many bills were used to make each amount. I can do this The only problem I am working on figuring out is recording the number of each bill that has been used and to subtract it for the current number. So this is what I have come up with and it has done everything right except for I did a value of 258 and it gave me a ten with the other correct values. It just gave me a extra 10 but everything else I tried worked.. I have an array of the amount of bills I have to be used.
I am subtracting a bill each time i use one.
my 100s are working but when I go to the next bill the remainder is wrong so i cant move on here is some of the stuff i tried..
C#
public static void countbills(int amount, int[] bills, int[] billsCount)
        {
            int remainder = amount;
            int billIterator = 1;
            for(int i = 0; i < billsCount.Length; i++)   // while(remainder > 0)
            {
                //if(remainder >= bills[billIterator])  //{  if(billsCount[])  }
                if (remainder >= bills[i])
                {
                    if (billsCount[i] != 0)
                    {
                        billsCount[i]--;
                        remainder -= bills[billIterator];//bills[i] * billsCount[billIterator];
                        billIterator++;
                        //= bills[i];//amount / bills[i];     //remainder -= counts[bill] * dollarSizes[bill]; //reminder = amount % cashBills[billrem];
                        //amount -= billsCount[i] * bills[i];
                        //billsCount[i]--;
                    }
                }
            }
        }


What I have tried:

I am getting the users input and Ill get a number and I want to minus the number of bills that were used to make that amount and to keep track.


Keeping track of what has been used in a variable so I can minus the dollar bills used and then I want to restock but thatll be easy after i figure this part out
Posted
Comments
TheBigBearNow 9-Feb-19 15:09pm    
I got everything working how I wanted to. I am just subtracting 2 arrays the initial value and the array of how many used.
The only question i have it what can i do if there is not even in the initial value. so the number is 0 or a negative. How can I solve that issue. That is all I have left. With an if statement ?
This is code i use to do the solving
 public static void Countsolve(int amount, int[] arraycount)
        {
            int[] cash = { 100, 50, 20, 10, 5, 1 };
            int[] bcounter = { 0, 0, 0, 0, 0, 0 };

            for(int i = 0; i < 6; i++)
            {
                if(amount >= cash[i])
                {
                    counter[i] = amount / cash[i];
                    amount -= bcounter[i] * cash[i];
                }
            }
            for(int x=0; x<arraycount.Length; x++)
            {
                arraycount[x] = arraycount[x] - bcounter[x];
            }

"
I need to so something with arraycount IF < 0 then print "..." in console
Richard MacCutchan 10-Feb-19 5:41am    
If you are working in C# then why are you tagging your question with all these other languages? Please use the proper tags only.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900