I am having problems getting this program to work. It will not compile due to errors for dollarAmount and Breakdown. Could someone please help

namespace test
{
   class Converter
    {
        //classs variable
       private int currency;
      

        
        public void converter(int currency, int[] breakdown, int[] denom)
        {

            int diff = currency;
            int x = 0;

            while (diff > 0)
            {
                breakdown[x] = diff / (denom[x]);
                diff = diff % denom[x];
                x++;
            }

        } 

    }  

    class Conversion
    {
        static void Main(string[] args)
        {

            int[] breakdown = new int[4];
            int[] denom = { 20, 10, 5, 1 };
            

            Converter converter = new Converter();
            converter.converter(dollarAmount, breakdown, denom);
            
                       
            Console.Write("Please enter a Dollar amount: ");
            converter.dollarAmount = Convert.ToInt32(Console.ReadLine());

            
            

            

            
            Console.WriteLine("The Dollar Amount Entered Was: {0:c}", converter.dollarAmount);

            Console.WriteLine("Twentys: {0}", converter.breakdown[0]);
            Console.WriteLine("Tens: {0}", converter.breakdown[1]);
            Console.WriteLine("Fives: {0}", converter.breakdown[2]);
            Console.WriteLine("Ones: {0}", converter.breakdown[3]);

           // Internal Documentation.

        } //end main

    }  //end class 
    
} //end namespace

Recommended Answers

All 9 Replies

Line 37 above: Where is dollarAmount coming from?

Also your Converter class has no property or field for dollarAmount, so Line 41 will cause an error as well since you can't set a property that doesn't exist. Same thing when trying to use convert.breakdown, it doesn't exist in the Converter class

first thing I noticed was

private int currency;
        public void converter(int currency, int[] breakdown, int[] denom)

It's OK, but it is a bad practice to use instance field names as local variables.
If your last denom[] isn't 1, it is possible to create an infinite loop, except that your code would blow up as soon as x overindexed the array.
Considering you never define or use the instance name, drop it from the code or comment on what you plan on doing with it in the future.

converter.converter(dollarAmount, breakdown, denom);

How could this compile when you ask it it convert dollarAmount when you've never declared what that variable is or defined it?
Using the same name for an object and a routine is OK, but tacky.

converter.dollarAmount = Convert.ToInt32(Console.ReadLine());

Converter has never declared a property or field called dollarAmount.
Read in invalid data and your code will blow up if you ever get it to run. Suggest you change this to an int field, declared and defined prior to trying to convert it.

Convert.ToInt32 is a terrible user interfacing method. The user enters non-numeric data, this will blow up. You don't have try/catch blocks set up for this. Check out the function "int.TryParse". Don't need try/catch blocks, it trys returns true if successful or catches and returns false. An out parameter defines the int value.

Consider making Converter a static class. (Assuming you remove the int I was complaining about.) You don't need an instance because you aren't storing data related to the class in it.

Okay I am just starting here so I know my code is sloppy and prob does not adherer to better programming practices, but if someone could provide the correction to make this work I would appreciate it. I need to take an amount that is entered and break it down into denomination of bills using a converter class. I am able to get tit to work with a series of for loops, but i need to do it this way as well and just cannot figure it out.

I have made some changes that were suggested but still getting errors of

Error 1 'test.Converter.currency': cannot declare instance members in
a static class
Error 2 'converter': cannot declare instance members in a static class

Again I am just learning C# and its been 6 or 7 years since I did anything with C++ and that was just in school. So if youcan help me fix this i would appreciate it.

namespace Dollar_Breakdown
{
   static class Converter
    {
        //classs variable
       private int currency;


        public void converter(int currency, int[] breakdown, int[] denom)
        {

            int diff = currency;
            int x = 0;

            while (diff > 0)
            {
                breakdown[x] = diff / (denom[x]);
                diff = diff % denom[x];
                x++;
            }

        } 

    }  

    class Conversion
    {
        static void Main(string[] args)
        {

            int[] breakdown = new int[4];
            int[] denom = { 20, 10, 5, 1 };
            int dollarAmount;

            Console.Write("Please enter a Dollar amount: ");
            dollarAmount = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("The Dollar Amount Entered Was: {0:c}", dollarAmount);
            Console.WriteLine("Twentys: {0}", breakdown[0]);
            Console.WriteLine("Tens: {0}", breakdown[1]);
            Console.WriteLine("Fives: {0}", breakdown[2]);
            Console.WriteLine("Ones: {0}", breakdown[3]);

         } //end main

    }  //end class 

} //end namespace

Well I made another change and now it will compile but the conversion does not happen, any amount I enter shows 0's for twentys,Tens,Fives, and ones. I am just so confused right now.

namespace Dollar_Breakdown
{
    class Converter
    {
        //class variable
       private int currency;



        public void converter(int currency, int[] breakdown, int[] denom)
        {

            int diff = currency;
            int x = 0;

            while (diff > 0)
            {
                breakdown[x] = diff / (denom[x]);
                diff = diff % denom[x];
                x++;
            }

        } 

    }  

    class Conversion
    {
        static void Main(string[] args)
        {

            int[] breakdown = new int[4];
            int[] denom = { 20, 10, 5, 1 };
            int dollarAmount;

            Console.Write("Please enter a Dollar amount: ");
            dollarAmount = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("The Dollar Amount Entered Was: {0:c}", dollarAmount);
            Console.WriteLine("Twentys: {0}", breakdown[0]);
            Console.WriteLine("Tens: {0}", breakdown[1]);
            Console.WriteLine("Fives: {0}", breakdown[2]);
            Console.WriteLine("Ones: {0}", breakdown[3]);
            Console.WriteLine("Press any key");
            Console.ReadKey();

           // Internal Documentation.

        } //end main

    }  //end class 

} //end namespace
int[] breakdown = new int[4];
//hint hint. You just defined an array called breakdown that has 4 values that are all 0
int dollarAmount;

Console.Write("Please enter a Dollar amount: ");
dollarAmount = Convert.ToInt32(Console.ReadLine());
// Really, look into TryParse. What if your user says "five dollars"?
//Don't underestimate the stupidity of users

Console.WriteLine("The Dollar Amount Entered Was: {0:c}", dollarAmount);
//well you should be getting the output you expect from the above line. Correct?
Console.WriteLine("Twentys: {0}", breakdown[0]);
// between hint and now, what have you done to change the array's values ? 
//Hmmm, wasn't there something you were supposed to do? Like with the Converter class?

It's your job to think about the process, what you are trying to do, and how you think it should work. It might help to write out comments. Every good coder does.
Write out what you want to accomplish in a comment.
Just before you do something, write out what you are trying to accomplish in a comment.
Do all the comments completely describe everything you want to do?
You might want to put a comment before every line explaining what it is doing and why it is being done. I don't think you wrote the converter routine, it has problems but is still showing more thought about process than you are used to doing. It's OK to be handed code, but if your teacher handed you the code, you need to know what it is doing and why it is doing it.

I have made some changes that were suggested but still getting errors of

Error 1 'test.Converter.currency': cannot declare instance members in
a static class
Error 2 'converter': cannot declare instance members in a static class

When I suggested using a static class I also said to get rid of the int you weren't using.

private int currency;

is an instance member and the int I suggested you get rid of. (error 1) I forgot to mention that if the class is static everything in it has to be static as well. Things like routines. (error 2)

Thanks for the help, I added

Converter converter = new Converter();
converter.converter(dollarAmount, breakdown, denom);

To populate the array

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.