Thread: Hi, I am having a problem with else not being linked to if.

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    30

    Hi, I am having a problem with else not being linked to if.

    I am doing a homework assignment where we have to have the user input a year and determine if it is a leap year or not. When i compile, I am have problems with "else" not being linked to "if". Am i writing a portion wrong, or what could it be. Any help is appreciated.



    Code:
    #include <iostream>
    
    using namespace std;
    
    
    int main()
    {
        int entered_year, leap_year;
    
    
        cout << "Welcome to the Leap Year program." << endl;
        cout << endl;
    
    
        cout << "Please enter the year you wish to check if it is a leap year: ";
        cin >> entered_year;
        cout << endl;
    
    
        cout << "Please wait while we check if ";
        cout << entered_year;
        cout << " is a leap year..." << endl;
    
    
            {
    
    
            if ( entered_year % 4 == 0 )
            cout << entered_year;
            cout << " is a leap year." << endl;
    
    
                {
    
    
                if ( entered_year % 100 == 0 )
                cout << entered_year;
                cout << " is not a leap year.";
    
    
                    {
    
    
                    if ( entered_year % 400 == 0)
                    cout << entered_year;
                    cout << " is a leap year.";
                    
                    
                    else
                    cout << entered_year;
                    cout << " is not a leap year." << endl;
    
    
                    }
    
    
                else
                cout << entered_year;
                cout << "is a leap year." << endl;
    
    
                }
    
    
            else
            cout << entered_year;
            cout << " is not a leap year." << endl;
    
    
            }
    
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    It is because your opening and closing brackets are not matching up. Take a look at How to place braces and parentheses and Lesson 2: If statements. That will clear up your errors.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    30
    When you say my opening and closing brackets, is it
    {
    if ( entered_year % 100 == 0 )


  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    72
    Read the if lesson he linked to, it will show your mistake.
    English is my first language, I'm just not any good at typing\writing it, I can read and speak it just fine.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, the brace should come after, not before the if clause.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    No, something like this:
    Code:
    if(some condition)
    {
         some stuff
         some more stuff
    }
    else if(some other condition)
    {
         some stuff
         some more stuff
    }
    else
    {
         some stuff
         some more stuff
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    72
    Code:
    if { (x=y)
    
    
    }
    like that
    you need the
    Code:
    {}
    for each if and eles
    English is my first language, I'm just not any good at typing\writing it, I can read and speak it just fine.

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Shawn Belcher View Post
    Code:
    if { (x=y)
    
    
    }
    like that
    you need the
    Code:
    {}
    for each if and eles
    Your opening bracket needs to move to after the conditional statement.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    30
    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    int main()
    {
        int Year;
    
    
        cout << "Welcome to the Leap Year program." << endl;
        cout << endl;
    
    
        cout << "Please enter the year you wish to check if it is a leap year: ";
        cin >> Year;
        cout << endl;
    
    
        cout << "Please wait while we check if ";
        cout << Year;
        cout << " is a leap year..." << endl;
    
    
            {
    
    
                if ( Year % 4 == 0 )
                {
                cout << Year;
                cout << " is a leap year." << endl;
    
    
    
    
                    if ( Year % 100 == 0 )
                    {
                    cout << Year;
                    cout << " is not a leap year." << endl;
    
    
    
    
                        if ( Year % 400 == 0)
                        {
                        cout << Year;
                        cout << " is a leap year." << endl;
    
    
                        }
                        else
                        cout << Year;
                        cout << " is not a leap year." << endl;
    
    
    
    
                    }
                    else
                    cout << Year;
                    cout << " is a leap year." << endl;
    
    
                }
                else
                cout << Year;
                cout << " is not a leap year." << endl;
    
    
            }
    
    
        return 0;
    
    
    }
    It now compiles, but now when I run it, it gives me 3 answers, 2 of which are correct, 1 of which is not. I attempted to input "else if" into the code, then it would not work.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Be careful when you fail to use braces.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Sep 2011
    Posts
    30
    Am i at least on the right track?

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes. Here's a hint. I would format this:
    Code:
    if ( Year % 400 == 0)
    {
    cout << Year;
    cout << " is a leap year." << endl;
    
    
    }
    else
    cout << Year;
    cout << " is not a leap year." << endl;
    as:
    Code:
    if ( Year % 400 == 0)
    {
        cout << Year;
        cout << " is a leap year." << endl;
    }
    else
        cout << Year;
    cout << " is not a leap year." << endl;
    and even write it as:
    Code:
    if ( Year % 400 == 0)
    {
        cout << Year;
        cout << " is a leap year." << endl;
    }
    else
    {
        cout << Year;
    }
    cout << " is not a leap year." << endl;
    Now can you spot the mistake?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    It is because you are still not matching up your braces. Try this:
    Code:
    #include <iostream>
    
    int main(void)
    {
        using namespace std;
        
        int Year;
    
        cout << "Welcome to the Leap Year program." << endl;
        cout << endl;
    
        cout << "Please enter the year you wish to check if it is a leap year: ";
        cin >> Year;
        cout << endl;
    
        cout << "Please wait while we check if ";
        cout << Year;
        cout << " is a leap year..." << endl;
    
        if ( Year % 4 == 0 )
        {
    	cout << Year;
    	cout << " is a leap year." << endl;
        }
        else if( Year % 100 == 0 )
        {
    	cout << Year;
    	cout << " is not a leap year." << endl;
        }
        else if ( Year % 400 == 0)
        {
    	cout << Year;
    	cout << " is a leap year." << endl;
        }
        else
        {
    	cout << Year;
    	cout << " is not a leap year." << endl;
        }
        return 0;
    }
    EDIT:OOPs it appears when I was fixing your formatting Laser was leading you down a path. My bad Laser.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  14. #14
    Registered User
    Join Date
    Sep 2011
    Posts
    30
    Oh, I understand. So should i simplify how I go about oragnizing and indenting my code to spot those mistakes easier? Sorry, I'm just barely starting coding.

  15. #15
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by iGuardian View Post
    Oh, I understand. So should i simplify how I go about oragnizing and indenting my code to spot those mistakes easier? Sorry, I'm just barely starting coding.
    Yes, it makes it much easier for you to spot your mistakes. Take a look at the link I gave you in post #2 about indentation.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Problem
    By llinocoe in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 07:02 PM
  2. Linked list problem
    By mr_glass in forum C Programming
    Replies: 4
    Last Post: 03-07-2006, 02:12 AM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. linked list....problem
    By Forever82 in forum C++ Programming
    Replies: 6
    Last Post: 07-21-2003, 03:30 AM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM