1

I'm setting up a console application that will take the input from the user and make a new object with my class "Items". But I want a loop there as well so the user can do multiple inputs, what kind of loop should I use and how does it works if I want the variable "foo" to change through the loop?

I've tried some for loops and I'm trying to put the "i" after "foo" but did't get it to work...

        Console.WriteLine("Write a item:");
        string item = Console.ReadLine();
        Console.WriteLine("Write a price:");
        decimal price = Convert.ToDecimal(Console.ReadLine());
        var foo = new Items(item, price);
        Console.Write(" {0} {1} ", foo.Item, foo.Price);

Edit: Sorry, I was thinking I should simplify everything. My mistake! Like one of the comment said, I want to store every object created at each iteration. Then showing it like a "shopping list" with the most expensive at the top and the cheapest item at the bottom.

This is one of the things I've tried. (not a for loop but with the same concept of i++ and so on)

        do
        {
       Console.WriteLine("Insert items that you want to buy, when you are finnish, write 'done'");
        Console.Write("Insert a item: ");
            string item = Console.ReadLine();
            switch (item.ToLower())
            {
                case "done":
                    break;

                default:
                    Console.WriteLine("Write a price:");
                    decimal price = Convert.ToDecimal(Console.ReadLine());
                    var foo(i) = new Items(item, price);
                    Console.Write(" {0} {1} ", foo.Item, foo.Price);
                    break;
            }
            i++;
        } while (item != "done");

        //Then show the list in order
3
  • 1
    Do you want to store each object created at each iteration as well? Commented Jun 5, 2019 at 11:18
  • 1
    "I've tried some for loops" - Not according to the code you're showing us you haven't. "but did't get it to work" - What was your attempt and what didn't work about it? Currently you're asking "How do I write a loop in C#?" This is generally answered by introductory tutorials on the language. Stack Overflow does not seek to replace general tutorials. If you have some code you've written which isn't working as expected in a specific way, we can help with that. In what way is the code you're showing us not working? Commented Jun 5, 2019 at 11:18
  • Sorry for my lack of information. I did edit the post so maybe this makes more sense. @David That was my mistake, what I am asking for is how to change the variable "foo" for every iteration. Commented Jun 5, 2019 at 11:39

2 Answers 2

1

Surely the compiler is telling you about this syntax error:

var foo(i) = new Items(item, price);

The variable declaration is broken. It looks like you're trying to create a series of numbered variables. Any time you want to do this, what you really want is a collection. Something like a List<T> for example. You would declare the list before the loop, then add to the list within the loop.

You probably also want to declare your input variable before the loop. And name your variables sensibly, this will help you understand and support your own code. (For example, you should rename your Items class to Item. Because it represents a single "item" object.)

Something like:

var items = new List<Items>();
var itemName = string.Empty;
do
{
    Console.WriteLine("Insert items that you want to buy, when you are finnish, write 'done'");
    Console.Write("Insert a item: ");
    itemName = Console.ReadLine();
    switch (itemName.ToLower())
    {
        case "done":
            break;
        default:
            Console.WriteLine("Write a price:");
            decimal itemPrice = Convert.ToDecimal(Console.ReadLine());
            var item = new Items(itemName, itemPrice);
            items.Add(item);
            break;
    }
} while (itemName != "done");

// Here you now have a list of items.  You can loop over that list for further logic, output, etc.
Sign up to request clarification or add additional context in comments.

3 Comments

var itemName = string.Empty; and i++ feels unnecessary. itemName is never checked until after it has been assign to a value and i++ is never used. But my compiler complains about the default: but only on your code but not mine, cant find why Otherwise it feels like this is working, Thank you alot @David!
@SkillLevelBeginner: In that case remove the i++ if you don't use it. You can also move the itemName declaration inside the loop if you like, I haven't tested if that will still be in scope in the while condition check, but you'll find out when you try.
It was just the break; that was missing below default:. Thank you for all help!
0

You may try this also for create application using C#. You can define length for how many items you want to add. Here i have used 2 length to add item in cart.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace StackQue
{
    class Sample
    {
        public static void Main(string[] args)
        {
            Dictionary<string, decimal> items = new Dictionary<string, decimal>();
            for (var i = 0; i < 2; i++)
            {
                Console.WriteLine("Write an item");
                string item = Console.ReadLine();
                Console.WriteLine("Write an price");
                decimal price = Convert.ToDecimal(Console.ReadLine());
                items.Add(item, price);
            }
            foreach (var item in items)
            {
                Console.WriteLine("Item " + item.Key + " have price " + item.Value + ".");
            }
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.