2

While writing my code, I received this error for a,b and c.

           while(n==0)
        {
            a = Convert.ToInt32(Console.ReadLine());
            b = Convert.ToInt32(Console.ReadLine());
            c = Convert.ToInt32(Console.ReadLine());
            n=CheckTriang(a, b, c, n);

        }

        x=RightTriang(a, b, c, x);

I input a, b and c's values in the loop, then check them in the working method CheckTriang (determining if the values can make a valid triangle), and break the condition, returning n=1. I think that the problem might be with the compiler not being sure if the loop will even stop to give the values. How can I rewrite the code to be clearer?

Thanks in advance!

Edit: I only copied the problematic part, not the whole code. The needed variables are declared.

3
  • 2
    can we see the part where you define and initialize a,b,c and n? Commented Nov 15, 2013 at 21:28
  • Can you please post a working example so that we can test your code? Commented Nov 15, 2013 at 21:29
  • 1
    Where are you assigning a value to x? Your very last line tries to use a value named x but as far as we can tell it has no value. Set some break points and step through your code line by line. Verify everything has a value. Commented Nov 15, 2013 at 21:34

4 Answers 4

6

Switch to a do-while loop:

do {
        a = Convert.ToInt32(Console.ReadLine());
        b = Convert.ToInt32(Console.ReadLine());
        c = Convert.ToInt32(Console.ReadLine());
        n=CheckTriang(a, b, c, n);
    } while (n == 0)

This guarantees that you'll get through the loop at least once.

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

Comments

3

The problem is that if n==0 on the first run a,b,c,& n would never be initialized. The compiler is catching this and warning you as such. Simply add

int a=0, b=0, c=0, n=0;

somewhere above your while loop

Comments

1

Make sure, when you declare n, that you initialize it:

int n = 0; // Add the = 0

3 Comments

Structs are initialized with their default values though. So int n = 0; is the same as int n;
The error is for a, b and c, everything else is working fine. I only copied a specific part. Thanks anyway.
@therealtbs While its true the value will be zero, and you could dynamically access it at run time, you'll have compile problems if you try compiling something like int n; bool x = n == 0;
1

You need to declare your variables by indicating their type, and also initialize n before you use it.

int n = 0;
while(n == 0)
{
    int a = Convert.ToInt32(Console.ReadLine());
    int b = Convert.ToInt32(Console.ReadLine());
    int c = Convert.ToInt32(Console.ReadLine());
    n = CheckTriang(a, b, c, n);
}

//replace 'var' by whichever type 'RightTriang' returns.
var x = RightTriang(a, b, c, x);

Comments

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.