Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,268 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,470 people online right now. Registration is fast and FREE... Join Now!




If / Else Error

2 Pages V  1 2 >  
Reply to this topicStart new topic

If / Else Error

aznballerlee
14 Oct, 2006 - 12:20 PM
Post #1

D.I.C Head
**

Joined: 14 Oct, 2006
Posts: 61


My Contributions
I keep getting error 2181 on Visual C++ when I try to run this.
It says something is invalid with my else if statement?
How can this be fixed?

Here is my code:


CODE

# include <iostream>
# include <string>
using namespace std;


int main ()
{
    cout << "Destination: ";
    string ridersDestination;
    cin >> ridersDestination;
    cout << "Number of zone boundaries crossed: ";
    int numberOfBoundaries;
    cin >> numberOfBoundaries;
    cout << "Age of rider: ";
    int ageOfRider;
    cin >> ageOfRider;
    cout << "Student? (y/n): ";
    if ((ageOfRider >= 18) && (65 > ageOfRider))
        cout << "y \n";
    else
        cout << "n \n";
    cout << "--- \n";

    if
            ( (ageOfRider < 18 && numberOfBoundaries == 0) || (ageOfRider < 18 && numberOfBoundaries == 1) )
            cout << "The fare to " << ridersDestination << " is $0.65.";

    else if
        ( (ageOfRider > 18) && (ageOfRider < 65) && (numberOfBoundaries == 0) )
        cout << "The fare to " << ridersDestination << " is $0.65.";

    for (ageOfRider >= 65)
        if
            (numberOfBoundaries !=0)
        double fare = 0.50 + (0.25 * numberOfBoundaries);
        cout << "The fare to " << ridersDestination << " is $" << fare << ".";

        else
         (numberOfBoundaries == 0)
        cout << "The fare to " << ridersDestination << " is $0.30.";

    else
        double seniorFare = 1.45 + (0.50 * numberOfBoundaries);
        cout << "The fare to" << ridersDestination << "is $" << seniorFare <<".";
    
    



    








}


edit: added [code] tags ~ jayman9
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: If / Else Error
14 Oct, 2006 - 12:43 PM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
Hmmm....it may be that the compiler is misintepreting some commands becasue there are no braces {} used in your if else if statements. In fact, it can be difficult to diagnose without them. Can we assume that each if, else if, or else statement should be followed by only one single command, or are there instances in which that statement should refer to more than one command?
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: If / Else Error
14 Oct, 2006 - 03:33 PM
Post #3

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(aznballerlee @ 14 Oct, 2006 - 01:20 PM) *

I keep getting error 2181 on Visual C++ when I try to run this.
It says something is invalid with my else if statement?
How can this be fixed?


CODE
if ((ageOfRider >= 18) && (65 > ageOfRider))
        cout << "y \n"; // one line - no braces
    else
        cout << "n \n"; // one line - no braces


CODE
if ( (ageOfRider < 18 && numberOfBoundaries == 0) || (ageOfRider < 18 && numberOfBoundaries == 1) )
            cout << "The fare to " << ridersDestination << " is $0.65."; // one line - no braces

    else if  ( (ageOfRider > 18) && (ageOfRider < 65) && (numberOfBoundaries == 0) )
        cout << "The fare to " << ridersDestination << " is $0.65."; // one line - no braces


I concur with Armadeus, although the code meets the C/C++ coding requirements ( braces optional for a single line ) it does look like the compiler is experiencing some problems.
User is offlineProfile CardPM
+Quote Post

aznballerlee
RE: If / Else Error
14 Oct, 2006 - 04:03 PM
Post #4

D.I.C Head
**

Joined: 14 Oct, 2006
Posts: 61


My Contributions
I don't understand what you mean by one line - no braces.
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: If / Else Error
14 Oct, 2006 - 04:20 PM
Post #5

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(aznballerlee @ 14 Oct, 2006 - 05:03 PM) *

I don't understand what you mean by one line - no braces.
Azn,

C/C++ allows you to do the following with an if statement:
CODE
if ( condition )
    cout <<  "Condition evaluated to true";
else
    cout << "condition evaluated to false";
which has one line after the if, and doesn't require the {}. which is the reason for saying "one line - no braces"

Optionally, you could have written:
CODE
if ( condition )
{
    cout <<  "Condition evaluated to true";
}
else
{
    cout << "condition evaluated to false";
}


They both do the same thing, but had you done this:
CODE
if ( condition )
    cout <<  "Condition evaluated to true" << endl;
    cout << "Well done!";
else
    cout << "condition evaluated to false";


Where you wanted "Condition evaluated to true" and "Well done!" to be displayed, then the use of the braces {} is mandatory. It must be written as:
CODE
if ( condition )
{
    cout <<  "Condition evaluated to true" << endl;
    cout << "Well done!";
}
else
    cout << "condition evaluated to false";
for the desired outcome and to successfully compile the program.

To save yourself some problems, use the braces in your if statements.

This post has been edited by gregoryH: 14 Oct, 2006 - 04:21 PM
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: If / Else Error
14 Oct, 2006 - 04:34 PM
Post #6

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(aznballerlee @ 14 Oct, 2006 - 01:20 PM) *

It says something is invalid with my else if statement?


CODE
if ( (ageOfRider < 18 && numberOfBoundaries == 0) || (ageOfRider < 18 && numberOfBoundaries == 1) )
    cout << "The fare to " << ridersDestination << " is $0.65.";

    else if
        ( (ageOfRider > 18) && (ageOfRider < 65) && (numberOfBoundaries == 0) )
        cout << "The fare to " << ridersDestination << " is $0.65.";
}

I think I have just realised the fault might be this:else if

Is there any chance that MSVC++ wants this: elseif?
User is offlineProfile CardPM
+Quote Post

aznballerlee
RE: If / Else Error
14 Oct, 2006 - 04:47 PM
Post #7

D.I.C Head
**

Joined: 14 Oct, 2006
Posts: 61


My Contributions
Thanks. I think I settled that part by just putting a bunch of if statements instead of else if.

But another problem I ran into was this:

Suppose I want the statements 18<ageOfRider<65 and also satisfying the condition numberOfBoundaries > 0.

Is this the right form?

if ((ageOfRider > 18) && (ageOfRider <65) && (numberOfBoundaries > 0))
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: If / Else Error
14 Oct, 2006 - 05:57 PM
Post #8

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
That is certainly an acceptable implementation.
User is offlineProfile CardPM
+Quote Post

aznballerlee
RE: If / Else Error
14 Oct, 2006 - 08:33 PM
Post #9

D.I.C Head
**

Joined: 14 Oct, 2006
Posts: 61


My Contributions
if #1: ( (ageOfRider >= 65) && (numberOfBoundaries > 0) )

if #2: ((ageOfRider > 18) && (ageOfRider < 65) && (numberOfBoundaries > 0))

here are two if statements that cause conflict when i try to build the function.
For example, when I try to input 67 for age, I get output for both statements 1 & 2.
I think this is because the second statement ageOfRider >18 causes statement 2 to apply too.
How can I fix this? So that I want statement 2 to fit only the range of 18 <x< 65?
User is offlineProfile CardPM
+Quote Post

Jayman
RE: If / Else Error
14 Oct, 2006 - 08:52 PM
Post #10

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Logically both statements cannot execute if you enter 67, only one will assuming numberOfBoundaries > 0.

The first IF statement would evaluate to true since both conditions would be "true true". Your 2nd IF statement must have all three conditions be true in order for the statement to be true. So the statement currently would evaluate to "true false true" which would result in a false statement.

You would need to show your current code structure to determine what else could be the cause. You may have a misplaced {} bracket or some other problem.
User is offlineProfile CardPM
+Quote Post

aznballerlee
RE: If / Else Error
15 Oct, 2006 - 02:39 PM
Post #11

D.I.C Head
**

Joined: 14 Oct, 2006
Posts: 61


My Contributions
Here is my program so far.

_____________________

CODE

# include <iostream>
# include <string>
using namespace std;


int main ()
{
    cout << "Destination: ";
    string ridersDestination;
    cin >> ridersDestination;
    cout << "Number of zone boundaries crossed: ";
    int numberOfBoundaries;
    cin >> numberOfBoundaries;
    cout << "Age of rider: ";
    int ageOfRider;
    cin >> ageOfRider;
    cout << "Student? (y/n): ";
    if ((ageOfRider >= 18) && (65 > ageOfRider))
        cout << "y \n";
    else
        cout << "n \n";
    cout << "--- \n";

    if
    ( (ageOfRider < 18 && numberOfBoundaries == 0) || (ageOfRider < 18 && numberOfBoundaries == 1) )
            cout << "The fare to " << ridersDestination << " is $0.65.";
    
    if
        ( (ageOfRider > 18) && (ageOfRider < 65) && (numberOfBoundaries == 0) )
        cout << "The fare to " << ridersDestination << " is $0.65.";
    
    if
    ( (ageOfRider >= 65) && (numberOfBoundaries > 0) )
        double fare;
        double fare = 0.50 + (0.25 * numberOfBoundaries);
       cout << "The fare to " << ridersDestination << " is $" << fare << ".";

    if
        ( (ageOfRider >= 65) && (numberOfBoundaries == 0) )
        cout << "The fare to " << ridersDestination << " is $0.30.";

    if
    (ageOfRider > 18 && ageOfRider < 65 && numberOfBoundaries > 0)

        double seniorFare;
        double seniorFare = 1.45 + (0.50 * numberOfBoundaries);
        cout << "The fare to " << ridersDestination << " is $" << seniorFare <<".";
    
    

}


edit: added [code] tags ~ jayman9
User is offlineProfile CardPM
+Quote Post

Jayman
RE: If / Else Error
15 Oct, 2006 - 03:19 PM
Post #12

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
The problem is this, if you have more than one line of code that needs to be in a IF statement then you need to enclose all of the statements in {} code block.

This was mentioned in a previous reply. So in your following code without it being enlosed in {}'s, the only statement that is inside your IF statement is double seniorFare;. The 2 lines after that will execute no matter what.
CODE

    if
    (ageOfRider > 18 && ageOfRider < 65 && numberOfBoundaries > 0)

        double seniorFare;
        double seniorFare = 1.45 + (0.50 * numberOfBoundaries);
        cout << "The fare to " << ridersDestination << " is $" << seniorFare <<".";


It is good programming practice to enlose ALL code inside an IF statement inside {}'s. Even if it is only one line.

The solution looks like this, now all of the code that should execute IF that condition is true, will execute accurately.
CODE

    if (ageOfRider > 18 && ageOfRider < 65 && numberOfBoundaries > 0)
    {
        double seniorFare;
        double seniorFare = 1.45 + (0.50 * numberOfBoundaries);
        cout << "The fare to " << ridersDestination << " is $" << seniorFare <<".";
    }


Do you now see the importance using {}'s??
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 12/4/08 07:41PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month