Hello,

I have been struggling with this and it is due today. I have searched and read and am unable to determine what I am doing wrong. Thank you in advance.

class Accounts
    {
        // private class members
        const int arrayLength = 5;
        private int [] accountNumbArray = new int[arrayLength]; //create arrays
        private double [] accountBalArray = new double[arrayLength];
        private string [] accountNameArray = new string[arrayLength];
     
        // fill all three parallel arrays with input
        public static void fillAccounts(int[] accountNumbArray, double[] accountBalArray, string[] accountNameArray)
        {
            int arrayLength = 0;
            for (int i = 0; i < accountNumbArray.Length; i++)
            {
                Console.Write("Enter integer account number ");
                accountNumbArray[arrayLength] = Convert.ToInt32(Console.ReadLine());
                Console.Write("Enter account balance ");
                accountBalArray[arrayLength] = Convert.ToDouble(Console.ReadLine());
                Console.Write("Enter account holder last name ");
                accountNameArray[arrayLength] = Convert.ToString(Console.ReadLine());
            } //end for

            //search the account num array and display

            // when found, account num entered and the corresponding bal and name
            //if Not found, display "You entered an invalid account"
        } //end fillAccounts method

        public static void searchAccounts(int[] accountNumbArray, double[] accountBalArray, string[] accountNameArray)
        {
            int accountNum = 0;
            bool found = false;
            int x = 0;
                
            Console.Write("Please enter the account number you wish to search for ");
            accountNum = Convert.ToInt32(Console.ReadLine());

            while (x < accountNumbArray.Length && accountNum != accountNumbArray[x])
            {
                ++x;
                double balance = accountBalArray[x];
                string name = accountNameArray[x];
                found = true;

            Console.WriteLine("Account number " + accountNumbArray[x] + " has a balance of " + accountBalArray[x]
                    + " and the account holder is " + accountNameArray[x]);
                
            }            
        }

        public static void averageAccounts(int[] accountNumbArray, double[] accountBalArray)
        {
            // compute and display average of all 5 bal as currency use length.
            int balanceCount = 0;
            double balance = accountBalArray[balanceCount];
            double balanceSum = 0;
            double averageBalance = 0;
            
            for (int i = 0; i < accountNumbArray.Length; i++)
            {
                balanceSum = balanceSum + balanceCount;
            }
            averageBalance = (balanceSum/balanceCount);
        } //end averageAccounts method
    } //end public class accounts


    class account_assignment  //wrapper class for main
    {
        static void Main(int[] accountNumbArray, double[] accountBalArray, string[] accountNameArray)
        {
            char entry;
            char a, A;
            char b, B;
            char x, X;
            
            //instantiate one new Accounts object
            Accounts accounts = new Accounts();

            //call class methods to fill accounts Array
            accounts.fillAccounts(accountNumbArray, accountBalArray, accountNameArray);
            //menu detailing entnries to select search (a or A) average (b or B) exit (x or X)
            
           
            Console.WriteLine("*****************************************");
            Console.WriteLine("enter an a or A to search account numbers");
            Console.WriteLine("enter a b or B to average the accounts");
            Console.WriteLine("enter an x or X to exit program");
            Console.WriteLine("*****************************************");

            entry = Convert.ToChar(Console.ReadLine());

            if (entry == 'a' || entry == 'A')
                accounts.searchAccounts(accountNumb);
            if (entry == 'b' || entry == 'B')
                accounts.averageAccounts(accountNumbArray, accountBalArray);
            if (entry == 'x' || entry == 'X')
                Console.WriteLine("The program will now close");
                   
            //internal documentation
        } //end main

} //end wrapper class for main


If I change this line to look like this:

if (entry == 'a' || entry == 'A')
accounts.searchAccounts(accountNumbArray, accountBalArray, accountNameArray);

Then I get this error:


Error 3 Member 'Assignment_3_Moon_AccountArrays.Accounts.searchAccounts(int[], double[], string[])' cannot be accessed with an instance reference; qualify it with a type name instead C:\Users\Teddi's Dell\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 101 17 ConsoleApplication1
Error 2 Member 'Assignment_3_Moon_AccountArrays.Accounts.fillAccounts(int[], double[], string[])' cannot be accessed with an instance reference; qualify it with a type name instead C:\Users\Teddi's Dell\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 88 13 ConsoleApplication1
Error 4 Member 'Assignment_3_Moon_AccountArrays.Accounts.averageAccounts(int[], double[])' cannot be accessed with an instance reference; qualify it with a type name instead C:\Users\Teddi's Dell\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 103 17 ConsoleApplication1

Recommended Answers

All 9 Replies

You have declared your methods static, which makes them class methods.

In your code you create an instance of the Accounts class (line 78) then attempt to call these class methods like they are instance methods. Remove the keyword 'static' from the method declarations.

Thank you so much for the quick response! :~) I did as you said, then I had to remove the a, b, x and false, because I had warnings saying they were listed but not used. This did not seem to affect anything, but then I this error.

Error 1 Program 'C:\Users\Teddi's Dell\AppData\Local\Temporary Projects\ConsoleApplication1\obj\x86\Release\ConsoleApplication1.exe' does not contain a static 'Main' method suitable for an entry point ConsoleApplication1
Warning 2 'Assignment_3_Moon_AccountArrays.account_assignment.Main(int[], double[], string[])' has the wrong signature to be an entry point C:\Users\Teddi's Dell\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 77 21 ConsoleApplication1
I have never had a problem with main before. Can I pass the arguments into main as I am? I fix one thing to get more issues. LOL

Main can only have one parameter, and array of strings. So it's either Main() or Main(String[] args) .

Since never call Main (the system does that) the only way to pass parameters is on the command line.

So I am still not clear on how to get it to work. How do I know when to use Main() and when to use Main(String[] args). Sorry, this stuff is like square peg in round hole for my brain. But I am determined to get this. Thank you.

OK, here is what I have now.

I have no errors and it lets me fill the arrays. But when I enter a choice 'a', it breaks and closes. Did I use bool incorrectly?

class Accounts
    {
        // private class members
        const int arrayLength = 5;
        private int [] accountNumbArray = new int[arrayLength]; //create arrays
        private double [] accountBalArray = new double[arrayLength];
        private string [] accountNameArray = new string[arrayLength];
 
        // fill all three parallel arrays with input
        public void fillAccounts()
        {
            int arrayLength = 0;
            for (int i = 0; i < accountNumbArray.Length; i++)
            {
                Console.Write("Enter integer account number ");
                accountNumbArray[arrayLength] = Convert.ToInt32(Console.ReadLine());
                Console.Write("Enter account balance ");
                accountBalArray[arrayLength] = Convert.ToDouble(Console.ReadLine());
                Console.Write("Enter account holder last name ");
                accountNameArray[arrayLength] = Convert.ToString(Console.ReadLine());
                arrayLength++;
            } //end for
 
            //search the account num array and display
 
           
        } //end fillAccounts method

        // when found, account num entered and the corresponding bal and name
        //if Not found, display "You entered an invalid account"
 
        public void searchAccounts()
        {
            int accountNum = 0;
            bool found = false;
            int x = 0;
 
            Console.Write("Please enter the account number you wish to search for ");
            accountNum = Convert.ToInt32(Console.ReadLine());
 
            while (found)
            {
                while (x < accountNumbArray.Length && accountNum != accountNumbArray[x])
                {
                    ++x;
                    double balance = accountBalArray[x];
                    string name = accountNameArray[x];
                }
                found = true;
            }
            Console.WriteLine("Account number " + accountNumbArray[x] + " has a balance of " + accountBalArray[x]
                    + " and the account holder is " + accountNameArray[x]); 
                       
        }
 
        public void averageAccounts()
        {
            // compute and display average of all 5 bal as currency use length.
            int i = 0;
            double balance = accountBalArray[i];
            double balSum = 0;
            double avgBalance = 0;
 
            while (i > 0)
            {
                i = i + 1;
                balSum = balSum + balance; 
            }
            avgBalance = (balSum/i);
        } //end averageAccounts method

    } //end public class accounts


   class account_assignment  //wrapper class for main
   {
       static void Main()
       {
           char entry;
          

           //instantiate one new Accounts object
           Accounts accounts = new Accounts();

           //call class methods to fill accounts Array
           accounts.fillAccounts();

           //menu detailing entnries to select search (a or A) average (b or B) exit (x or X)

           Console.WriteLine("*****************************************");
           Console.WriteLine("enter an a or A to search account numbers");
           Console.WriteLine("enter a b or B to average the accounts");
           Console.WriteLine("enter an x or X to exit program");
           Console.WriteLine("*****************************************");

           entry = Convert.ToChar(Console.ReadLine());

           bool is_x_or_X = false;

           while (is_x_or_X)    //begin while
           {
               switch (entry)  //set switch
               {
                   case 'a':
                   case 'A':
                       accounts.searchAccounts();
                       break;
                   case 'b':
                   case 'B':
                       accounts.averageAccounts();
                       break;

                   default:
                       (is_x_or_X) = true;
                       break;
               }
           }  //end while

           //internal documentation
       } //end main
   } //end main wrapper class

OOPs, I see I did not put an output line of code.

Just in the interest of closure, I will post what I finally came up with. I ended up changing quite a lot.

I could not figure out how to fix the searchAccount method to handle an invalid account number correctly. But I think I got most of the requirements. Thank you for the help.
BTW, I am very open to suggestions on how to make this cleaner and more efficient. I tend to bulk things up more than necessary. I will leave it open until tomorrow night just to see if anyone posts suggestions, then I will mark it as solved. Thanks all.

namespace Accounts
{
   class Accounts
    {
        // private class members
        const int arrayLength = 5;
        private int [] accountNumbArray = new int[arrayLength]; //create arrays
        private double [] accountBalArray = new double[arrayLength];
        private string [] accountNameArray = new string[arrayLength];
 
        // fill all three parallel arrays with input
        public void fillAccounts()
        {
            int arrayLength = 0;
            for (int i = 0; i < accountNumbArray.Length; i++)
            {
                Console.Write("Enter integer account number ");              
                accountNumbArray[arrayLength] = Convert.ToInt32(Console.ReadLine());
                Console.Write("Enter account balance ");
                accountBalArray[arrayLength] = Convert.ToDouble(Console.ReadLine());
                Console.Write("Enter account holder last name ");
                accountNameArray[arrayLength] = Convert.ToString(Console.ReadLine());
                arrayLength++;
            } //end for
 
            //search the account num array and display
           
        } //end fillAccounts method

        // when found, account num entered and the corresponding bal and name
        //if Not found, display "You entered an invalid account"
 
        public void searchAccounts()
        {
            int accountNum = 1;
            bool found = true;
            int x = 1;
 
            Console.Write("Please enter the account number you wish to search for ");
            accountNum = Convert.ToInt32(Console.ReadLine());

            while (found)
            {
                while (x < accountNumbArray.Length && accountNum != accountNumbArray[x])
                {
                    
                    double balance = accountBalArray[x];
                    string name = accountNameArray[x];
                    ++x;
                }
                found = false;
                
             Console.WriteLine("Account number {0} has a balance of {1:c} and the account holder is {2}", accountNumbArray[x], accountBalArray[x], accountNameArray[x]);
            
            }   

            if (accountNum != accountNumbArray[x])
            {
                Console.WriteLine("You entered an invalid account number");
                accountNum = Convert.ToInt32(Console.ReadLine());
            }                    
        }
 
        public void averageAccounts()
        {
            // compute and display average of all 5 bal as currency use length.
            int i = 0;
            double balSum = 0;
            double avgBalance = 0;


            for (i = 0; i < arrayLength; i++ )
            {
                balSum = balSum + accountBalArray[i];
                
            }
            avgBalance = (balSum / i);
            Console.WriteLine("The average dollar amount for all accounts is {0:c}", avgBalance);
        } //end averageAccounts method

    } //end public class accounts


   class account_assignment  //wrapper class for main
   {
       static void Main()
       {
           char entry = '0';
          

           //instantiate one new Accounts object
           Accounts accounts = new Accounts();

           //call class methods to fill accounts Array
           accounts.fillAccounts();

           //menu detailing entnries to select search (a or A) average (b or B) exit (x or X)
           bool is_X = false;
           while (!is_X)    //begin while
           {
           while (entry != 'x' && entry != 'X')
           {
              

               Console.WriteLine("*****************************************");
               Console.WriteLine("enter an a or A to search account numbers");
               Console.WriteLine("enter a b or B to average the accounts");
               Console.WriteLine("enter an x or X to exit program");
               Console.WriteLine("*****************************************");

               
              entry = Convert.ToChar(Console.ReadLine());
               
                   switch (entry)  //set switch
                   {
                       case 'a':
                       case 'A':
                           accounts.searchAccounts();
                           break;
                       case 'b':
                       case 'B':
                           accounts.averageAccounts();
                           break;
                       case 'x':
                       case 'X':
                           (is_X) = true;
                           break;
                       default:
                           break;
                   }
               }  //end inner while
           } //end outer while

           //internal documentation
       } //end main
   } //end main wrapper class
} //end namespace

On line 14 you are setting the constant arrayLength to 0 ?
This must give an error.
Also in your fillAccounts method you are setting only element with index 5( the constant arrayLength )
Your arrays have 5 elements: counting from 0 to 4!

Still I am serching for the answer.it is showing an error. Anyone can help me with this please?

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.