Welcome to Dream.In.Code
Getting C++ Help is Easy!

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




Need help leap year

 
Reply to this topicStart new topic

Need help leap year, fatal error LNK1120: 1 unresolved externals

KMH
12 Apr, 2008 - 08:03 PM
Post #1

D.I.C Head
**

Joined: 23 Mar, 2008
Posts: 80

Hello,

I read don't know what is wrong what this code, I has been at it a few hours now and can't see what is the proplem??? I an error message : fatal error LNK1120: 1 unresolved externals, please help.

CODE

#include <iostream>
#include <string>
#include <math.h>

using namespace std;

//Declare the function
bool leapyear(int year);
    
struct // struct declaration
{
    int Month;
    int Day;
    int Year;
    
}date;

int main()
{
                              
    // Intialize Array For Number Of Days In Each Month
    int months[12]= {31,28,31,30,31,30,31,31,30,31,30,31}; //Array For Number Of Days In Each Month
    //Initialize array for number of month in the year
    const unsigned int string_month = 12;
    string arr[string_month] = {"January","february","March","April","May","June","July","August","September","October","November","December"};
                                                                                
    int mm, dd, yyyy, year = 0;
    
    cout << "This program will display date and convert number of month to string" << endl;
    //Prompts user for input
    cout << "Enter month in the form of mm: ";
    cin >> mm;//Get month from input
    cout << "Enter day in the form of dd: ";
    cin >> dd;//Get day from input
    cout << "Enter year in the form of yyyy: ";
    cin >> yyyy;//Get year from input
    cin.get();

    if( mm < 1 || mm > 12 )// Validate to see if input for month is valid
        {
         cout << "INVALID month: Please try again." << endl;
         cin.get();
         exit (1);
         }
        
    if( dd < 1 || dd > months[dd-1] )// Validate to see if input for day is valid
        {
         cout << "INVALID day: Please try again." << endl;
         cin.get();
         exit (1);
        }

    if(leapyear(year))

        cout << "is a leap year\n";
    else
        cout << "is not a leap year\n";

        cout << "You have entered: " << mm << "-" << dd << "-" << yyyy << " which " << endl;
        cout << yyyy << " is a leap year and it is should be." << endl;
        return (year % 4 == 0);         
         {        
            date.Year = yyyy;
            date.Month = mm;
            date.Day = dd;

            if(dd <= months[mm -1])
            
                cout << arr[mm - 1] << ", " << date.Day << " " << date.Year << endl;
            else
                cout << "Invalid Date for the month!";
            //return (year % 4 == 0);
         }        
cin.get();
return 0;
}

User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Need Help Leap Year
12 Apr, 2008 - 08:11 PM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,435



Thanked: 64 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
You define the leapyear function....
QUOTE
CODE

//Declare the function
bool leapyear(int year);



You call it & test it's results...
QUOTE
CODE
    
if(leapyear(year))
        cout << "is a leap year\n";
    else
        cout << "is not a leap year\n";



But ... where is the function? Where is the code for the function leapyear that you defined?
User is online!Profile CardPM
+Quote Post

KMH
RE: Need Help Leap Year
12 Apr, 2008 - 09:40 PM
Post #3

D.I.C Head
**

Joined: 23 Mar, 2008
Posts: 80

Hello no2pencil,

Thanks for your replied, by the way what is the video you have captured below?? the lady in the blue hit the lady in the red?? where is it take palce?? look like they were in the court house or something.

Anyway thanks for piont that out, I added the FunctionCall below main and it compiled but din't work right I give up. : blink.gif

CODE

#include <iostream>
#include <string>
#include <math.h>

using namespace std;

//Declare the function
bool leapyear(int year);
    
struct // struct declaration
{
    int Month;
    int Day;
    int Year;
    
}date;

int main()
{
                              
    // Intialize Array For Number Of Days In Each Month
    int months[12]= {31,28,31,30,31,30,31,31,30,31,30,31}; //Array For Number Of Days In Each Month
    //Initialize array for number of month in the year
    const unsigned int string_month = 12;
    string arr[string_month] = {"January","february","March","April","May","June","July","August","September","October","November","December"};
                                                                                
    int mm, dd, yyyy, year = 0;
    
    cout << "This program will display date and convert number of month to string" << endl;
    //Prompts user for input
    cout << "Enter month in the form of mm: ";
    cin >> mm;//Get month from input
    cout << "Enter day in the form of dd: ";
    cin >> dd;//Get day from input
    cout << "Enter year in the form of yyyy: ";
    cin >> yyyy;//Get year from input
    cin.get();

    if( mm < 1 || mm > 12 )// Validate to see if input for month is valid
        {
         cout << "INVALID month: Please try again." << endl;
         cin.get();
         exit (1);
         }
        
    if( dd < 1 || dd > months[dd-1] )// Validate to see if input for day is valid
        {
         cout << "INVALID day: Please try again." << endl;
         cin.get();
         exit (1);
        }
        
    if(leapyear(year))

        cout << "is a leap year\n";
    else
        cout << "is not a leap year\n";

        cout << "You have entered: " << mm << "-" << dd << "-" << yyyy << " which " << endl;
        cout << yyyy << " is a leap year and it is should be." << endl;
        {        
            date.Year = yyyy;
            date.Month = mm;
            date.Day = dd;

            if(dd <= months[mm -1])
            
                cout << arr[mm - 1] << ", " << date.Day << " " << date.Year << endl;
            else
                cout << "Invalid Date for the month!";
            
         }

cin.get();
return 0;
}

bool leapyear(int year)
  {
    return (year % 4 == 0);
  }


result:
Enter month in the form mm: 02
enter day in the form dd: 29
Enter year in the form yyyy: 2004
INVALID DAY: Please try again.


*Mod Edit: Fixed code tags. There is no [result] tag in out bbcode system, so I surrounded that in a font tag with a fixed width font.
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Need Help Leap Year
12 Apr, 2008 - 10:04 PM
Post #4

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,435



Thanked: 64 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
You have 2 errors in your code.

Error number 1. You initilize year to 0, & then you never set it.

At line 35, you read the value into variable yyyy, & then you do nothing with it. Try entering the following.
cpp

cin >> yyyy;//Get year from input
year = yyyy;


Error number 2. You don't return the proper math on the year passed in in your leapyear function.

Try the following function instead.
cpp

int leapyear(int year) {
return ((year % 4) == 0);
//return (year % 4 == 0);
}


After these changes the code works correctly, as so...
QUOTE

>$./time
This program will display date and convert number of month to string
Enter month in the form of mm: 01
Enter day in the form of dd: 01
Enter year in the form of yyyy: 2001
is not a leap year
You have entered: 1-1-2001 which
2001 is a leap year and it is should be.

QUOTE

>$./time
This program will display date and convert number of month to string
Enter month in the form of mm: 01
Enter day in the form of dd: 01
Enter year in the form of yyyy: 2004
is a leap year
You have entered: 1-1-2004 which
2004 is a leap year and it is should be.


I'm not sure why that you have that last line in there, that says it should be, but the logic is correct with these 2 fixes.
User is online!Profile CardPM
+Quote Post

KMH
RE: Need Help Leap Year
12 Apr, 2008 - 10:41 PM
Post #5

D.I.C Head
**

Joined: 23 Mar, 2008
Posts: 80

OK no2pencil, it work with either functionCall ( int or bool) but if it is a leap year 02-28-2004 entered by user then it should display 02-29 2004 instead??? I know I'm very close get it working. crazy.gif

CODE

#include <iostream>
#include <string>
#include <math.h>

using namespace std;

//Declare the function
bool leapyear(int year);
    
struct // struct declaration
{
    int Month;
    int Day;
    int Year;
    
}date;

int main()
{
                              
    // Intialize Array For Number Of Days In Each Month
    int months[13]= {31,28,31,30,31,30,31,31,30,31,30,31}; //Array For Number Of Days In Each Month
    //Initialize array for number of month in the year
    const unsigned int string_month = 12;
    string arr[string_month] = {"January","february","March","April","May","June","July","August","September","October","November","December"};
                                                                                
    int mm, dd, yyyy, year = 0;
    
    cout << "This program will display date and convert number of month to string" << endl;
    //Prompts user for input
    cout << "Enter month in the form of mm: ";
    cin >> mm;//Get month from input
  if( mm < 1 || mm > 12 )// Validate to see if input for month is valid
        {
         cout << "INVALID month: Please try again." << endl;
         cin.get();
         system ("pause");
         exit (1);
         }
    cout << "Enter day in the form of dd: ";
    cin >> dd;//Get day from input
  if( dd < 1 || dd > months[dd-1] )// Validate to see if input for day is valid
        {
         cout << "INVALID day: Please try again." << endl;
         cin.get();
         system ("pause");
         exit (1);
        }
    cout << "Enter year in the form of yyyy: ";
    cin >> yyyy;//Get year from input
    cin.get();    
       
    if(leapyear(year))

        cout << yyyy << " is a leap year.\n";
    else
        cout << yyyy << " is not a leap year\n";

        cout << "You have entered: " << mm << "-" << dd << "-" << yyyy << " which ";// << endl;
        cout << "it should be: ";// << endl;
        {        
            date.Year = yyyy;
            date.Month = mm;
            date.Day = dd;

            if(dd <= months[mm - 1])
            
                cout << arr[mm - 1] << ", " << date.Day << " " << date.Year << endl;
            else
                cout << "Invalid Date for the month!";
            
         }

cin.get();
return 0;
}

bool leapyear(int year)
  {
    return ((year % 4) == 0);
  }

User is offlineProfile CardPM
+Quote Post

KMH
RE: Need Help Leap Year
13 Apr, 2008 - 09:00 AM
Post #6

D.I.C Head
**

Joined: 23 Mar, 2008
Posts: 80

so far I got it working without any problem, one thing that I still didn't feel right about this code is that, what if user enter 02-28-2004, it tell yes that is the leapyear, but it didn't correct the date entered, it should display 2004 is the leapyear: February, 29 2004. Another problem is that if user entered 02-29-2004, then it just need to display yyyy is leapyear: February, 29 2004. This code got too complicated, all I want it to do is display the leapyear if divisible by 4 then days range from 1-29, else it is not the leapyear day range from 1-28.

CODE

year = yyyy;
    cout << "You have entered: " << mm << "-" << dd << "-" << yyyy << " which: ";
    cout << arr[mm - 1] << ", " << date.Day << " " << date.Year << endl;
    {
    lp_ck = yyyy%4;
    lp_ck = yyyy%4;
    if(lp_ck==0)
    if(leapyear(year))
      months[1]=29;
      cout << yyyy << " is a leap year.\n";
    else
      months[1]=28;
      cout << yyyy << " is not a leap year\n";
    }

    //if(leapyear(year))
        
        //cout << yyyy << " is a leap year.\n";
    //else
        //cout << yyyy << " is not a leap year\n";



I changed the code in check for leapyear part, it tell me that I error:
[error]
error C2181: illegal else without matching if
[error]
User is offlineProfile CardPM
+Quote Post

KMH
RE: Need Help Leap Year
13 Apr, 2008 - 04:21 PM
Post #7

D.I.C Head
**

Joined: 23 Mar, 2008
Posts: 80

OK the code now compiled cause missing the {}, but it did not display what it should, the user can not enter int months[14]= {31,28,31,30,31,30,31,31,30,31,30,31}; dd > 28 for the month of February, 29 2004 for leapyear, I have to increase the array size to 14. This part of the code did not work right, check for months[1] = 29; if it is leapyear, else months[1]=28; if not leapyear.
CODE

{
    lp_ck = yyyy%4;
    if(lp_ck==0)
    //if(leapyear(year))
     {
      months[1]=29;
      cout << yyyy << " is a leap year.\n";
     }
    else
     {
      months[1]=28;
      cout << yyyy << " is not a leap year\n";
     }
}


This part is just to check to see if it is leapyear then display month = 29 instaed of 28 and display the date for leapyear, help.

This post has been edited by KMH: 13 Apr, 2008 - 04:33 PM
User is offlineProfile CardPM
+Quote Post

KMH
RE: Need Help Leap Year
13 Apr, 2008 - 09:05 PM
Post #8

D.I.C Head
**

Joined: 23 Mar, 2008
Posts: 80

Ok no2pencil,

I can't imagine how that lady in red she probably has a very bad headache and big black eye. Anyway I got the code working now, but I don't feel right the fact that I have to increase the number of my array element to cover dd = 29 entered for February month. Perhaps this line in the code did work.
[array]
int months[12]= {31,28,31,30,31,30,31,31,30,31,30,31}; >>change to>>int months[14]= {31,28,31,30,31,30,31,31,30,31,30,31};
[a/rray]
[line]
if( dd < 1 || dd > months[dd-1] )// Validate to see if input for day is valid
[/line]

Here is the completed working code: dry.gif
CODE

#include <iostream>
#include <string>
#include <math.h>

using namespace std;

//Declare the function
bool leapyear(int year);
    
struct // struct declaration
{
    int Month;
    int Day;
    int Year;
    
}date;

int main()
{
                              
    // Intialize Array For Number Of Days In Each Month
    int months[14]= {31,28,31,30,31,30,31,31,30,31,30,31}; //Array For Number Of Days In Each Month
    //Initialize array for number of month in the year
    const unsigned int string_month = 12;
    string arr[string_month] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
                                                                              
    int mm, dd, yyyy, year = 0;
    
    cout << "This program will display date and convert number of month to string" << endl;
    //Prompts user for input
    cout << "Enter month in the form of mm: ";
    cin >> mm;//Get month from input
  if( mm < 1 || mm > 12 )// Validate to see if input for month is valid
         {
         cout << "INVALID month: Please try again.\n" << endl;
         //cin.get();
         system ("pause");
         exit (1);
         }
    cout << "Enter day in the form of dd: ";
    cin >> dd;//Get day from input
  if( dd < 1 || dd > months[dd-1] )// Validate to see if input for day is valid for the month
         {
         cout << "INVALID day: Please try again.\n" << endl;
         cin.get();
         system ("pause");
         exit (1);
         }
    cout << "Enter year in the form of yyyy: ";
    cin >> yyyy;//Get year from input
    cin.get();
    year = yyyy;
    cout << "You have entered: " << mm << "-" << dd << "-" << yyyy << " which " << endl;
          
if(leapyear(year) && mm == 2 && dd > months[1] + 1)// Check to see if leapyear or not
       cout << "Invalid # of days for the month of (" << arr[mm - 1] << ") in the year of (" << yyyy << ").\n";
    else if( dd > months[1] )
       cout << "Invalid # of days for the month of (" << arr[mm - 1] << ") in the year of (" << yyyy <<").\n";
    else if(leapyear(year)&& mm == 2)
      {
      months[1] = 29;
      cout << yyyy << " is a leap year.\n";
      }
    else
      {
      months[1] = 28;
      cout << yyyy << " is not a leap year\n";
      }            
            date.Year = yyyy;
            date.Month = mm;
            date.Day = dd;
            
cin.get();
return 0;
}

bool leapyear(int year)
  {
    return ((year % 4) == 0);
  }


User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 07:39PM

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