Attached is my code for my homework for my C++ class. My problem comes from when I run the program. It returns a value of 1 every single time for the output of the "is it a leap year or not" part of my program. Then, when it outputs the ordinal date of the year the program returns a value like -14300986, when the actual value should be between 1 and 365. I searched all over the net and this site, finding partial help that has allowed me to get this far. The assignment statement can be viewed here: http://www.cs.mtsu.edu/~jsigle/1170/projects/project4.txt :I would ask my teacher for help on this except he has changed the due date to a day that doesn't coincide with my schedule, therefore I can't go to him for help. Any help would be highly appreciated, and if anyone doesn't want to give me the exact answer to my problem, then examples are welcome as well. I want to learn this, I'll take the easy answer. But if someone wants to take the time, I would like to learn what I'm doing wrong. Thank you.

#include <iostream>
using namespace std;
bool isLeapYear (int year)
{
    if (year%400 == 0 || year%4 == 0)
    {
        return true;
        cout << "Year is a Leap Year." << endl;
    }
    else
    {   
        return false;
        cout << "Year is not a leap year." << endl;
    }
}

int main()
{
    int month;
    int day;
    int year;
    int ordinalDay;

    // Input date
    cout << "Please type in a date in the form MM DD YYYY: ";
    cin >> month >> day >> year;

    // Calculate Ordinal Day of the Year and display it
    if (!isLeapYear)
    {
    if (month == 1)
    { 
        ordinalDay == day;
    }
    if (month == 2)
    {
        ordinalDay == 31 + day;
    }
    if (month == 3)
    { 
        ordinalDay == 59 + day;
    }
    if (month == 4)
    {
        ordinalDay == 90 + day;
    }
    if (month == 5)
    {
        ordinalDay == 120 + day;
    }
    if (month == 6)
    {
        ordinalDay == 151 + day;
    }
    if (month == 7)
    {
        ordinalDay == 181 + day;
    }
    if (month == 8)
    {
        ordinalDay == 212 + day;
    }
    if (month == 9)
    {
        ordinalDay == 243 + day;
    }
    if (month == 10)
    {
        ordinalDay == 273 + day;
    }
    if (month == 11)
    {
        ordinalDay == 304 + day;
    }
    if (month == 12)
    {
        ordinalDay == 334 + day;
    }
    }
    else if (isLeapYear)
    {
    if (month == 1)
    { 
        ordinalDay == day;
    }
    if (month == 2)
    {
        ordinalDay == 31 + day;
    }
    if (month == 3)
    { 
        ordinalDay == 60 + day;
    }
    if (month == 4)
    {
        ordinalDay == 91 + day;
    }
    if (month == 5)
    {
        ordinalDay == 121 + day;
    }
    if (month == 6)
    {
        ordinalDay == 152 + day;
    }
    if (month == 7)
    {
        ordinalDay == 182 + day;
    }
    if (month == 8)
    {
        ordinalDay == 213 + day;
    }
    if (month == 9)
    {
        ordinalDay == 244 + day;
    }
    if (month == 10)
    {
        ordinalDay == 274 + day;
    }
    if (month == 11)
    {
        ordinalDay == 305 + day;
    }
    if (month == 12)
    {
        ordinalDay == 335 + day;
    }
    }   

    // Display output from leap year function and ordinal day function
    cout << isLeapYear << endl;
    cout << "Ordinal Day Is: " << ordinalDay << endl;


    return 0;
}

Why can't you do something like this.

unsigned int mm; //holds the month
unsigned int dd; //holds the day
unsigned int yy; //holds the year

unsigned int ordinal_day = 0; //holds your ordinal day number

//define an array which holds the number of days in each month
unsigned int month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

if (/* yy is leap year*/)
    month[1] = 29;

for (int i = 0; i < (mm-1) ; i++)
    ordinal_day += month[i];

ordinal_day += day;

Why can't you do something like this.

unsigned int mm; //holds the month
unsigned int dd; //holds the day
unsigned int yy; //holds the year

unsigned int ordinal_day = 0; //holds your ordinal day number

//define an array which holds the number of days in each month
unsigned int month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

if (/* yy is leap year*/)
    month[1] = 29;

for (int i = 0; i < (mm-1) ; i++)
    ordinal_day += month[i];

ordinal_day += day;

Unfortunately, we haven't learned the "unsigned int" function you used, as well as the "+=". Also, it has to have a bool function for the leap year as that is part of the assignment. This my first C++ class, so him throwing in functions that are not described in class was not fun to try and figure out. I like the way your code looks, it's simple and to the point. But I can't use it, is there any way to make what I have work?

oh come on!!!
I should not have spoon fed :(

1. There is no harm in using int variables. change unsigned to int.
2. += can be replaced with ordinal_day = ordinal_day + month;
3. Last but not least, you must have a bool function for leap year check. I didn't take the pain in writing one for you. So I put a comment inside if-condition. You have to call a bool function, inside the if-condition. The argument to that function should be the year and it returns true or false.

oh come on!!!
I should not have spoon fed :(

1. There is no harm in using int variables. change unsigned to int.
2. += can be replaced with ordinal_day = ordinal_day + month;
3. Last but not least, you must have a bool function for leap year check. I didn't take the pain in writing one for you. So I put a comment inside if-condition. You have to call a bool function, inside the if-condition. The argument to that function should be the year and it returns true or false.

int is simple enough, just didn't understand the whole unsigned part. For the most part I understand the problem better. At the moment I'm trying to work this in without copy and pasting it. Thank you for your help!

Any idea on how I get the cout statements in my bool function to display instead of the one I have at the bottom?

In your code you are not calling isLeapYear()

//if (!isLeapYear) -- wrong
if (!isLeapYear(year)) //right way to call the function!!

Why I preferred unsigned is that there is not such thing like a negative date, month or year.

That makes sense, I'll have to thank my teacher for throwing in code and not explaining it to us.

Here are the changes I was able to make, it took a little bit. Having "==" for ordinal day was giving me my large numbers. As well as not calling my bool function in "int main()". I appreciate the help, another one down, many more to go.

#include <iostream>
using namespace std;
bool isLeapYear (int year)
{
	if (year%400 == 0 || year%4 == 0)
	{
		cout << "Year is a Leap Year." << endl;
		return true;
		
	}
	else
	{	
		cout << "Year is not a leap year." << endl;
		return false;
		
	}
}
	
int main()
{
	int month = 0;
	int day = 0;
	int year = 0;
	int ordinalDay = 0;
	
	// Input date
	cout << "Please type in a date in the form MM DD YYYY: ";
	cin >> month >> day >> year;
	bool leapYear = isLeapYear(year);
	
	// Calculate Ordinal Day of the Year
	if (!leapYear)
	{
		if (month == 1)
		{ 
			ordinalDay = 0 + day;
		}
		else if (month == 2)
		{
			ordinalDay = 31 + day;
		}
		else if (month == 3)
		{ 
			ordinalDay = 59 + day;
		}
		else if (month == 4)
		{
			ordinalDay = 90 + day;
		}
		else if (month == 5)
		{
			ordinalDay = 120 + day;
		}
		else if (month == 6)
		{
			ordinalDay = 151 + day;
		}
		else if (month == 7)
		{
			ordinalDay = 181 + day;
		}
		else if (month == 8)
		{
			ordinalDay = 212 + day;
		}
		else if (month == 9)
		{
			ordinalDay = 243 + day;
		}
		else if (month == 10)
		{
			ordinalDay = 273 + day;
		}
		else if (month == 11)
		{
			ordinalDay = 304 + day;
		}
		else if (month == 12)
		{
			ordinalDay = 334 + day;
		}
	}
	else 
	{
		if (month == 1)
		{ 
			ordinalDay = 0 + day;
		}
		else if (month == 2)
		{
			ordinalDay = 31 + day;
		}
		else if (month == 3)
		{ 
			ordinalDay = 60 + day;
		}
		else if (month == 4)
		{
			ordinalDay = 91 + day;
		}
		else if (month == 5)
		{
			ordinalDay = 121 + day;
		}
		else if (month == 6)
		{
			ordinalDay = 152 + day;
		}
		else if (month == 7)
		{
			ordinalDay = 182 + day;
		}
		else if (month == 8)
		{
			ordinalDay = 213 + day;
		}
		else if (month == 9)
		{
			ordinalDay = 244 + day;
		}
		else if (month == 10)
		{
			ordinalDay = 274 + day;
		}
		else if (month == 11)
		{
			ordinalDay = 305 + day;
		}
		else if (month == 12)
		{
			ordinalDay = 335 + day;
		}
	}	
	
	// Display output from Ordinal Day function
	cout << "Ordinal Day Is: " << ordinalDay << endl;
	
	
	return 0;
}

I seriously believe this is not the right way to code. Why can't you use arrays and make the code shorter? Or is it that you are not taught about arrays till now?

Mark as solved if you are done with it.

I know that this code is a bit too much. But it's what our teacher wants, and also expects out of us as beginner Computer Science majors. No, I haven't been taught about arrays. I just learned what flags were today. If that's any indicator at what type of pace we're going at. At the end of the day, they want us to start it off big while understanding every single aspect of the code. Then when we transfer to the next class we'll learn to make our code shorter and more precise.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.