Unassigned variable

Asked

Viewed 65 times

1

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

  1. 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 didn’t get anything from the 4 lines above, what they do?

        //Displaying number vertically
        Console.WriteLine(s); // porquê visualizar a variável s?
        Console.ReadLine();
  • This code is very confusing and missing a part. Which line gives the error? The rest seems to be just a matter of text interpretation.

  • It is mandatory to use this while (number > 0)? It would be better to use the foreach.

  • @Francis how to use a foreach if not even a collection exists?

  • If I understand the question the right way, convert the number for string and makes foreach (char in string). It would work @bigown?

  • It is possible, but it seems to me not what he wants, he wants to do mathematically.

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

Show 1 more comment

1 answer

2

The code is very confusing, I could not reproduce the problem of the title, I find it strange. Actually this variable does not even need to exist.

I will let go of the possible mistakes that can occur in circumstances outside of what is expected, I commented in a simple way. If you want to see about the TryParse().

find declare the variables closest to where you will use them. This gives more readability. Also use names that identify what is the variable.

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

He had the text variable printed, which he called s in the original because the exercise has to print, you have to show 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);
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.