0

I'm having an issue with a loop where I have to input the temperature twice to start the loop. I think I know where the issue is, I just don't know how to fix it. I have been coding for a total of three weeks, so I am a total beginner at this.

Here's the part of the code I'm having issues with:

{
Console.WriteLine("Enter the temperature in Fahrenheit: ");


            int fahrenheit = int.Parse(Console.ReadLine());
            int celsius = FahrToCels(fahrenheit);

            do
            {
                fahrenheit = int.Parse(Console.ReadLine());
                celsius = FahrToCels(fahrenheit);

                if (celsius < 73)
                {
                    Console.WriteLine(celsius);
                    Console.WriteLine("It's too cold, raise the temperature.");
                }

I think you can see what I mean. The only way I could get the loop to work at all was to repeat the int.Parse(Console.ReadLine(), but perhaps there is another fix that would fix this problem of having to input the temperature twice?

Really hoping someone can help me out with this.

1
  • I've solved the issue, thank you all for helping me out! Commented Sep 19, 2016 at 14:21

3 Answers 3

3

There's more to your code than what you've shared, but adding in what I think you need is moving the readline to another method:

          do
        {
            int farenheight = getTemp();
            celsius = FahrToCels(fahrenheit);

            if (celsius < 73)
            {
                Console.WriteLine(celsius);
                Console.WriteLine("It's too cold, raise the temperature.");
            }
       }
 public int getTemp(){
     return int.Parse(Console.ReadLine());
 }
Sign up to request clarification or add additional context in comments.

2 Comments

Why do you use a function to encapsulate a single line of code that does occur only once in the code snippet? In my opinion, that is just overengineering. However, if the function would do some kind of error handling, i.e. catching any exceptions that arise from parsing non-integer inputs, that might be a whole different story.
That's why I said "there's more to your code than is showing." Yes - as a one-liner, it doesn't make much sense, but I'm guessing that OP has reduced to minimal -- the code posted looks as though it's been chopped.
1

If the problem is that you need to access the variables outside the scope of the loop then you can declare them without assigning them.

{
Console.WriteLine("Enter the temperature in Fahrenheit: ");

        int fahrenheit;
        int celsius;

        do
        {
            fahrenheit = int.Parse(Console.ReadLine());
            celsius = FahrToCels(fahrenheit);

            if (celsius < 73)
            {
                Console.WriteLine(celsius);
                Console.WriteLine("It's too cold, raise the temperature.");
            }

Comments

1

The point here is: Whenever you do a Console.ReadLine(), the program waits for some input from the console. Since you encounter one Console.ReadLine() before the loop and one inside the loop, you need to enter a value twice to "start" the loop.

What's more: The do-while-loop gets executed at least once, because the condition is only checked after a loop iteration. If you change this to a while-loop, where the condition gets checked before every loop iteration, you might get what you expect:

int fahrenheit = int.Parse(Console.ReadLine());
int celsius = FahrToCels(fahrenheit);

while (celsius < 73)
{
    Console.WriteLine(celsius);
    Console.WriteLine("It's too cold, raise the temperature.");

    Console.WriteLine("Enter the temperature in Fahrenheit: ");
    fahrenheit = int.Parse(Console.ReadLine());
    celsius = FahrToCels(fahrenheit);
}

I also like to point out that the code as it is now will throw an exception, if the user enters something that cannot be parsed into an int. This usually causes the program to terminate - unless the exception is caught somehow with a try-catch-block.

2 Comments

Or use int.TryParse instead of a try-catch block. Also it would probably make more sense to declare the temperatures as doubles/floats/decimals since Fahrenheit and Celsius don't convert precisely as integers.
Yep, the try-catch-block is part of the assignment, and I've added that to my code now. Thanks for the help!

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.