Click here to Skip to main content
15,880,132 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

i have a dout for given program which is developed by console application , the thing is please see my below code in that program i got output that is when i enter 70 it showing you are distinction ,and 50 you are failed.but my question is i just want to display the whole input without entering any value,

i need the given output,

1.you are distinction
2.average
3.failed

so what i change in my code or what i initialize in the given apps, please let me know if any solution about this..

my code is
C#
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int mark;
            Console.WriteLine("enter the mark");
            mark = Convert.ToInt32(Console.ReadLine());
            switch (mark)
            {
                case 70:
                    Console.WriteLine(" you are distinction");
                    Console.ReadLine();
                    break;
                case 60:
                    Console.WriteLine("Average");
                    Console.ReadLine();
                    break;
                case 50:
                    Console.WriteLine("failed");
                    Console.ReadLine();
                    break;
            }
        }
    }
}


with regards,
stellus.
Posted
Updated 8-Jan-13 22:36pm
v2

I'm not totally sure what you are trying to do, but if you are trying to show a message for a range of values,
70+           Distinction
69...60       Average
59 and below  Failed
Then a Switch is not the best way to do it: it only matches exact values, rather than a range. You woudl either have to list each value in the switch:
C#
switch (mark)
   {
   case 100:
   case 99:
   case 98:
   case 97:
...
   case 72:
   case 71:
   case 70:
      Console.WriteLine(" you are distinction");
      Console.ReadLine();
      break;
Which is very clumsy, and difficult to read.
You could use a switch, if you used a division:
C#
switch ((int) mark / 10)
   {
   case 10:
   case 9:
   case 8:
   case 7:
      Console.WriteLine(" you are distinction");
      Console.ReadLine();
      break;
   case 6:
      Console.WriteLine(" Average");
      Console.ReadLine();
      break;
...
But even then it is difficult to maintain - what if necxt year the Average mark is changed to 65?
You would be much better using an if...else construct:
C#
if (mark >= 70) Console.WriteLine(" you are distinction");
else if (mark >= 60) Console.WriteLine(" Average");
else Console.WriteLine(" Failed");
Console.Readline();

It's a lot clearer to read, and you can replace the "magic numbers" with readable constant, or file driven values.
 
Share this answer
 
v2
Comments
ridoy 9-Jan-13 5:52am    
+5
Sorry, but have it to be a switch? If not, here's my shot :)

C#
delegate String GetEvaluationDelegate(int Mark_);

private static List<GetEvaluationDelegate> _Evaluations;

private static List<GetEvaluationDelegate> Evaluations {
    get {

        if (_Evaluations == null) {
            _Evaluations = new List<GetEvaluationDelegate>();

            _Evaluations.Add(delegate(int Mark_) { return (70 <= Mark_ ? "you are distinction" : String.Empty); });
            _Evaluations.Add(delegate(int Mark_) { return (50 <= Mark_ && Mark_ < 70 ? "average" : String.Empty); });
            _Evaluations.Add(delegate(int Mark_) { return "failed"; }); // default

        }

        return _Evaluations; 
    }

}

static void Main(string[] args) {

    var mark = int.Parse(Console.ReadLine());

    var evaluation  = String.Empty;
    var evaluations = Evaluations.GetEnumerator();

    while (evaluation == String.Empty && evaluations.MoveNext()) {
        evaluation = evaluations.Current(mark);
    }

    Console.WriteLine(evaluation);

    Console.ReadLine();

}


... so you could maintain the marks and evaluations in a seperate list.
 
Share this answer
 
v2
Comments
ridoy 9-Jan-13 5:53am    
though not the way OP asked,but a good effort..upvoted..:)
Try
C#
class Program
{
static void Main(string[] args)
{
int mark;
Console.WriteLine(" you are distinction");
Console.WriteLine("Average");
Console.WriteLine("failed");
Console.WriteLine("enter the mark");
mark = Convert.ToInt32(Console.ReadLine());
switch (mark)
{
case 70:
Console.WriteLine(" you are distinction");
Console.ReadLine();
break;
case 60:
Console.WriteLine("Average");
Console.ReadLine();
break;
case 50:
Console.WriteLine("failed");
Console.ReadLine();
break;
}
}
 
Share this answer
 
v2
Comments
OriginalGriff 9-Jan-13 4:31am    
Just a thought: What happens if the mark is 52?
Abhinav S 9-Jan-13 4:46am    
OP just wants the three messages displayed before the case statements set in.
I don't think the OP is interested in the question.
OriginalGriff 9-Jan-13 4:55am    
Dunno who down voted you there, but it's countered.
Um. Judging by the upvoted "answer" I'm wondering if we have the start of a sock puppet upvoting pair.
Abhinav S 9-Jan-13 5:03am    
Thank you. I checked my rep point chart. It was a -4 so it was a lurker.

Not sure what a sock pupper upvoting pair is?
OriginalGriff 9-Jan-13 5:19am    
Where you get a question which is set purely so that it can be answered, and the answer upvoted to increase the rep points.
Normally the OP is a sock puppet for a user who wants a good authority rep but doesn't want to work for it.
In this case the question makes little sense, the answer is not relevant if you follow the link, but it got an upvote anyway so it fits the pattern.
Refer below Link:

Link 1[^]
 
Share this answer
 
v2

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