Kevin
Kevin

Reputation: 25

C#--Why do I have to hit enter twice to commit Console.Readline

C# newbie. Trying to make a simple gradebook program where user:

  1. Enters names of students until 'done' is entered
  2. Enter grades for each user, then calculate average

Part 2 works, but my problem is with part one--you have to hit enter twice to commit the name to the list. For instance, if I enter Bob, Lisa, Kevin, Jane--only Bob and Kevin would make it in--the second line (even if you type something) acts as the line where the console.read is committed to the list.

Here's my code:

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

namespace Csharp
{

    class MainClass
    {
        static List<string> mylist = new List<string> { };
        public static void Main(string[] args)
        {

            UserInput();
            GradeEnter();
        }
        public static void UserInput()
        {
            Console.WriteLine("Enter Some names (type 'done' when finished)");
            do
            {
                mylist.Add(Console.ReadLine());
            } while (!Console.ReadLine().Equals("done"));


        }
        public static void GradeEnter()
        {

            foreach (var x in mylist)
            {
                List<int> myInts = new List<int>();
                Console.WriteLine("\nEnter grades for {0}, (enter any letter when done)", x);
                while (Int32.TryParse(Console.ReadLine(), out int number))
                {
                    myInts.Add(number);
                }
                Console.Write("Average is ");
                Console.Write(myInts.Average());

            }
        }

    }
}

Any help on this would be much much appreciated!

Thanks

Upvotes: 1

Views: 1275

Answers (3)

Kevin
Kevin

Reputation: 25

Thanks for everyone's help. I ended up using a combination of while(true) and an if statement:

Console.WriteLine("Enter some names (type 'done' when finished)");
            do
            {
                string name = Console.ReadLine();
                if (!name.Equals("done"))
                {
                    mylist.Add(name);
                }
                else
                    break;


            } while (true);

Upvotes: 0

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186823

Read name once and then either add it to myList or stop looping:

public static void UserInput() {
  Console.WriteLine("Enter Some names (type done to exit)");

  for (string name = Console.ReadLine(); !name.Equals("done"); name = Console.ReadLine()) 
    mylist.Add(name);
}

Upvotes: 0

Carlos Garcia
Carlos Garcia

Reputation: 2970

You are calling ReadLine twice. You can try this instead:

public static void UserInput()
{
    Console.WriteLine("Enter Some names (type done to exit)");
    string name = Console.ReadLine();
    while (!name.Equals("done"));
    {
        mylist.Add(name);
        name = Console.ReadLine();
    } 
}

Another way of doing the same

public static void UserInput()
{
    Console.WriteLine("Enter Some names (type done to exit)");
    while (true);
    {
        string name = Console.ReadLine();
        if (name == "done")
        {
            // This will stop the while-loop
            break;
        } 
        mylist.Add(name);
    } 
}

Now let's analyze what your code is doing

        do
        {
            // Read line and add it to the list. Even if the user writes "done" 
            mylist.Add(Console.ReadLine());

        // Read the console again, if the user enters done, exit. But if the user enters other name, you are discarding it, you are not adding it to the list
        } while (!Console.ReadLine().Equals("done"));

Some test cases using your code:

1. Peter <- gets added to the list
2. Lucas <- does not get added to the list, just checks if it is done
3. Mario <- gets added to the list
4. Juan  <- again, just checking if it is done, so not added to the list
5. done  <- It is treated like a name, so it will be added to the list
6. done  <- now it will finish :) 

Upvotes: 2

Related Questions