Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
   string surname;
    int score;
    int numRecords = 0;
    Console.WriteLine("How many students?");


    if (int.TryParse(Console.ReadLine(), out numRecords))
    {
        for (var i = 0; i <= numRecords; i++)
        {

            Console.WriteLine();
            Console.Write("Enter Name:");
            name = Console.ReadLine();
            Console.Write("Enter Surname:");
            surname = Console.ReadLine();
            Console.Write("Enter Score:");
            score = int.Parse(Console.ReadLine());
            Console.WriteLine();
            Console.WriteLine("{0},{1},{2}", name, surname, score);
            Console.WriteLine();
        }
    }


    Console.ReadLine();
}>

The code above enables me to input multiple records, but now l want to be able to view the records and determine which is the top student. That is where l'm stuck. l have to use console app for this as thats what the excercise requires l do.
Posted
Updated 23-Nov-12 18:30pm
v2

Hi,

You need to store maximum mark in one variable. Use an array to store the information. in your for loop store the information in array and then you can use linq query to find maximum out of all the value.

Or you can store maximum value in temporary table and check if the input value by user is lesser then leave it otherwise update temporary variable. and at the end of the program print the temporary variable.

it will work for you.
 
Share this answer
 
Hi Kills,

Just add the below line of code in your code.

C#
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string surname;
            int score;
            int numRecords = 0;
            Console.WriteLine("How many students?");
            if (int.TryParse(Console.ReadLine(), out numRecords))
            {
                List<Student> lstStudent = new List<Student>();
                for (var i = 1; i <= numRecords; i++)
                {
                    Student student = new Student();
                    Console.WriteLine();
                    Console.Write("Enter Name:");
                    student.Name = Console.ReadLine();
                    Console.Write("Enter Surname:");
                    student.SurName = Console.ReadLine();
                    Console.Write("Enter Score:");
                    student.Score = int.Parse(Console.ReadLine());
                    lstStudent.Add(student);
                    Console.WriteLine();                    
                }
                lstStudent = (List<Student>)lstStudent.OrderBy(x => x.Score).ToList(); // It is sorted.
                //Display here all student.

                for (int i = 0; i < lstStudent.Count; i++)
                {
                    Console.WriteLine("Rank#{0}, Name ={1}, SurName ={2}, Score ={3}", (i + 1).ToString(), lstStudent[i].Name, lstStudent[i].SurName, lstStudent[i].Score.ToString());
                }
            }
            Console.ReadLine();
        }
    }

    public class Student
    {
        public string Name { get; set; }
        public string SurName { get; set; }
        public Int32 Score { get; set; }
    }
}
 
Share this answer
 
v3
Comments
ridoy 24-Nov-12 1:00am    
+5
Mohd. Mukhtar 24-Nov-12 1:15am    
Thank you :)
kills 24-Nov-12 1:54am    
Thanks for the response Mohd, but the code above is giving me errors. It says 'the name lstStudent does not exist in its current context' and it also says the namespace student could not be found.
Mohd. Mukhtar 24-Nov-12 2:02am    
See the updated answer.
Mohd. Mukhtar 24-Nov-12 2:03am    
This is full program

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900