Click here to Skip to main content
15,918,178 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i'm doing this program to make the user enter 10 student names along with their student number and prelim grade, i made a looping statement to let the output display 10 of them. the problem is i don't know how to display which of them has the highest grade on the output.

here is the problem:

Create a C# program that will ask the user to enter the name of 10 students with their student no. and Prelim Grade. Then determine which of the 10 students has the highest Prelim Grade. Display only the name, student number and Prelim Grade of the highest student among the 10.

Use any of the looping statements: For, While, Do-While

What I have tried:

i tried doing with this code but i'm getting confused on how i should compare them to each other and to see which of them has the highest grade

here is the code

C#
int count = 0;
do
{
    Console.WriteLine("\nEnter student name: ");
    string name = (Console.ReadLine());
    Console.WriteLine("Enter student No: ");
    double number = double.Parse(Console.ReadLine());
    Console.WriteLine("Enter Prelim Grade: ");
    double grade = double.Parse(Console.ReadLine());
    count++;

} while (count < 10)
Posted
Updated 16-Oct-21 23:05pm
v2

You can't do it with that code: you need to store the inputs as they are entered so they can be accessed after the input is done. None of your inputs are available because they all go out of scope when the loop ends.

There are two ways to do this: I would create a Student class and write a method that collected the user input and returned a Student instance, then store them all in a collection such as a List - then getting the student with the highest grade is easy:
C#
Student top = myStudents.Max(s => s.Grade);
. But it's also possible without a class if you keep an existing max and check within your loop:
C#
Student top = new Student(null, 0.0, double.MinValue);
for (Int i = 0; i < 10; i++)
   {
   Student current = GetStudent();
   if (current.Grade >= top.Grade)
      {
      top = current;
      }
   }
You can do this without a Student class, but it's more work and a lot messier.

By the way, you shouldn't use double.Parse with user inputs: users make mistakes, and your app will crash if they mistype. Getting to the tenth student and teh app crashing because you miskeyed is going to annoy users!
Instead, use double.TryParse[^] instead and report problems and let then re-do the input:
C#
double grade;
while (true)
    {
    string inp = Console.ReadLine();
    if (double.TryParse(inp, out grade))
        {
        break;
        }
    Console.WriteLine($"\"{inp}\" is not a number!");
    }
 
Share this answer
 
I would encourage you to study the solutions OriginalGriff shows you; in real-world code you are probably not going to "throw away" the data the user has entered. But, here's an example that does not maintain an internal structure:
string highsname = "";
double highsid = 0;
double highsgrade = 0;

int count = 0;
do
{
    Console.WriteLine("\nEnter student name: ");
    string name = (Console.ReadLine());
    Console.WriteLine("Enter student No: ");
    double number = double.Parse(Console.ReadLine());
    Console.WriteLine("Enter Prelim Grade: ");
    double grade = double.Parse(Console.ReadLine());

    if(grade > highgrade)
    {
        highsname = "";
        highsid = number;
        highsgrade = grade;
    }

    count++;

} while (count < 10)
Consider what happens if 'highsgrade is #0 when the loop exits; does that mean:

1) all students had the same #0 grade

2) one student had #0 grade and all the others had negative grades

What happens if more than one student has the same highest grade, and you want to show that after the loop exits ? Returning more than one will require your creating some kind of structure.
 
Share this answer
 

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