How do l add multiple records in c# console application?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kills
    New Member
    • Nov 2012
    • 3

    How do l add multiple records in c# console application?

    Hi there, l am still beginner at c# and wanted to know how l can add multiple records when using the console app. This is the excercise l have done so far:

    Code:
                string name;
                string surname;
                int score;         
               
                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.ReadLine();
    l want to be able to add more student records and display the student with the top score.
    Last edited by zmbd; Nov 22 '12, 03:05 PM. Reason: [Z{Please use the <CODE/> button to format your postd code/SQL}]
  • madankarmukta
    Contributor
    • Apr 2008
    • 308

    #2
    Specific reason for using console Application ?

    Thank you.

    Comment

    • kills
      New Member
      • Nov 2012
      • 3

      #3
      Its part of an excercise l have to do, which requires l use console application.

      Comment

      • PsychoCoder
        Recognized Expert Contributor
        • Jul 2010
        • 465

        #4
        You would have to do some sort of a looping structure, a for loop, a while loop or something of the sort, something like this (just as an example:

        Code:
        static void Main(string[] args)
        {
            string name;
            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("Enter Name:");
                    name = Console.ReadLine();
                    Console.WriteLine("Enter Surname:");
                    surname = Console.ReadLine();
                    Console.WriteLine("Enter Score:");
                    score = int.Parse(Console.ReadLine());
                    Console.WriteLine();
                    Console.WriteLine("{0},{1},  {2}", name, surname, score);
                }
        
            }
            Console.ReadLine();
        }
        Hope that helps :)

        EDIT: Where you're using int.Parse I would check out using int.TryParse instead, that way your application doesn't crash if something other than a number is entered (look at how I'm using it in the sample code)
        Last edited by PsychoCoder; Nov 22 '12, 05:23 PM. Reason: Add explanation to int.TryParse

        Comment

        • kills
          New Member
          • Nov 2012
          • 3

          #5
          Thanks PsychoCoder. It helped.

          Comment

          Working...