Home > Software design >  How to add objects to a list in a while loop with user input?
How to add objects to a list in a while loop with user input?

Time:06-27

I've made this class:

public class Row
{
    public string ID { get; set; }
    public int Value { get; set; }

    public Row(string id, int rowValue)
    {
        this.ID = id;
        this.Value = rowValue;
    }
}

And right now I'm adding objects to a list with the following code. It works, but the thing is I have to add those objects with an user input, something like I did with the 1st row AND while (TOTAL <= maxTotal). If next row I'm trying to add makes TOTAL more than maxTotal it is not added to the list. So I suppose I have to put that somehow in a while loop, but I can't seem to do it. I just started to learn C# and I can't find how to do it and I don't know if I'm on the right path.

public class Program
{
    static void Main(string[] args)
    {
        List<Row> rows = new List<Row>();
        int rowValue;
        int TOTAL = 0;
        int maxTotal = 100;
        
        Console.Write("Insert row ID: ");
        string id = Console.ReadLine();

        Console.Write("Insert row value: ");
        int.TryParse(Console.ReadLine(), out rowValue);
        

        Row row1 = new Row(id, rowValue); // if this rowValue would be 20 then row3 shouldn't be added
        Row row2 = new Row("BBB", 40);
        Row row3 = new Row("CCC", 50);
        rows.Add(row1);
        rows.Add(row2);
        rows.Add(row3);

        for (int i = 0; i < rows.Count; i  )
        {
            TOTAL  = rows[i].Value;
        }

        for (int i = 0; i < rows.Count; i  )
        {
            Console.WriteLine(rows[i].ID   " "   rows[i].Value);
        }
    }
}

CodePudding user response:

Read the input in a loop and break out of the loop if total gets too large:

List<Row> rows = new List<Row>();
int total = 0;
int maxTotal = 100;

// The loop only ends when the `break` is hit later
while (true) {
    Console.Write("Insert row ID: ");
    string id = Console.ReadLine();
    // TODO: You might want to add a condition here to allow the user to end loop.
    // For example, if the user enters "quit" or an empty string "".

    int rowValue;
    Console.Write("Insert row value: ");
    int.TryParse(Console.ReadLine(), out rowValue);
    // TODO: what it TryParse fails?

    // If this new data goes over the limit, exit loop
    total  = rowValue;
    if (total > maxTotal) {
        break;
    }

    // Add the new data
    rows.Add(new Row(id, rowValue));
}

CodePudding user response:

If you don't know how many times a loop needs to execute you use a while() { } loop structure. And inside the loop, you need to do four things

  1. Accept user input
  2. Check if the user input terminates the loop and issue a break; statement
  3. Parse the input into data
  4. Add the data to a list/collection.

Then outside the loop, you can do the processing you want (summing totals etc).

You have most of the pieces of the above together already, you just missing the actual loop and to arrange them in the order shown above.

  •  Tags:  
  • c#
  • Related