3

I am learning C# and having an issue understanding why my highNumber value is being displayed as 56 when running my code snippet.

//Write a program and ask the user to enter a series of numbers separated by comma. Find the maximum of the numbers //and display it on the console. For example, if the user enters “5, 3, 8, 1, 4", the program should display 8

int highNumber = 0;

Console.Write("Enter a series of numbers separated by comma: ");
var userInput = Console.ReadLine();

for (var i = 0; i < userInput.Length; i++)
{
    if (char.IsNumber(userInput[i]) == true)
    {
        if (userInput[i] > highNumber)
        {
            highNumber =  Convert.ToInt32(userInput[i]);
        }
    }
}

Console.WriteLine("The largest number is {0}", Convert.ToInt32(highNumber));
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
TJ2
  • 33
  • 3

3 Answers3

3

You've entered the ASCII character '8', which has the numeric value 56.

If it's a single digit character then convert it with this

int val = (int)Char.GetNumericValue(userInput[i]);

If it's a numeric string then convert it in one call with this

int val;
bool success = Int32.TryParse(userInput, out val);

or

int val = System.Convert.ToInt32(userInput);
LordWilmore
  • 2,829
  • 2
  • 25
  • 30
2

The LordWilmore`s answer is somehow correct if you dont use numbers greater than 10 but you can simplify and make it more accurate as below:

int highNumber = 0;
int valueTemp;
Console.Write("Enter a series of numbers separated by comma: ");
var userInput = Console.ReadLine();
string[] array = userInput.Split(',');

for (var i = 0; i < array.Length; i++)
{
    if (int.TryParse(array[i], out valueTemp))
    {
        if (valueTemp > highNumber)
        {
            highNumber =  valueTemp;
        }
    }
}

Console.WriteLine("The largest number is {0}", highNumber);

or using Max() function if you have knowledge about the List<> in c#:

int valueTemp;
Console.Write("Enter a series of numbers separated by comma: ");
var userInput = Console.ReadLine();
string[] array = userInput.Split(',');
List<int> intList = new List<int>();

for (var i = 0; i < array.Length; i++)
{
    if (int.TryParse(array[i], out valueTemp))
    {
        intList.Add(valueTemp);
    }
}
Console.WriteLine("The largest number is {0}", intList.Max());

This is a more accurate example because if put 3,33,4 in your example it will fail since you get each character and not values separated by comma. In my case I split them into pieces accordingly and then continue to test them appropriately.

Rey
  • 3,663
  • 3
  • 32
  • 55
  • Thank you, this worked great. Looks like turning it into an array was what I was missing. – TJ2 Dec 20 '17 at 16:07
1

Console.ReadLine(); returns a string. When you use the indexer ([i]) on the string you get individual characters, not strings or numbers. What you want is to Split the string by the commas, then use TryParse to ensure that each string is a number:

string userInput = Console.ReadLine();
string[] parts = userInput.Split(',');
for (var i = 0; i < parts.Length; i++)
{
    int number;
    if (int.TryParse(parts[i], out number))
    {
        if (number > highNumber)
        {
            highNumber = number;
        }
    }
}
D Stanley
  • 149,601
  • 11
  • 178
  • 240