I have never run into this problem before and I am frustrated. What am I missing? I am trying to initialize an array of five social security numbers. It does fine until the fifth one, then throws an error saying something about the indexer out of bounds.

SSN has to be a string, Also would like help in formatting it as xxx-xx-xxxx. The line I commented out gave me other errors. Thank you.

static void Main(string[] args)
        {
            //  instantiate an array of five (5) Taxpayer objects.
           
            string SSN = "0";
            int income = 0;
            int tax = 0;
            int x = 0;
            Taxpayer[] taxArray = new Taxpayer[5];
            

            //  Implement a for-loop that will prompt the user to enter the Social Security Number and gross income.
            for (x = 0; x <= taxArray.Length; x++)
            {
                taxArray[x] = new Taxpayer();
                Console.Write("Please enter the Social Security Number for taxpayer {0}:  ", x);
                SSN = Convert.ToString(Console.ReadLine());
                //SSN = String.Format("{0:000-00-0000}");
                Console.Write("Please enter the gross income for taxpayer {0}:  ", x);
                income = Convert.ToInt32(Console.ReadLine());

                Taxpayer.GetRates();
            }

Recommended Answers

All 4 Replies

array index is 0---> length -1 Use line number 13 like this

for (x = 0; x < taxArray.Length; x++)
commented: Good observation. +9

Thank you! I had tried that before, but something else must have not been right. Now it works for me. Now I need to get the data for each member of the array to display at the end. I have this wrong, it just displays the same data 5 times. How can I get the information for all five to store and print. Thank you.

//  Implement a for-loop that will prompt the user to enter the Social Security Number and gross income.
            for (x = 0; x < taxArray.Length; x++)
            {
                taxArray[x] = new Taxpayer();
                Console.Write("Please enter the Social Security Number for taxpayer {0}:  ", x);
                SSN = Convert.ToString(Console.ReadLine());
                //SSN = String.Format("{0:000-00-0000}");
                Console.Write("Please enter the gross income for taxpayer {0}:  ", x);
                income = Convert.ToInt32(Console.ReadLine());

                Taxpayer.GetRates();

            }

                //  Implement a for-loop that will display each object as formatted taxpayer SSN, income and calculated tax.
                for (int i = 0; i < 5; i++)
                {
                    Console.WriteLine("Taxpayer # {0} SSN: {1}, Income is {2:c}, Tax is {3:c}", i, SSN, income, tax);
                }

in your code SSN is a string not a string array and also income is int not an int array so it overwrites the previous values with every loop iteration

for the formatting of ssn try this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            textBox1.MaxLength = 11;
            textBox1.TextAlign = HorizontalAlignment.Center;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            {
                if (textBox1.Text.Length == 3)
                {
                    textBox1.Text += '-';
                    textBox1.SelectionStart = 5;
                }
                if (textBox1.Text.Length == 6)
                {
                    textBox1.Text += '-';
                    textBox1.SelectionStart = 8;
                }

            }
        }
    }
}
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.