Home > Back-end >  I need to add user's input into a list, then print it out. C#
I need to add user's input into a list, then print it out. C#

Time:12-11

I've run into a problem with my school assignment. The program is supposed to ask the user their grades (from 0 to 5) and add them to a list. The user may write as many grades as they want and a blank space ends the loop. The program is then supposed to show the user the number of grades they wrote, and the grade's average.

I've been stuck with the latter part, when the loop ends and the program is supposed to show the number of grades and the average.

    internal class Program
    {
        public static void YourGrades()
        {
            List<int> Grades = new List<int>();

            Console.WriteLine("Write a grade (0-5), [Enter] gives the results:");
            string grades = Console.ReadLine();

            while (grades != "")
            {
                Grades.Add(int.Parse(grades));
            }
            if (grades == "")
            {
                int num = Int32.Parse(grades);
                string average = num.Average();
                string sum = num.Length;
                Console.WriteLine($"The average of your {sum} grades is {average}.");
            }
        }

        static void Main(string[] args)
        {
            YourGrades();
        }
    }

The lines num.Average(); and num.Length; show up as red, and the intelligence says that 'int' does not contain the definition for 'Average' and 'Length'. I've tried different variations of there lines based on other posts on this site, but none work.

CodePudding user response:

You are getting some things mixed up, here's how the code could look to achieve what you want, the comments explain how you were mistaken. Also, I'm assuming you may assume the user will only enter valid values.

internal class Program
{
    public static void YourGrades()
    {
        List<int> Grades = new List<int>();

        Console.WriteLine("Write a grade (0-5), [Enter] gives the results:");

        // First just declare the string, we'll assign the value with Console.Readline in the loop.
        string grades;

        // Set the loop to run indefinitely, we will exit it when presses ENTER on an empty line
        while (true)
        {
            grades = Console.ReadLine();
            if (string.IsNullOrWhiteSpace(grades)) //This checks if the line was empty when the user pressed ENTER, otherwise, it adds it to the list
                break;
            Grades.Add(int.Parse(grades));
        }

        // Average returns a double, that's why you got that error, also, you were trying to run Average() on an int, not on the Grades list
        double average = Grades.Average();

        // Lists don't have a Length value, but they have a Count() method, which also, you were trying to run on an int, not the list
        int sum = Grades.Count();
        Console.WriteLine($"The average of your {sum} grades is {average}.");
        
    }

    static void Main(string[] args)
    {
        YourGrades();
    }
}
  • Related