2

I'm trying to create a simple program to calculate an average. The user should enter a positive number, then I create a loop to sum from 0 to the number entered. Then the average is the total divided by the number entered.

Problem: When I enter a number, for example, 10, the variable becomes 58. To any value that I enter, it always adds 48. Anyone have a clue about this issue?

Here is the code:

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

namespace inClass1
{
    class Loops
    {
        static void Main(string[] args)
        {
            int total = 0;
            int num;
            Console.Write("Enter a positive number: ");

            num = Convert.ToInt32(Console.Read());

            for (int i = 0; i <= num; i++)
            {
                total = total + i;
            }

            double average = total / (double)num;
            Console.WriteLine("The average is = " + average);
            Console.ReadLine();
        }
    }
}
3
  • 5
    A good opportunity to learn about ASCII codes, something every programmer should know. Commented Jan 22, 2015 at 0:46
  • 1
    This would make a great interview question... Commented Jan 22, 2015 at 1:13
  • @HansPassant I respectfully disagree. I don't think many programmers need to know about ASCII. But almost all need to know about Unicode. Commented Jan 22, 2015 at 3:33

2 Answers 2

5

It's because Console.Read method reads the first char and returns its ASCII value. And it only reads one char, so you can't read more than one number at the same time. To fix that just use Console.ReadLine to take a string as input:

 um = Convert.ToInt32(Console.ReadLine());

In case where the user inputs an invalid number this will fail. To prevent that you can look into int.TryParse method.

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

1 Comment

Thank you @Selman22, it worked! I'll take a look on the int.Parse method!
1

The problem is that you are using Console.Read.

That method returns a int, not a string. That int is the Unicode character code of the read character. The overload of Convert.ToInt32 that takes a int looks like this:

public static int ToInt32(int value) {
        return value;
    }

(Reference Source)

Which means that its just returning the passed value (instead of parsing). Unicode '1' is the number 49.

Instead, use Console.ReadLine which in addition to getting the whole input (not just the first character) will return a string, so that int.Parse is called on it instead of casting when you use Convert.ToInt32.

3 Comments

@Selman22 Wow, thats what I get for not carefully reading the docs. Fixed.
Not ASCII; Unicode (after being translated per Console.InputEncoding).
@TomBlodget Interesting, they are the same in this case. Thanks for the clarification

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.