1

Just started learning C#, my question is how do i keep record of the user input in order like this: score 1:

score 1: 98
score 2: 76
score 3: 65
score 4: 78
score 5: 56

In my code i can input the number but cant seem to setup the order how can i achieve this goal my input:

98
76
65
78
56

code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyGrade03
{
    public class Program
    {
        private int total;  // sum of grades
        private int gradeCounter; //number of grades entered
        private int aCount; // Count of A grades
        private int bCount; // Count of B grades
        private int cCount; // Count of C grades
        private int dCount; // Count of D grades
        private int fCount; // Count of F grades
        private string v;



        public string CourseName { get; set; }

        public Program(string name)
        {
            CourseName = name;
        }

        public void DisplayMessage()
        {
            Console.WriteLine("Welcome to the grade book for \n{0}!\n",
                CourseName);
        }

        public void InputGrade()
        {
            int grade;
            string input;

            Console.WriteLine("{0}\n{1}",
                "Enter the integer grades in the range 0-100",
                "Type <Ctrl> z and press Enter to terminate input:");

            input = Console.ReadLine(); //read user input

            while (input != null)
            {
                grade = Convert.ToInt32(input); //read grade off user input
                total += grade;// add grade to total
                gradeCounter++; //  increment number of grades

                IncrementLetterGradeCounter(grade);

                input = Console.ReadLine();
            }
        }
        private void IncrementLetterGradeCounter(int grade)

        {
            switch (grade / 10)
            {
                case 9: //grade was in the 90s
                case 10:
                    ++aCount;
                    break;
                case 8:
                    ++bCount;
                    break;
                case7:
                    ++cCount;
                case6:
                    ++dCount;
                    break;
                default:
                    ++fCount;
                    break;

            }
        }
        public void DisplayGradeReport()
        {
            Console.WriteLine("\nGrade Report");

            if (gradeCounter != 0)
            {
                double average = (double)total / gradeCounter;

                Console.WriteLine("Total of the {0} grades entered is {1}",
                    gradeCounter, total);
                Console.WriteLine("class average is {0:F}", average);
                Console.WriteLine("{0}A: {1}\nB: {2}\nC: {3}\nD: {4}\nF: {5} ",
                    "Number of students who received each grade: \n",
                   aCount,
                   bCount,
                   cCount,
                   dCount,
                   fCount);
            }
            else
                Console.WriteLine("No grades were entered");
        }
        static void Main(string[] args)
        {
            Program mygradebook = new Program(
                "CS101 introduction to C3 programming");
            mygradebook.DisplayMessage();
            mygradebook.InputGrade();
            mygradebook.DisplayGradeReport();
        }
    }
}
5
  • You're not clear on what you want. Do a little research and effort especially on this school assignment. Commented Mar 7, 2017 at 9:35
  • is there anything you want to do with the ordinal of the input? is it just an index or will it be used for anything? Commented Mar 7, 2017 at 9:36
  • @dave i just want to know how do i get the score 1: numerical for each input Commented Mar 7, 2017 at 9:37
  • let me uinderstand: if i enter the first score as 98, and the 2nd score as 76, instead of just having 98,76 you want 1:98, 2:76? so later you can interrogates "what was score number 2"? i think you can only do this either by storing the data somewhere or using a list. Commented Mar 7, 2017 at 9:40
  • @jmike take a look on the MSDN link posted in the answer by Chris. List<T> has a couple of nice methods that you try to program yourself like Sum(), Average() and Count(). Commented Mar 7, 2017 at 9:45

3 Answers 3

1

There are a lot of data structures that will allow you to store data in order. I'd personally recommend a List<int> for this.

You can add things to it as simply as:

var list = new List<int>();
list.Add(37);
list.Add(95);

And you can either read it with an iterator (foreach(var score in list){...}) or get individual numbers out (var firstScore = list[0]). The documentation will tell you more about what you can do with a List<T>.

Sign up to request clarification or add additional context in comments.

Comments

1

declare one variable to count inputs like private static int counter = 0;
in InputGrade method, put like below

Console.WriteLine("{0}\n{1}",
                "Enter the integer grades in the range 0-100",
                "Type <Ctrl> z and press Enter to terminate input:");
counter++;
System.Console.Write("score " + counter + ":");
input =  Console.ReadLine(); //read user input

and inside while (input != null) put like below

IncrementLetterGradeCounter(grade);
counter++;
System.Console.Write("score " + counter + ":");
input = Console.ReadLine();

so, output will be like enter image description here

Here is the full code

    public class Program
    {
        private int total;  // sum of grades
        private int gradeCounter; //number of grades entered
        private int aCount; // Count of A grades
        private int bCount; // Count of B grades
        private int cCount; // Count of C grades
        private int dCount; // Count of D grades
        private int fCount; // Count of F grades
        private string v;
        private static int counter = 0;


        public string CourseName { get; set; }

        public Program(string name)
        {
            CourseName = name;
        }

        public void DisplayMessage()
        {
            Console.WriteLine("Welcome to the grade book for \n{0}!\n",
                CourseName);
        }

        public void InputGrade()
        {
            int grade;
            string input;

            Console.WriteLine("{0}\n{1}",
                "Enter the integer grades in the range 0-100",
                "Type <Ctrl> z and press Enter to terminate input:");
            counter++;
            System.Console.Write("score " + counter + ":");
            input =  Console.ReadLine(); //read user input

            while (input != null)
            {
                grade = Convert.ToInt32(input); //read grade off user input
                total += grade;// add grade to total
                gradeCounter++; //  increment number of grades

                IncrementLetterGradeCounter(grade);
                counter++;
                System.Console.Write("score " + counter + ":");
                input = Console.ReadLine();
            }
        }
        private void IncrementLetterGradeCounter(int grade)
        {
            switch (grade / 10)
            {
                case 9: //grade was in the 90s
                case 10:
                    ++aCount;
                    break;
                case 8:
                    ++bCount;
                    break;
                case7:
                    ++cCount;
                case6:
                    ++dCount;
                    break;
                default:
                    ++fCount;
                    break;

            }
        }
        public void DisplayGradeReport()
        {
            Console.WriteLine("\nGrade Report");

            if (gradeCounter != 0)
            {
                double average = (double)total / gradeCounter;

                Console.WriteLine("Total of the {0} grades entered is {1}",
                    gradeCounter, total);
                Console.WriteLine("class average is {0:F}", average);
                Console.WriteLine("{0}A: {1}\nB: {2}\nC: {3}\nD: {4}\nF: {5} ",
                    "Number of students who received each grade: \n",
                   aCount,
                   bCount,
                   cCount,
                   dCount,
                   fCount);
            }
            else
                Console.WriteLine("No grades were entered");
        }
        static void Main(string[] args)
        {
            Program mygradebook = new Program(
                "CS101 introduction to C3 programming");
            mygradebook.DisplayMessage();
            mygradebook.InputGrade();
            mygradebook.DisplayGradeReport();
            Console.ReadKey();
        }
    }

Comments

0

You can look for Collections available in C# (MSDN Collections).

In your case, you don't really care about order, you can use List<int>. Otherwise if you want to keep an order you can use Stack<int> or Queue<int>. And if you want to keep a collection of Student Name + Score you can use a Dictionary<string,int>

1 Comment

List<int> will keep things in order as well. The difference between them and stacks/queues is that you can access any items in a list but stacks and queues only give back the contents in a very specific order. They all store the contents in the order you have defined it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.