0

Assignment is as follows:

"Write a program that accepts any number of monthly sales amounts. Display the total of the values. Display a report showing each original value entered and the percentage that value contributes to the total. You may prompt the user for the number of values to be inputted."

Code runs fine outside of my "percent" variable always coming back as zero when listing the sales amount and percentage of total...my 4AM brain is stumped.

static void Main(string[] args)
    {
        int arraylength;
        string inValue;
        Console.WriteLine("Input desired length of array");
        inValue = Console.ReadLine();
        if  (int.TryParse(inValue,  out  arraylength)  ==  false)
            Console.WriteLine("Invalid  data  entered  -  " +
            "0 recorded for number of sales.");
        int [] sales = new int [arraylength];
        for (int i = 0; i < arraylength; i++)
        {
            inValue = Console.ReadLine();
            sales[i] = int.Parse(inValue);
        }
        int sum = 0;
        for (int i = 0; i < arraylength; i++)
        {
            sum += sales[i];
        }
        for (int i = 0; i < arraylength; i++)
        {
            int percent = sales[i] / sum;
            Console.WriteLine("Sales amount is " + sales[i]);
            Console.WriteLine("Percentage of total is " + percent);
        }
        Console.ReadKey();

       }
A B
  • 37
  • 1
  • 1
  • 10
  • Thank you. That bit of logic always escapes me. – A B Mar 28 '15 at 11:08
  • This is not about logic, this is about numbers and understanding how they divide – Mrinal Kamboj Mar 28 '15 at 11:17
  • Two integers divide, in reality. Two decimals divide, in reality. An integer and a decimal divide, in reality. I may have gotten the vocab wrong, but the fact that I tell VisualStudio to expect an integer when a decimal should be displayed was the flaw in this code. – A B Mar 29 '15 at 04:08

0 Answers0