if/else/else if statements

closed account (98qGz8AR)
hi everybody,
new to the forums, so if this isn't the place to be asking this sort of question i will gladly delete this. i'm in computer science 1 in school, and i'm hung up on this assignment. it's something to do with the if/else statements, but i've tried many combinations of types of statement, structuring, and i'm still going crazy trying to figure it out. the specific point it's breaking at is when the calculator checks to see if the vehicle is over 7 feet tall. it seems to skip that "if" statement completely and just goes to the "else".
sorry for all the code, but i thought it might not make sense if the main structure wasn't there. all the variables are declared and such in the .cpp i have, but i didn't bother to post them here.
any tips on what i'm doing wrong will be greatly appreciated.
thanks!

int main()
{
// Passenger price calculation
cout << "Welcome to BCF Ferry Fare Calculator.\n";
cout << "How many adults (12+) in the party?\n";
cin >> adult;
cout << "How many children (age 5-11) are in your party?;
cin >> child;
cout << "Are you driving a vehicle onto the ferry? (y/n) \n";
cin >> input;


//For passengers with no vehicle
if (input == 'n' || input == 'N')
{
cout << "Your fare will be $"
<< fixed << adult*13 + child*6.50 <<
" plus a gas surcharge of $"
<< fixed << adult*1.25 + child*1.25 <<
".\n The total amount payable is $"
<< fixed << adult*14.25 + child*7.75 <<
".\n Thank you for using Fare Calculator. \n";
return 0;
}

//For passengers driving a vehicle onto the ferry
if (input == 'y' || input == 'Y');
{
cout << "What is the length of the vehicle in feet?\n";
cin >> vehicle;

//Check to see if vehicle is over 7 feet tall
if (vehicle > 20)
{
cout << "Is the vehicle over 7 feet high? (y/n) \n";
cin >> oversize;

//It skips this statement here when input is 'y'
if (oversize == 'y')
{
cout << "Your fare will be $"
<< fixed << adult*13 + child*6.50 + 69 + (vehicle - 20)*3.45
<<" plus a gas surcharge of $"
<< adult*1.25 + child*1.25 + 10.40
<<".\n The total amount payable is $"
<< adult*14.25 + child*7.75 + 79.40 + (vehicle - 20)*3.45
<<".\n Thank you for using Fare Calculator. \n";
}


//Here is where it skips to
else
{
cout << "Your fare will be $"
<< fixed << adult*13 + child*6.50 + 43 + (vehicle - 20)*2.15
<<" plus a gas surcharge of $"
<< fixed << adult*1.25 + child*1.25 + 4.15
<<".\n The total amount payable is $"
<< adult*14.25 + child*7.75 + 47.15 + (vehicle - 20)*2.15
<<".\n Thank you for using Fare Calculator. \n";
}
}

else if (vehicle <= 20)
{
cout << "Your fare will be $"
<< fixed << adult*13 + child*6.50 + 43
<<" plus a gas surcharge of $"
<< adult*1.25 + child*1.25 + 4.15
<< ".\n The total amount payable is $"
<< adult*14.25 + child*7.75 + 47.15
<< ".\n Thank you for using Fare Calculator. \n";
}

}
return 0;
}
Last edited on
please put your code in code tags before posting it so that people can actually read it :)

to do this just click the <> symbol and put your code inbetween the symbols that appear in the braces
This is the perfect place to ask such a question. In the future, use the code blocks tag [_code]//Your code here[_/code] without the underscore.


Are you entering a capital Y, by chance?
youre just missing a curly bracket after the vehicle < 20 statement :)
Last edited on
Check this statement. if (input == 'y' || input == 'Y'); that semicolon should not be there.
closed account (98qGz8AR)
thanks everybody! i took everybody's suggestions in sequence and fooled around with the code a bit more, but it still keeps skipping that oversize statement. i tried changing the condition to (oversize != 'n'), and then it executed the first statement successfully, but even when i put in "n" it would skip the else statement. i'm considering rewriting the program, but i don't see a way to get around using if/else statements because of the structure. is there another type of statement that would be better for this kind of branching (i.e. while, for, do, etc.)?
i'll keep messing with it, but i don't really know what else i can do.
EDIT: Nevermind. The brace was matched. Though, Maese909 was correct about the semicolon you put after the IF statement. Remove it.

Since you seem to be having trouble understand IF / ELSE blocks, let me give you some insight.

Here's an example of a simple IF / ELSE block:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using std::cout; // Don't worry about this.
using std::cin; // Or this...
using std::endl; // Or this.......

int main(int argc, char** argv) // You do not need these parameters.
{
    int n = 0; // Initializing a variable.

    cout << "Enter an integer: ";
    cin >> n;

    if(n >= 20)
    {
        cout << "N >= 20" << endl;
    }
    else
    {
        cout << "N < 20" << endl;
    }

    return 0;
}


Now, I think where you might be getting confused is nested IF statements and/or comparing variables to strings/characters, so here's an example of both together. I've spaced mine so it's easier for you to see where each IF statement and ELSE statement begin and end.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <string>
using std::string;

int main(int argc, char** argv)
{
    int n = 0;
    string x;

    cout << "Enter an integer: ";
    cin >> n;
    cout << "Type \"yes\" or \"no\": ";
    cin >> x;

    if(n >= 20)
    {
        cout << "N >= 20" << endl;

        if( ( x == "yes" ) || ( x == "no" ) )
        {
            cout << "N>=20: You entered \"yes\" or \"no\"!" << endl;
        }
        else
        {
            cout << "N>=20: I told you to enter \"yes\" or \"no\"!!!" << endl;
        }
    }
    else
    {
        cout << "N < 20" << endl;

        if( ( x == "yes" ) || ( x == "no" ) )
        {
            cout << "N<20: You entered \"yes\" or \"no\"!" << endl;
        }
        else
        {
            cout << "N<20: I told you to enter \"yes\" or \"no\"!!!" << endl;
        }
    }

    return 0;
}


Make note of the indentation and how each statement is structured. I spaced out the parentheses so you could read it better. Try it out and see if you can make sense of it.
Last edited on
closed account (98qGz8AR)
i finally got ahold of my programmer friend, and it took him 10 seconds to point out that i had cast the "oversize" variable as an 'int' instead of 'char'.
boy do i feel stupid. thanks for the help anyways everybody!
Well it was hard for us to point that out, being that you have your variables in a separate file and we couldn't see what type they all were... not to mention you didn't use code tags and didn't have any indentation.
closed account (98qGz8AR)
next time i need to post code, i'll remember to use the code tags and indent properly. thanks for posting what you did anyways, it was very helpful.
Topic archived. No new replies allowed.