-1

I have a c# code below. This code takes the name, marks for maths, computer, science, environment and History from the user and prints the total and percentage. The problem with this code is that it takes only the marks from maths. And it prints the output. But it takes the number from maths and assigns that number to all other subjects. Please help me out with this problem. The code goes like this:

using System;

 namespace test
 {
class Program
{
    public static void Main(string[] args)
    {
        int total = 500;
        Console.WriteLine("Hello This is my first C# Program. Hope you like it");
        Console.Write("Enter a name: ");
        String name = Console.ReadLine();
        Console.WriteLine($"Hello {name}");
        Console.Write("Enter the marks for Maths: ");
        int mathsMarks = Console.Read();
        Console.Write("Enter the marks for Science: ");
        int scienceMarks = Console.Read();
        Console.Write("Enter the marks for Computer: ");
        int computerMarks = Console.Read();
        Console.Write("Enter the marks for Environment: ");
        int environmentMarks = Console.Read();
        Console.Write("Enter the marks for Social: ");
        int socialMarks = Console.Read();
        int totalMarks = mathsMarks + scienceMarks + computerMarks + environmentMarks + socialMarks;
        Console.WriteLine($"The total marks is {totalMarks}");
        Console.WriteLine($"The percentage is %{(totalMarks/total)*100}");
    }

}
  }
Fuzzybear
  • 1,388
  • 2
  • 25
  • 42
  • 2
    do you know how to use the `Debugger...?` I would suggest stepping through the code this is not that difficult but this is what the debugger is there for.. also google search on how to use string.Format function – MethodMan Jul 10 '18 at 15:45
  • What exactly do you mean by *"But it takes the number from maths and assigns that number to all other subjects"*? – bassfader Jul 10 '18 at 15:46
  • 1
    https://stackoverflow.com/questions/6825943/difference-between-console-read-and-console-readline Please take a look at the answer by 0xack13 – GSazheniuk Jul 10 '18 at 15:47
  • `Read` doesn´t return the int typed in, but the asci-code of the character you typed. – MakePeaceGreatAgain Jul 10 '18 at 15:49
  • @MethodMan Nah! man I don't know how to use debugger !!!! – Pramish Luitel Jul 10 '18 at 15:51
  • @bassfader first it takes the marks from maths and does not take marks from another subject. But in total it sums up the number 5 times. For example: if we give 20 for the maths, then the total will be 100. And the percentage is always %0. – Pramish Luitel Jul 10 '18 at 15:53
  • @GSazheniuk I looked at this link already and tried but didn't work.. – Pramish Luitel Jul 10 '18 at 15:53
  • @PramishLuitel read how to use the debugger also understand what literals are vs what integer values are.. you are making this much harder than it needs to be.. it's very simple fix if you take the time to use the debugger and step thru the code setting breakpoints.. come on now man..!!~ – MethodMan Jul 10 '18 at 15:55
  • @PramishLuitel The simplest way to debug your code is to display what your variable's values are. I.e. - use Console.WriteLine to output values of mark variables after you just read them. – GSazheniuk Jul 10 '18 at 15:55

2 Answers2

3

Console.Read doesn´t return the number typed in (if any at all), but the ascii-code of the next character you typed. Every character is just a number. Thus typing in the character a would return 97, which is the ASCII-code for a.

From the docdumentation of Read:

Reads the next character from the standard input stream

Thus only a single character is read.

What you want instead is this:

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

or to avoid inout of something like "this is not a number":

int mark;
if(!int.TryParse(Console.ReadLine(), out mark) throw new ArgumentException("no valid number");

Furthermore dividing an integer with an integer will allways return a (truncated) integer as well. E.g. 3 / 7 results in 0.

To avoid this you have to use some floating-point number, e.g. float or double:

double percentage = (double) totalMarks / marks * 100;
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
2

You can see the difference is between how you read the name and how you read the score for the exams. The console.read does not return the int that you expect..it reads 1 char at a time until you press the Enter key.

So change how you read the inputs from the user - maybe use console.ReadLine(). This will return data as a string and then you can explore the use of int.TryParse() to safely convert it to int.

Also your final division will likely always return 0 as the division between integers will ignore the fraction ...so look up double and float types as well.

NoviceProgrammer
  • 3,347
  • 1
  • 22
  • 32