So you already got a string from Console.ReadLine(). So why go through that crazy conversion from a string to an int, and then back to a string again?
Also, unless you are planning on changing the characters of a string, there is no need to make the string into an char array. This should cover what you need:
C#:
Console.WriteLine("Enter number = ");
string input = Console.ReadLine();
if (!int.TryParse(input, out int value))
Console.Error.WriteLine($"{input} is not a number.");
// Look at each character of the input:
for(int i = 0; i < input.Length; i++)
{
char ch = input[i];
Console.WriteLine($"input[{i}] is '{ch}'");
}
I'm still not seeing why you would need negative indexes.
Please explain where you are attempting this, why you are attempting this, and how you are attempting this, and provide the code you are using and put that code in the provided code tags block : [CODE=csharp] Your code here [/CODE].
Okay I am new to C# and programming as a whole , I attempting to solve this question where I am demanded to access the last digit of a user input .
For example 56772 to and I need to get 2 from the numbers and store it in a variable.
I think I must share my code ;
Determining the last value of a user input:
int num ;
Console.Writeline("Enter number = ");
num = Convert.ToInt32(Console.ReadLine());
// My intention is to convert the int input first to string before and array
//Don't know if I am right.:)
num = num.ToString();
num = To.Array();
// I'm stuck
So you already got a string from Console.ReadLine(). So why go through that crazy conversion from a string to an int, and then back to a string again?
Also, unless you are planning on changing the characters of a string, there is no need to make the string into an char array. This should cover what you need:
C#:
Console.WriteLine("Enter number = ");
string input = Console.ReadLine();
if (!int.TryParse(input, out int value))
Console.Error.WriteLine($"{input} is not a number.");
// Look at each character of the input:
for(int i = 0; i < input.Length; i++)
{
char ch = input[i];
Console.WriteLine($"input[{i}] is '{ch}'");
}
I'm still not seeing why you would need negative indexes.
As a quick aside, if you know that the input is an integer, you could just divide by 10 and get the remainder using the modulo operator to get the last digit.
So you already got a string from Console.ReadLine(). So why go through that crazy conversion from a string to an int, and then back to a string again?
Also, unless you are planning on changing the characters of a string, there is no need to make the string into an char array. This should cover what you need:
C#:
Console.WriteLine("Enter number = ");
string input = Console.ReadLine();
if (!int.TryParse(input, out int value))
Console.Error.WriteLine($"{input} is not a number.");
// Look at each character of the input:
for(int i = 0; i < input.Length; i++)
{
char ch = input[i];
Console.WriteLine($"input[{i}] is '{ch}'");
}
I'm still not seeing why you would need negative indexes.
As a quick aside, if you know that the input is an integer, you could just divide by 10 and get the remainder using the modulo operator to get the last digit.
Be warned that it's only available on newer version of the language (C# 8 or higher). If you are using the .NET Framework 4.8 or lower, or .NET Core 2.1 or lower, or .NET Standard 2.0 or lower, you'll be using C# 7.x. No handy indexing using ^value readily available there.
An an aside, and in our OP's defence, I must admit that I have often done this myself under extreme tiredness. You of all people know how hard I work, and the hours I work for what I'm building. My last shift was three days straight including my own personal/client projects. It's a very simple mistake to make when under a heavy workload or great pressure. My point is little mistakes happen to the best of us!
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.