SprJD7903
SprJD7903

Reputation: 105

Need help figuring out what I'm doing wrong in my if statement

I'm still learning C# with Windows Forms and I would like some help with my if statement.

The variables are fixed so there is going to be no user input other than them just clicking one button. Where my issue begins is at the beginning of the if statement.

VS(Visual Studios) constantly says that the half and calc in the condition, and the hours for the conversion to string are all unassigned local variables.

Then in the else statement where the math equation is, VS keeps saying that "hours" cannot be converted from double to hours.

I dont know what that means because I already declared them. Then it says the thirtheenpercent cannot be declared in the scope because it has already been declared. I dont know why it's saying this because this double is not supposed to change.

Can someone help me and explain why I am getting these errors? Any help would be greatly appreciated.

        decimal onecup = 130;
        decimal thirtheenpercent;
        decimal hours=0;
        decimal half=130/2;
        decimal calc=0;

        while (onecup>half)
        {
            thirtheenpercent = (onecup * 13) / 100;
           calc =onecup-thirtheenpercent;
            onecup=calc;
            hours++;
        } mtbHalf.Text = Convert.ToString(hours)+" Hours.";

        while (!(hours==24))
        {
            thirtheenpercent = (onecup * 13) / 100;
            calc = onecup - thirtheenpercent;
            onecup = calc;
            hours++;
        } mtbOnecup24.Text = Convert.ToString(Math.Round(calc,2)) + " mg.";
        //While i was running this program, i ran into some conflictions with my already declared variables for my last while loop so i created some new ones.//
        decimal thirtheenpercent2;
        decimal calc2 = 0;
        decimal onecup2 = 130;
        int hours2 = 0;

        while (!(hours2 == 24))
        {
            thirtheenpercent2 = (onecup2 * 13) / 100;
            calc2 = onecup2 - thirtheenpercent2;
            onecup2 = calc2;
            hours2++;
            onecup2 = onecup2 + 130;
        } mtbQuantity.Text = Convert.ToString(Math.Round(onecup2, 2)) + " mg.";


    }

Upvotes: 0

Views: 80

Answers (1)

Peter Duniho
Peter Duniho

Reputation: 70691

The hours variable is unassigned, in the code you posted. So that error seems correct to me.

The code example is incomplete, but given what you've posted it does not appear that half and calc are in fact unassigned. So the most likely explanation for that is that you are either misinterpreting the error message (i.e. it doesn't say what you say it says), or you are not sharing the correct code (i.e. the code you included isn't the code that's being compiled when the error occurs).

As far as the last errors, your program statement hours * thirtheenpercent = calc is not valid syntax. The compiler will probably try to interpret that as a variable declaration, where hours is the type and the thirtheenpercent variable is being declared as a pointer to that type, initialized with the value held by the variable calc.

That's clearly not what you meant, and of course since hours is not a type, there's no way for the statement to compile successfully.

Possibly you meant calc = hours * thirtheenpercent? It's hard to tell from your question.

You seem to have other algorithmic issues. But without seeing the actual specification for what your code is supposed to do, it's hard to know for sure what the code should look like. Based on the comments, it's doubtful the algorithm would ever work as you seem to want it. You start with 13% of 130, and then (maybe) iteratively work through the multiples of that value. But no multiple of 13% is equal to 50%, so no multiple of (13% of 130) can be equal to (50% of 130).

On top of that, as commenter Ed Gibbs says, dealing with floating point, it's entirely possible that half and calc will never equal each other even the algorithm made sense. You could get extremely close, but not where the two values actually compare as identical.

Upvotes: 1

Related Questions