0

While loops runs through the if statement once and stops.

I am new to c#, so excuse me if I am overlooking something seemingly obvious. I am currently writing a program that visualizes the Collatz conjecture through console entries. The program begins by prompting the user to enter a natural number. The program is supposed to run that number through the formulae of the conjecture until it eventually reaches a value of 1. However, when I type in the number in console, the program runs it through one formula and crashes. It seems that is has a problem with the Double.Parse line. I already tried using the convert method and tried defining "num" as a decimal instead of a double.

    {
        Console.WriteLine("Enter a natural number:");

        Double num = Convert.ToDouble(Console.ReadLine());

        while (num != 1)
        {
            {
                if (num % 2 == 0)
                {
                    Console.WriteLine(num / 2);
                    num = Double.Parse(Console.ReadLine());
                }
                else
                {
                    Console.WriteLine(num * 3 + 1);
                    num = Double.Parse(Console.ReadLine());
                }
            }


            Console.ReadLine();
        }
    }
}
}
4
  • 1
    What error do you see when program crashes? Did you debut the code? What code gets executed and which line causes the crash? Why do you need num = Double.Parse(Console.ReadLine()); while loop? Commented Jan 8, 2019 at 1:47
  • Is your intent that the call to ReadLine reads in the number you just wrote out? Commented Jan 8, 2019 at 1:51
  • Console.ReadLine() gets input from the user. Your program isn't crashing, it's waiting for you to type something and then press Enter. Most likely, you want to perform an operation on num rather than get more input from the user, like num = num / 2; or num = num * 3 + 1; Commented Jan 8, 2019 at 2:15
  • Thank you all for the help. The problem was the fact that Console.ReadLine() was recording a blank entry after the first run through the while loop. I removed the Console.ReadLine() and replaced it with manic_coder's suggestion. Commented Jan 8, 2019 at 2:24

1 Answer 1

3

To be honest, I'm not positive what you're trying to achieve from the description (i.e., I have not idea what a Collatz conjecture visualization formula is), but I think I figured out what your issue is.

I think you're a little confused about Console.ReadLine(). This method pauses and waits for user input. As a result, during your first loop through the while statement, your program will pause and wait for user input. I think what you're trying to do is take the result of the formula in either the "if" or "else" section and capture that as the new value of "num."

Here is my best guess at what you're trying to achieve:

static void Main(string[] args)
    {
        Console.WriteLine("Enter a natural number:");

        Double num = Convert.ToDouble(Console.ReadLine());

        while (num != 1)
        {
            if (num % 2 == 0)
            {
                num /= 2;
                Console.WriteLine(num);
            }
            else
            {
                num = num * 3 + 1;
                Console.WriteLine(num);
            }
        }

        Console.WriteLine(num);

        Console.ReadLine();
    }

Also, it looks like you might have an extra set of brackets within your while statement. It doesn't seem like that would compile as-is, so perhaps it is just the way in which you copied it into your question.

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

1 Comment

The collatz conjecture is a conjecture in mathematics where, if you take any positive number, and either divide it by two (if it's even) or add one to the proudct of the number and 3, you will eventually get to one. The program is intended to walk the user through each step. I.e. 5, 16, 8, 4, 2, 1

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.