Chandima Gayan
Chandima Gayan

Reputation: 41

My C# grading program keep outputting the same result

I just started C# and was making this very simple C# program to print a grade according to the inputed marks.

Here's my program:

    class data
{

    int marks;
    public void input()
    {
        Console.WriteLine("Enter your marks");
        marks = Convert.ToInt16(Console.ReadLine());



    }

    public void output()
    {
        if (marks >= 75)
        {
            Console.WriteLine("Merit");
        }
        else if (marks < 75)
        {
            Console.WriteLine("Distiction");
        }
        else if (marks < 65)
        {
            Console.WriteLine("Credit");
        }
        else if (marks < 55)
        {
            Console.WriteLine("Pass");
        }
        else Console.WriteLine("Fail");


    }
}
}

And here's the main:

 class Program
{
    static void Main(string[] args)
    {
        data obj1 = new data();
        obj1.input();
        obj1.output();
        Console.ReadLine();

    }
}
}

Problem is it keeps outputting "Distinction" as the grade no matter what input I give. What am I doing wrong?

Edit - Fail marks is below 40

Upvotes: 1

Views: 1988

Answers (5)

Bill Roberts
Bill Roberts

Reputation: 1171

I'm also having a difficult time trying to resolve your logic - I'll take a hit in negative votes just to get a better feel for your intent.

But I think it's good for you to see that if asked to check if a number sits "between two numbers", you can construct an IF to check only for the lower boundary.

Edit - Fail Mark below 40

  if (marks >= 75) Console.WriteLine("Merit");
  else if (marks >= 65) Console.WriteLine("Distinction");
  else if (marks >= 55) Console.WriteLine("Credit");
  else if (marks >= 40) Console.WriteLine("Pass");
  else Console.WriteLine("Fail");

Upvotes: 0

Shahjahan
Shahjahan

Reputation: 552

For all marks entry below 75 it makes 2nd condition true, that's why you get distinction every time. Change your output function to this

    public void output()
    {
        if (marks >= 75)
        {
            Console.WriteLine("Merit");
        }
        else if (marks >= 65 && marks < 75)
        {
            Console.WriteLine("Distiction");
        }
        else if (marks >= 55 && marks < 65)
        {
            Console.WriteLine("Credit");
        }
        else if (marks >= 40 && marks < 55) // Here 40 is passing marks. You've set up of your own
        {
            Console.WriteLine("Pass");
        }
        else Console.WriteLine("Fail");
    }

Upvotes: 2

Vladimir Arustamian
Vladimir Arustamian

Reputation: 842

I guess this is what you have expected:

public void output()
{
    if (marks >= 75)
    {
        Console.WriteLine("Merit");
    }
    else if (marks < 55)
    {
        Console.WriteLine("Pass");
    }
    else if (marks < 65)
    {
        Console.WriteLine("Credit");
    }
    else if (marks < 75)
    {
        Console.WriteLine("Distiction");
    }
    else Console.WriteLine("Fail");
}

P.S. Please begin classes and methods names with an uppercase.

Upvotes: 0

PhoenixFireFlite
PhoenixFireFlite

Reputation: 134

Your problem is that your second if statement, (marks < 75) includes all the statements below it. For example, if marks < 75 is correct, then that statement will run, and no other statements will be checked like marks < 65 or marks < 55 because marks < 75 includes them. You need to do:

if (marks >= 75){
    Console.WriteLine("Merit");
}else if (marks < 55){
    Console.WriteLine("Pass");
}else if (marks < 65){
    Console.WriteLine("Credit");
}else if (marks < 75){
    Console.WriteLine("Distiction");
}else 
    Console.WriteLine("Fail");

Upvotes: 0

David
David

Reputation: 10708

You have failed to use proper ranges; since the marks < 75 block comes before marks < 65 block, and for all cases where marks <65 (and so on) are also true for marks < 75, your code will always stop at marks < 75 and thus output Distinction.

Not seeing an obvious reason it won't output Merit for properly high values, however

Upvotes: 5

Related Questions