Reputation: 29
I am attempting to create a program that makes receipts for people. The console will ask for the item name, price, and quantity; following this it should print out the array (formatted hopefully). But the issue here is I am stuck on trying to figure out how to break the array naturally. As I am supposed to end the loop when a user inputs 0 but I cannot find a way to put that into a boolean statement.
Ive checked the Microsoft documentation and have not been able to find a solution to this specific problem. I also checked here but to no avail.
public class InitArray
{
// create and output rectangular and jagged arrays
public static void Main(string[] args)
{
string[,] items = new string[100, 4];
do
{
for (int row = 0; row < items.GetLength(0); row++)
{
// loop through columns of current row
double price = 0;
double subtotal = 0;
Console.WriteLine("Item Name(enter 0 to stop): ");
items[row, 0] = Convert.ToString(Console.ReadLine());
Console.WriteLine("Item Price(enter 0 to stop): ");
items[row, 1] = Convert.ToString(Console.ReadLine());
Console.WriteLine("Quantity(enter 0 to stop): ");
items[row, 2] = Convert.ToString(Console.ReadLine());
items[row, 3] = Convert.ToString(price + subtotal);
}
Console.WriteLine(); // start new line of output
}
while ();
} // end method OutputArray
} // end class InitArray
Upvotes: 1
Views: 509
Reputation: 11563
Based on the answer of @Steve you could also use logical ANDs.
List<Product> products = new List<Product>();
while (getInput("Item Name(enter 0 to stop): ", out var name)
&& getInput("Item Price(enter 0 to stop): ", out var price)
&& getInput("Quantity(enter 0 to stop): ", out var quantity))
{
products.Add(new Product
{
Name = name,
Price = Convert.ToDecimal(price),
Quantity = Convert.ToInt32(quantity)
});
}
decimal totalPrice = products.Sum(p => p.Price);
Console.WriteLine(totalPrice);
Upvotes: 1
Reputation: 216293
You should use a more convenient way to store your information. Better use a List<Product>
instead of a bidimensional array. Using a List of a specific class will allow you to not worry about array dimensions, but also will allow you to have all your data with a correct datatype and not a bunch of strings.
Your problem to stop the input whenever you get a "0" from your user could be solved with a generic function that returns a boolean if your user has requested to stop the inputs.
So first define a Product class
public class Product
{
public string Name {get;set;}
public int Quantity { get; set; }
public decimal Price { get; set; }
... other field if you need them...
}
Now write a boolean function that signals the calling code the intention to stop the inputs
public bool getInput(string message, out string result)
{
Console.WriteLine(message);
result = Console.ReadLine();
return (!result.Equals("0"))
}
In the function above, we return the input from the user in the out variable and if the user asks to stop we return false.
Now to put all together we could write this
List<Product> products = new List<Product>();
bool ok = true;
while(ok)
{
string input;
Product p = new Product();
ok = getInput("Item Name(enter 0 to stop): ", out input);
if (ok) p.Name = input;
if (ok) ok = getInput("Item Price(enter 0 to stop): ", out input);
if (ok) p.Price = Convert.ToDecimal(input);
if (ok) ok = getInput("Quantity(enter 0 to stop): ", out input);
if (ok) { p.Quantity = Convert.ToInt32(input); products.Add(p);}
}
decimal totalPrice = products.Sum(p => p.Price);
Console.WriteLine(totalPrice);
Upvotes: 1
Reputation: 28499
You already have a loop with the for-Statement. You don't need an additional loop. Break out of the for-Loop by using break
when the user has entered "0".
public static void Main(string[] args)
{
string[,] items = new string[100, 4];
for (int row = 0; row < items.GetLength(0); row++)
{
// loop through columns of current row
string input;
Console.WriteLine("Item Name(enter 0 to stop): ");
input = Console.ReadLine();
if (input == "0") break;
items[row, 0] = input;
Console.WriteLine("Item Price(enter 0 to stop): ");
input = Console.ReadLine();
if (input == "0") break;
items[row, 1] = input;
Console.WriteLine("Quantity(enter 0 to stop): ");
input = Console.ReadLine();
if (input == "0") break;
items[row, 2] = input;
// ...
}
}
Upvotes: 1