Tommy Vidrio Tommy Vidrio - 1 month ago 30
C# Question

Pizza Parallel Arrays

I need to define three arrays (Size, Price, and Extra). I then had to ask them their size and their choice for toppings.

Using a for loop and the parallel array technique, walk through the size array to find the size that matches the user’s input. Using the current index of your loop, look up the price for that size in the price array, and write out to the user how much the pizza will cost.

If the user said they do want extra toppings, still using the same index for your loop, look up how much that costs in your extra array, and tell the user the total cost for the pizza.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string[] sizes = { "S", "M", "L", "X" };
double[] prices = { 6.99, 8.99, 12.50, 15.00 };
double[] extra = { 1.00, 2.00, 3.25, 4.50 };
string inputToppings;
string inputSize;
Console.Write("What size pizza would you like? Enter S, M, L, or X: ");
inputSize = Console.ReadLine();
Console.Write("Would you like extra toppings? Enter Y or N: ");
inputToppings = Console.ReadLine();
for (int i = 0; i < sizes.Length; i++)
{
if (sizes[i] == inputSize)
{
Console.WriteLine("You ordered a {0} pizza that costs {1:C}.", sizes[i], prices[i]);
break;
}
}
Console.ReadLine();
}
}
}


My Problem is that I can get the correct input for the Pizza Size and Prices, but I cannot a working statement to display the price of the pizza size WITH the toppings in the form of a WriteLine. I have spent hours on this and I cannot find a working method. Please help...

Answer

Try changing your "for" loop to:

        for (int i = 0; i < sizes.Length; i++)
        {
            if (sizes[i] == inputSize)
            {
                var totalPrice = prices[i] + (inputToppings == "Y" ? extra[i] : 0);
                Console.WriteLine("You ordered a {0} pizza that costs {1:C}.", sizes[i], totalPrice);
                break;
            }
        }

'Hope this helps.