Hi there, I'm a bit of a newbie when it comes to C# and only had previous experience with Visual Basic. The problem i'm having revolves around a program I need to create too look at Students marks, the idea is the user inputs the marks and to end the program they press -1.

Now, I have that sorted but the final part requires me to sort grades out by categorising them into A, B, C etc by adding 1 to the grade variable. I have tried If statements but it doesn't seem to work, I can't use "if Marks[count]" because it says it's out of the current context. Here is the code I have so far;

System.Console.WriteLine("\t\t\t\t Student Results Analyser ");
            System.Console.WriteLine("\t\t\t\t ------------------------ ");
            System.Console.WriteLine();

            int[] Marks = new int[100];

            System.Console.Write("Please enter a mark or -1 to finish> ");
            string NumberAsString = System.Console.ReadLine();
            int mark = Convert.ToInt32(NumberAsString);
            int numberOfMarksInput = 0;
            while (mark != -1)
            {
                Marks[numberOfMarksInput] = mark;
                numberOfMarksInput++;
                System.Console.Write("Please enter a mark or -1 to finish> ");
                NumberAsString = System.Console.ReadLine();
                mark = Convert.ToInt32(NumberAsString);
            }

            System.Console.WriteLine();
            System.Console.WriteLine("\t\t\t\t Results ");
            System.Console.WriteLine("\t\t\t\t ------- ");

            
            System.Console.WriteLine();
            System.Console.WriteLine("Total amount of marks entered > {0}", numberOfMarksInput);

            int a = 0;
            int b = 0;
            int c = 0;
            int d = 0;
            int e = 0;
            int f = 0;

Can anyone give me an idea on how I can achieve this? It's completely stumped me.

Thankyou for any help you can give me.

Recommended Answers

All 10 Replies

Think the best way will be to create a void that you send it the mark and it has conditions that will add 1 to the variable while the user input the mark.

public void Grade(int mark)
        {
            if (mark > 80)
            {
                a++
            }
            else if (mark < 79 && mark > 70)
            {
                b++
            }

I used a simple if statement at first which was .

if (mark > 80)
{
a = a + 1
}

That worked just once, so maybe I can loop it?

Thanks for the method provided, I tried to incorporate the public void but it just didn't work. Also when I included a++, I was getting the message again "out of the current context".

Hi mate can you give some more info about exactly what your trying to achieve..

you have uneeded parts of code such as

string NumberAsString = System.Console.ReadLine();
            int mark = Convert.ToInt32(NumberAsString);

NumberAsString is in string format anyway so you can do these two lines in a single line...

int mark = convert.toint32(console.readline());

also declare that you are using system at the top so that you do not have to keep writing out System.console.writeline.

using system;

give some more info and will help you..(sorry for the bad punctuation, my fingers are cold.)

Cheers for the tips, forgot about using system;

So for the end of the program I want to add numbers to the variables depending on what mark has been input. So for example if 80 is entered it will be logged as "+1" to the "a" variable. I was expecting every mark that was input to trigger a +1 to any of the variables. Then when the user presses -1 a table shows with how many A's, B's, C's...etc

This is basically what I have for the end of the program.

System.Console.WriteLine();
                System.Console.WriteLine("Number of A's > {0}", a);
                System.Console.WriteLine("Number of B's > {0}", b);
                System.Console.WriteLine("Number of C's > {0}", c);
                System.Console.WriteLine("Number of D's > {0}", d);
                System.Console.WriteLine("Number of E's > {0}", e);
                System.Console.WriteLine("Number of F's > {0}", f);

Call the Grade function as proposed by Singlem in your while loop.
Declare it as public static void Grade(int mark) etc.
Succes!

Ok well in that case i would probably do something like this..use if statements...

int numberagrade = 0;
int numberbgrade = 0; //so on so on for each grade ..(a,b,c,d,e,f)


console.writeline("enter grade:");
int gradentered = convert.toint32(console.readline()); //gradeentered holds number of grade entered by user.

//now using if statements you can use the ++ operator that will increase number of a variable by one.

if (gradeentered < 0)
{
console.writeline("you have not entered a valid grade");
}
else if (gradentered >= 0 <= 20)
{
numberagrade++;//this will cause numberagrade to increase from 0 to 1
}
else if (gradeentered >=21 <=35)
{
numberbgrade++;//increase bgrade to be increased from 0 to 1
}

so on so on...

once you have done this you can then refer to those variables from you code above...

System.Console.WriteLine();
                System.Console.WriteLine("Number of A's > {0}", a);//change a to numberagrade
                System.Console.WriteLine("Number of B's > {0}", b);//change b to numberbgrade
                System.Console.WriteLine("Number of C's > {0}", c);
                System.Console.WriteLine("Number of D's > {0}", d);
                System.Console.WriteLine("Number of E's > {0}", e);
                System.Console.WriteLine("Number of F's > {0}", f);

does that make sense? let me know

it would be better for you to use functions and properties to set the variables but it doesnt look as though you have got as far as using oop techniques?

Right, james I tried it but it was only saving 1 mark and counting the last mark that was entered.

I did however find a solution to it, I thought long and hard about looping the if statement and come up with this.

for (int i = 0; i < numberOfMarksInput; i++)
            {
                if (Marks[i] > 80)
                {
                    a = a + 1;
                }
                else if (Marks[i] > 70 && Marks[i] <= 79)
                {
                    b = b + 1;
                }
                else if (Marks[i] > 60 && Marks[i] <= 69)
                {
                    c = c + 1;
                }
                else if (Marks[i] > 50 && Marks[i] <= 59)
                {
                    d = d + 1;
                }
                else if (Marks[i] > 40 && Marks[i] <= 49)
                {
                    e = e + 1;
                }
                else if (Marks[i] < 40)
                {
                }
}

Thankyou to all that helped, I will stick around on this forum as I am hoping to become a programmer after I graduate (I'm in my first year), and there are some very talented people here. I'll try to contribute whenever I can.

Once again thankyou.

ok mate cool...im in first year at uni too...so just learning..which uni are you at if you dont mind me asking?

I'm studying Computer Science at University of Birmingham. What about you?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.