Question: I am trying to store data into two arrays using a do-while loop. However the data is being lost. Can someone help me figure this
I am trying to store data into two arrays using a do-while loop. However the data is being lost. Can someone help me figure this out?
Note: I have to use arrays and if I don't initialize the arrays within the loop I will get an out-of-bounds error message.
C# Code
using System;
class Program { //Function to calulate a customers order. static double CalculateOrder(double cost, double option) { //Declarations string stylus, glasses, canvas;
//Take in the base value of the HoloScroll tablet. Console.WriteLine(" Enter HoloScroll Base Price: "); cost = Convert.ToDouble(Console.ReadLine());
//Confirm the size of the tablet the customer wants. Console.WriteLine(" Press 1 for the regular canvas or 2 for larger canvas: "); option = Convert.ToDouble(Console.ReadLine()); if (option == 2) { cost += 400; }
//Determine if the customer wants to include a stylus. Console.WriteLine(" Add a HoloFeather? (y/n): "); stylus = Console.ReadLine(); stylus = stylus.ToLower(); if (stylus == "y" || stylus == "yes") { cost += 200; }
//Determine if the customer wants to include a base station. Console.WriteLine(" Add a HoloBase? (y/n): "); canvas = Console.ReadLine(); canvas = canvas.ToLower(); if (canvas == "y" || canvas == "yes") { cost += 500; }
//Determine if the customer wants to include a pair of privacy glasses. Console.WriteLine(" Add a HoloSights? (y/n): "); glasses = Console.ReadLine(); glasses = glasses.ToLower(); if (glasses == "y" || glasses == "yes") { cost += 200; } return cost; }
static void Main(string[] args) { //Introduction Message Console.WriteLine("Welcome to Ghost Inc.!");
//Declarations double cost = 0, option = 0, total; string name; int response, x = 0; string[] customer; double[] amount;
//This part decides whether to enter or exit the loop. Console.WriteLine(" Select a menu item:"); Console.WriteLine(" 1. Place an Order"); Console.WriteLine(" 2. Finish for the day"); response = Convert.ToInt16(Console.ReadLine());
//The do while loop will continue until option 2 is selected. do { //Counter to increase the max value of both arrays x++;
//Initializing both arrays customer = new string[x]; amount = new double[x];
//Receive customer's name and adds it to the array called "customer". Console.WriteLine(" Enter Customer Name: "); name = Console.ReadLine(); customer[x-1] = name;
//This part starts the function called "CalculateOrder" //and takes the final cost value, assigns it to "total" //then adds it to the array called "amount". total = CalculateOrder(cost, option); amount[x-1] = total;
//Output the customer's name and thier total. Console.WriteLine($" Order placed for {name} ${total}");
//This part decides whether to enter or exit the loop. Console.WriteLine(" Select a menu item:"); Console.WriteLine(" 1. Place an Order"); Console.WriteLine(" 2. Finish for the day"); response = Convert.ToInt16(Console.ReadLine());
} while (response == 1) ;
//Locates and open file named "todaySales.txt" StreamWriter todaySales = new StreamWriter(@"C:\Users\samma\OneDrive\Documents\BlueRidge\SDE-193 (C#)\todaySales.txt");
//Using a foreach loop it outputs the objects //within the arrays "customer" and "amount" foreach (var (y, z) in customer.Zip(amount)) { todaySales.WriteLine($"{y}, ${z}"); }
//Closes the file named "todaySales.txt" todaySales.Close();
//Closing Message Console.WriteLine(" Have a great rest of your day!"); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
