Creating array, but every new value writes over the previous one

60 Views Asked by At

I've got a project due tomorrow I'm having trouble with. I'm new to programming and I'm building a project for school using parallel arrays. When I access my AddItem method from my main, it will write over the previous method call every time. If you have any input on how I can fix this please let me know. Here are the two methods in question:

using System;
using System.Linq;  
using System.IO;

namespace Assignment3
{
    class Program
    {
        static void Main()
        {
            // set the physical array size and declare the parallel arrays
            const int PHYSICAL_ARRAY_SIZE = 6;
            string[] menuItems = new string[PHYSICAL_ARRAY_SIZE];
            double[] menuPrices = new double[PHYSICAL_ARRAY_SIZE];

            // set the menu choices and quiz parameters
            char[] menuChoices = { '1', '2', '3', '4', '5', '6', '7', '0' };
            char userChoice;
            int logicalArraySize = 0;
            do
            {              
                userChoice = GetMenuChoice(menuChoices);

                if (userChoice == '1')
                {
                    logicalArraySize = AddItem(menuItems, menuPrices);
                }
                else 
                {
                    ProcessMenuChoice(userChoice, menuPrices, menuItems,
                        ref logicalArraySize, PHYSICAL_ARRAY_SIZE);
                }
            } while (userChoice != menuChoices[^1]);
        }


        public static int AddItem(string[] items, double[] prices) 
        {
            double price;
            string description;
            int count = 0;

            Console.Clear();
            Console.WriteLine("New Item");
            Console.WriteLine("-----------------------------------------");
            Console.WriteLine("Enter an item to add to bill: ");
            
            string message = "Enter description: ";
            description = PromptForStringNotEmpty(message);

            Console.Write($"Enter price of {description}: ");
            price = GetSafeDouble();

            Console.WriteLine("Add Item was successful");
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
            Console.Clear();

            prices[count] = price; 
            items[count] = description; 
            count++;
    
            return count;
        }
0

There are 0 best solutions below