user1863593
user1863593

Reputation: 199

Can't Add to an Array

I am working on an array tutorial. My knowledge on arrays is limited. I am encountering an error "operator '+=' cannot be applied to operands of type 'double[]'. I am not sure why this isn't working. The original code worked correctly until the tutorial requested to change using arrays. Any ideas?

Original code without arrays:

string inputString;
        double sales=0, totalA = 0, totalB = 0, totalC=0;
        double total = 0;
        string initial;

        Console.Write("Enter family initial: ");
        inputString = Console.ReadLine();
        initial = inputString.ToString();

        while (initial != "z" && initial != "Z")
        {
            Console.Write("Enter a sales amount: ");
            inputString = Console.ReadLine();
            sales = Convert.ToDouble(inputString);
            total += sales;

            if (initial == "A")
            {
                totalA += sales;
            }
            else if (initial == "B")
            {
                totalB += sales;
            }
            else if (initial == "C")
            {
                totalC += sales;
            }


            Console.Write("Enter family initial: ");
            inputString = Console.ReadLine();
            initial = inputString.ToString();

        }
        Console.WriteLine("Family A earned: {0}", totalA.ToString("C"));
        Console.WriteLine("Family B earned: {0}", totalB.ToString("C"));
        Console.WriteLine("Family C earned: {0}", totalC.ToString("C"));
        Console.WriteLine("Grand Total Sales: {0}",total.ToString("c"));

This is my attempt to modify the code (per request) to store the initials and accumulated totals sales value in an array. Any ideas of what I can do to achieve that?

string inputString;
// double sales=0, totalA = 0, totalB = 0, totalC=0;
double sales;
double total = 0;
string initial;

string[] familyNames = {"Anderson","Bowman","Claxton"};
string[] inital = { "A", "B", "C" };
double[] totalA, totalB, totalC;

Console.Write("Enter family initial: ");
inputString = Console.ReadLine();
initial = inputString.ToString();

while (initial != "z" && initial != "Z")
{
    Console.Write("Enter a sales amount: ");
    inputString = Console.ReadLine();
    sales = Convert.ToDouble(inputString);
    total += sales;




    if (initial == "A")
    {
        totalA += sales;
    }
    else if (initial == "B")
    {
        totalB += sales;
    }
    else if (initial == "C")
    {
        totalC += sales;
    }


    Console.Write("Enter family initial: ");
    inputString = Console.ReadLine();
    initial = inputString.ToString();

}
Console.WriteLine("Family A earned: {0}", totalA.ToString("C"));
Console.WriteLine("Family B earned: {0}", totalB.ToString("C"));
Console.WriteLine("Family C earned: {0}", totalC.ToString("C"));
Console.WriteLine("Grand Total Sales: {0}",total.ToString("c"));

Upvotes: 1

Views: 226

Answers (3)

Michael Roy
Michael Roy

Reputation: 184

Since this is a tutorial that you're following, I think that the point of this exercise was to eliminate the 3 total variables and use an array instead.

So, instead of having totalA, totalB and totalC, you would just have a 3 element array: totals. Perhaps something like this:

string inputString;
double sales;
double total = 0;
string initial;

string[] familyNames = {"Anderson","Bowman","Claxton"};
string[] inital = { "A", "B", "C" };
double[] totals = new double[3];

Console.Write("Enter family initial: ");
inputString = Console.ReadLine();
initial = inputString.ToString();

while (initial != "z" && initial != "Z")
{
    Console.Write("Enter a sales amount: ");
    inputString = Console.ReadLine();
    sales = Convert.ToDouble(inputString);
    total += sales;

    if (initial == "A")
    {
        totals[0] += sales;
    }
    else if (initial == "B")
    {
        totals[1] += sales;
    }
    else if (initial == "C")
    {
        totals[2] += sales;
    }


    Console.Write("Enter family initial: ");
    inputString = Console.ReadLine();
    initial = inputString.ToString();

}
Console.WriteLine("Family A earned: {0}", totals[0].ToString("C"));
Console.WriteLine("Family B earned: {0}", totals[1].ToString("C"));
Console.WriteLine("Family C earned: {0}", totals[2].ToString("C"));
Console.WriteLine("Grand Total Sales: {0}",total.ToString("c"));

There are ways to optimize this, for instance using a loop to print out the totals, but I'll leave that to you.

Upvotes: 1

Vlad
Vlad

Reputation: 1919

The + operator doesn't apply to arrays. For example:

double[] array = new double[2];
array = array + 2; //// this is undefined.

In your case are totalA, totalB, totalC double (values) or double[] (arrays)?

Upvotes: 0

Tilak
Tilak

Reputation: 30698

Following lines have problem

totalA += sales;
totalB += sales;
totalC += sales;

because totalA/totalB/totalC are declared as double array.

After Analyzing your code, I sense that you donot need double array anyway. So, to correct the issue, you need to change following

double[] totalA, totalB, totalC;

to

double totalA = 0;
double totalB = 0;
double totalC = 0;

Upvotes: 1

Related Questions