Question: In this C# program, I need to add the number of items that have been entered by the user . If 3 items were entered

In this C# program, I need to add the number of items that have been entered by the user .

If 3 items were entered by the user , the program should show: "There are 3 item in this order." If 2 items were enter; "There are 2 items in this order."

Any thoughts of how to do that?

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

namespace ItemsApp { class Program { static void Main(string[] args) { //declare variable to hold count int count = 0; //declare a 2d array string[,] items = new string[100, 4]; //variable that controls the loop bool repeat = true; //variable to hold item name string name; //variables to hold price,subtotal, tax and total decimal price, subtotal=0.0M,tax,total; //variable to hold quantity int quantity;

//do-while loop that reads items from user until user enters 0 do { //run a loop to read the items for (int i = 0; i < 100; i++) { //read the item name Console.Write(" Item Name (enter 0 to stop): "); name = Console.ReadLine();

//if user entered 0 exit the loop by setting repeat to false if (name.Trim() == "0") { repeat = false; break; } //else continue read the item details else { //read the price of the item Console.Write(" Item Price: "); //convert it to decimal by removing $ from the user input price = Convert.ToDecimal(Console.ReadLine().Replace('$', ' ').Trim());

//prompt and read the quantity Console.Write(" Quantity: "); quantity = Convert.ToInt32(Console.ReadLine());

//compute subtotal subtotal = price * quantity;

//store the item details int the array items[i, 0] = name; items[i, 1] = price.ToString(); items[i, 2] = quantity.ToString(); items[i, 3] = subtotal.ToString(); count++; //increment count } } } while (repeat);

//loop through the items array and compute the sub total subtotal = 0.0M; for (int i = 0; i < count; i++) { subtotal = subtotal + Convert.ToDecimal(items[i, 3]); }

//calculate the tax tax = subtotal * Convert.ToDecimal(0.065); //calculate the total total = subtotal + tax;

//add $ symbol for price and subtotal in the array for (int i = 0; i < count; i++) { items[i, 1] = "$" + items[i, 1]; items[i, 3] = "$" + items[i, 3]; }

//print the receipt Console.WriteLine(" Your Receipt "); //print the header Console.WriteLine("Item\t\tPrice\t Quantity\t Subtotal "); //loop through the array and print the items for (int i = 0; i < count; i++) { for (int j = 0; j < 4; j++) { //{0,-15} is used to left align and fix a length of 15, 0 represents the index of the output parameter Console.Write("{0,-15}", items[i, j]); } Console.WriteLine(); }

//print the subtotal Console.WriteLine(" Subtotal : $" + subtotal.ToString("0.00")); //print the tax Console.WriteLine(" Tax(0.065%) : $" + tax.ToString("0.00")); //print the total Console.WriteLine(" Total : $" + total.ToString("0.00"));

//terminate the program Console.WriteLine(" Press any key to exit..."); Console.ReadKey(); } } }

Output:

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!