0

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;
        }
Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
CheckDeck
  • 5
  • 3
  • 1
    Why wouldn't it? Each time `AddItem` runs, `count` is created as `0`. – ProgrammingLlama Nov 28 '21 at 04:42
  • keep your count variable local to the Main method and pass it to the AddItem method along with other two arguments. Increment it after you call the AddItem method in your `Main` method. – Jawad Nov 28 '21 at 04:48
  • Better still, wrap this in a class. Keep the count and the array in the class, and when someone calls Add, set the array at index count to the value. Also be aware of how long the array is. If an Add operation results in the current count exceeding the length of the array, create a new array, twice as long. Then copy the old array into the beginning of the new array (using it instead of the old array). At that point, you can realize you mostly implemented `List` – Flydog57 Nov 28 '21 at 05:00
  • *"...using parallel arrays."* <== What do you mean by that? I can't see anything related to parallel processing inside the code in the question. – Theodor Zoulias Nov 28 '21 at 09:07

0 Answers0