Variable not designated

0

Give me an error saying that the remainder variable was not assigned. There are several parts of the code that I do not understand and would like explanation.

  
  • Write a program that continually reads in integer values until a four digit number in the range 111 to 9999 is entered. Display the number vertically i.e. each digit on a separate line.
  •   

    Example:
      Input: 1234
      Output:
      1
      2
      3   4

        static void Main(string[] args)
        {
            int number, remainder;
            string s = "";
    
            //Reading a 4-digit number between 1111 and 9999
            do
            {
                Console.WriteLine("Enter a number between 1111 and 9999: ");
                number = int.Parse(Console.ReadLine());
            } while (number < 1111 || number > 9999);/*Por que tenho que pôr esta condição*/
    
            //Breaking number and formatting it vertically in a string
    
            while (number > 0)
                remainder = number % 10;
                s = remainder + "\n" + s;
                number = number / 10;
    

    // I did not notice anything of the 4 lines above, what do they do?

            //Displaying number vertically
            Console.WriteLine(s); // porquê visualizar a variável s?
            Console.ReadLine();
    
        
    asked by anonymous 14.07.2017 / 15:17

    1 answer

    1

    The code is very confusing, I could not reproduce the problem of the title, I think even strange. In fact this variable does not even need to exist.

    I'll leave out the possible mistakes that can occur in circumstances that are out of the way, I said simply. If you'd like to check out TryParse() .

    try to declare the variables closest to where you're going to use them. This gives more readability. Also use names that identify what the variable is.

    The condition in the first loop is doing what the exercise asks for, it asks for a new number until it is entered a number in the range that is allowed. If the number is outside of it, repeat the request.

    You have to print the text variable, which you called s in the original because the exercise has to print, it has to be displayed on the screen.

    using static System.Console;
    
    public class Program {
        public static void Main() {
            int number;
            do {
                WriteLine("Enter a number between 1111 and 9999: ");
                number = int.Parse(ReadLine()); //isto dá erro se digitar algo errado, deveria usar TryParse()
            } while (number < 1111 || number > 9999);
            var texto = "";
            while (number > 0) {
                texto = number % 10 + "\n" + texto; //o uso de \n pode não ser o ideal sempre
                number /= 10;
            }
            WriteLine(texto);
        }
    }
    

    See running on .NET Fiddle . And at Coding Ground . Also put it in GitHub for future reference .

        
    14.07.2017 / 15:38