Jason Smith
Jason Smith

Reputation: 11

`if` statement issues

I've made a small speed citation form for my class that calculates predetermined fields and adds them to the users speed.

For example, if the user selects visitor the minimum citation is #25 dollars if the infraction happened at night their is an additional $25 dollar penalty. If student is selected the citation is #35 + additional $2 per every mile over, unless Sophomore is selected than it becomes $5.

Unfortunately, IF statements are my bane and I've made a mistake with my IF statements because my math isn't coming out correctly. If I select a student + night + 25 in a 20 that total should equal 70 and I am getting 10.

GUI Image: http://postimg.org/image/doy6n0sp3/

Edit: I ended up re-writing the code to resolve the issues I had

Upvotes: 0

Views: 116

Answers (4)

Justin R
Justin R

Reputation: 198

I would also suggest nesting your IF statements if you wish to charge ONLY the students $2 or $5 respectively.

        if (VisitorRB.Checked)
        {
            ticketPrice = visitorFine ;
        }
        else if (StudentRB.Checked)
        {
            ticketPrice = studentFine;
            if (SophCB.Checked)
            {
                ticketPrice = ticketPrice + sophomoreOver;
            }
            else
            {
                ticketPrice = ticketPrice + freshmenOver;
            }
        }

Upvotes: 0

thewisegod
thewisegod

Reputation: 1542

Why should this:

student + night + 25 in a 20 = 70?

If student = 45 and night = 25 and you add another 25 to that?

Now if you are only saying that you are looking for student + night, then you are correct, but here is a piece of code that is running every time and maybe causing your problem:

if (SophCB.Checked)
{
    ticketPrice = ticketPrice + sophomoreOver;
}
else
{
    ticketPrice = ticketPrice + freshmenOver; // this is running if you don't select SophCB every time.
}

basically ticketPrice will either be:

ticketPrice = ticketPrice + sophomoreOver;

or

ticketPrice = ticketPrice + freshmenOver;

Upvotes: 1

Manu
Manu

Reputation: 1485

One error I see in your code is missing braces. The * will be executed before the -.

Replace

sophomoreOver = double.Parse(SpeedtextBox.Text) - speedLimit * 5;
freshmenOver = double.Parse(SpeedtextBox.Text) - speedLimit * 2;

by

sophomoreOver = ( double.Parse(SpeedtextBox.Text) - speedLimit ) * 5;
freshmenOver = ( double.Parse(SpeedtextBox.Text) - speedLimit ) * 2;

Upvotes: 0

Arghya C
Arghya C

Reputation: 10068

I feel you mean this

sophomoreOver = (double.Parse(SpeedtextBox.Text) - speedLimit) * 5;
freshmenOver = (double.Parse(SpeedtextBox.Text) - speedLimit) * 2;

Upvotes: 2

Related Questions