Announcement

Collapse
No announcement yet.

C++ again still confused

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • C++ again still confused

    i still have two parse errors..why is this happening and what am i doing wrong
    #include <iostream>

    using namespace std;

    int main(void)
    {
    cout << "Input a number for a year greater than one: ";
    int N;
    cin >> N;

    if (N<1)
    {cout << "Sorry, your year must be greater than 1.\n";
    return(1);
    }
    if(N%4==0)
    {if (N%100==0);
    {if (N%400==0);
    {cout << "The year" << N << "is a leap year.\n";
    return(0);
    }
    }
    else(N%100==-0)}
    {cout << "The year" << N << "is a leap year.\n";
    return(0);
    }
    }
    else(N%4==-0)}
    {cout << "The year" << N << "is not a leap year.\n";
    return(0);
    }
    }
    }

  • #2
    So many many things I see that I do not like with your code.

    First, this isn't the right forum, and you should use the code tags to preserve formatting.

    Why do you have so many returns? You only need one at the end of the function. Shouldn't the main be an int for this one? Why are you using nested if statements when you can just use && instead? (I am talking about yout (if (N%4==0) then having another if insiade it. It can be accomplished with the && operator - BUT - it isn't even necessary to have those extra ifs in there) It is also a good practise to use meaningful identifers..

    Here is what I made from scratch.

    Code:
    #include <iostream>
        using namespace std;
        
        int main()
        {
        	int year;
        	int leap;
        	
        	cout << "Input a year greater then one: ";
        	cin >> year;
        
        	leap = year % 4;
        
        	if (year < 1) {
        		cout << "Sorry, the number must be greater then one\n";
        	}
        	else {
        		if (leap == 0) {
     			cout << "The year " << year << " is a leap year\n"; //please see my next post - I had a wrong definition of a leap year
        		}
        		else {
     			cout << "The year " << year << " is not a leap year\n";
        		}
        	}
        
        	return 0;
        }
    I tested it with a ton of different years, and it works fine.
    Last edited by UHN_ED; Thu 2nd Oct '03, 2:02pm. Reason: Bad formula used

    Comment


    • #3
      You can't just check if the year is divisible by four. 1900 is not a leap year, 2100 is not a leap year. If the year is divisible by 100, but not 400, then it is not a leap year.

      Comment


      • #4
        Hmm, guess I didn't know of the definition of a leap year very well then.

        Code very easily adapted though:

        Code:
        #include <iostream>
         using namespace std;
         
         int main()
         {
         	int year;
         	
         	cout << "Input a year greater then one: ";
         	cin >> year;
         
         	if (year < 1) {
         		cout << "Sorry, the number must be greater then one\n";
         	}
         	else {
         		if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
         			cout << "The year " << year << " is a leap year\n";
         		}
         		else {
         			cout << "The year " << year << " is not a leap year\n";
         		}
         	}
         
         	return 0;
         }

        Comment


        • #5
          c++ is not html

          Comment

          Loading...
          Working...
          X