Tropical
Tropical

Reputation: 23

Console.Redline reading second line

I have a simple program that tells the user to input n amount of students, foreach student an x amount of money is allocated. At the end, the program divides x by n, meaning the total money is divided equally by the students.

The problem is that Console.Readline() is reading the second inputted value as see below.:

Console output

This means that the user has to enter the values two times, each time Console.Readline() is called, which is obviously wrong!

Code:

static void Main(string[] args)
{
    double total = 0;
    int noOfStudents = 0;
    try
    {
        Console.WriteLine("Please enter the number of students :");
        noOfStudents = checkInputTypeInt(Console.ReadLine());
        Console.WriteLine("Enter the students(" + noOfStudents + ") money!");

        for (int i = 0; i <= noOfStudents - 1; i++)
        {
            double money = checkInputTypeDouble(Console.ReadLine());
            total += money;
        }
        double dividedTotal = total / noOfStudents;
        Console.WriteLine("Total divided by " + noOfStudents + " is $ " + dividedTotal.ToString("F2"));
        Console.ReadKey();
    }
    catch(Exception e)
    {
        Console.WriteLine(e);
    }
}

private static int checkInputTypeInt(string s)
{
    int numericValue = 0;
    bool done = false;
    while (!done)
    {
        if (!int.TryParse(Console.ReadLine(), out numericValue))
            Console.WriteLine("The input must be between 0 and 1000!");
        else if (numericValue > 100)
            Console.WriteLine("The input must be between 0 and 1000!");
        else
            done = true;
    }
    return numericValue;
}

Upvotes: 0

Views: 666

Answers (2)

Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34180

You ReadLine twice:

noOfStudents = checkInputTypeInt(Console.ReadLine());

and in checkInputTypeInt method:

if (!int.TryParse(Console.ReadLine(), out numericValue))

You should either send only the string to method like this:

  noOfStudents = checkInputTypeInt(Console.ReadLine());

and in your method just check that value like:

if (!int.TryParse(s, out numericValue))

in this case you should have the while loop in your main method.

or just use readline in the called method.

 Console.WriteLine("Please enter the number of students :");
 noOfStudents = checkInputTypeInt(); // and you should change your method to fit no arguments.

Edit: final codes:

static void Main(string[] args)
{
    double total = 0;
    int noOfStudents = 0;
    try
    {
        Console.WriteLine("Please enter the number of students :");
        noOfStudents = checkInputTypeInt();
        Console.WriteLine("Enter the students(" + noOfStudents + ") money!");

        for (int i = 0; i <= noOfStudents - 1; i++)
        {
            double money = checkInputTypeDouble(Console.ReadLine());
            total += money;
        }
        double dividedTotal = total / noOfStudents;
        Console.WriteLine("Total divided by " + noOfStudents + " is $ " + dividedTotal.ToString("F2"));
        Console.ReadKey();
    }
    catch(Exception e)
    {
        Console.WriteLine(e);
    }
}

private static int checkInputTypeInt()
{
    int numericValue = 0;
    bool done = false;
    while (!done)
    {
        if (!int.TryParse(Console.ReadLine(), out numericValue))
            Console.WriteLine("The input must be between 0 and 1000!");
        else if (numericValue > 100)
            Console.WriteLine("The input must be between 0 and 1000!");
        else
            done = true;
    }
    return numericValue;
}

OR:

static void Main(string[] args)
{
    double total = 0;
    int noOfStudents = -1;
    try
    { 
        Console.WriteLine("Please enter the number of students :");
        do{
        noOfStudents = checkInputTypeInt(Console.ReadLine());
        }while(noOfStudents == -1);
        Console.WriteLine("Enter the students(" + noOfStudents + ") money!");

        for (int i = 0; i <= noOfStudents - 1; i++)
        {
            double money = checkInputTypeDouble(Console.ReadLine());
            total += money;
        }
        double dividedTotal = total / noOfStudents;
        Console.WriteLine("Total divided by " + noOfStudents + " is $ " + dividedTotal.ToString("F2"));
        Console.ReadKey();
    }
    catch(Exception e)
    {
        Console.WriteLine(e);
    }
}

private static int checkInputTypeInt(string s)
{
    int numericValue = -1;
        if (!int.TryParse(Console.ReadLine(), out numericValue))
            Console.WriteLine("The input must be between 0 and 1000!");
        else if (numericValue > 1000)
            {Console.WriteLine("The input must be between 0 and 1000!");
             numericValue = -1;
    }
    return numericValue;
}

Upvotes: 1

RafBianco
RafBianco

Reputation: 1

Try to divide the noOfStudents and Console.ReadLine() like this:

int i = Convert.ToInt32(Console.ReadLine());
noOfStudents = i;

Upvotes: 0

Related Questions