| | |
leap year program
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2008
Posts: 35
Reputation:
Solved Threads: 0
Hello Everyone,
I'm new to the forum, currentely I'm taking the basic C++ class and struggle through, it has been over 20 years since write any codes. I have some trouble with the code below, it work fine if I don't bother to check for leap year, but the assignmnet required check the for leap year divisible by 4 then day range from 1-29 and year is February. I put the part for leapyear checker, then it did not work, can anyone help see what I did wrong?? Part that I highlighted has been added then the code crash, please help.
I'm new to the forum, currentely I'm taking the basic C++ class and struggle through, it has been over 20 years since write any codes. I have some trouble with the code below, it work fine if I don't bother to check for leap year, but the assignmnet required check the for leap year divisible by 4 then day range from 1-29 and year is February. I put the part for leapyear checker, then it did not work, can anyone help see what I did wrong?? Part that I highlighted has been added then the code crash, please help.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <math.h> using namespace std; //Declare the function bool leapyear(int lpyr); 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,29,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, lpyr = 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); } else if( dd < 1 || dd > 31 )// Validate to see if input for day is valid { cout << "INVALID day: Please try again." << endl; cin.get(); exit (1); } else cout << "You have entered: " << mm << "-" << dd << "-" << yyyy << " which " << endl; /*{ [COLOR="Green"]if(lpyr % 4 != 0) return false; else if(lpyr % 100 != 0) return true; else if(lpyr % 400 != 0) return false; else return true;[/COLOR] }*/ dd = dd + 1;// Increase the number of days in feb to 29 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; }
Last edited by Ancient Dragon; Apr 12th, 2008 at 9:22 am. Reason: corrected code tags and add line numbers
Once you get the year. Look if the remainder when divided by 4 is zero.
if(yyyy%4=0)
{
then //code for leap year;
}
else{
//code for normal years.
Secondly.
Instead of checking between 1 to 31 . You should check for values between 1 and the number of days.
Something like this .
if( dd < 1 || dd > (mm-1) )
I guess this will do fine.
if(yyyy%4=0)
{
then //code for leap year;
}
else{
//code for normal years.
Secondly.
Instead of checking between 1 to 31 . You should check for values between 1 and the number of days.
Something like this .
if( dd < 1 || dd > (mm-1) )
I guess this will do fine.
line 47: get rid of the else. And check if dd is not greater than the number of days in the month, such as
line 65: delete it. Do not do that whether its a leap year or not. The only reason for checking for leap year is to determine whether the max days in Feb is 28 or 29.
You didn't post that leapyear() function which I guess is supposed to determine if the year is a leap year or not.
if( dd < 1 || dd > months[dd-1] ) line 65: delete it. Do not do that whether its a leap year or not. The only reason for checking for leap year is to determine whether the max days in Feb is 28 or 29.
You didn't post that leapyear() function which I guess is supposed to determine if the year is a leap year or not.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Apr 2008
Posts: 35
Reputation:
Solved Threads: 0
Hello Ancient Dragon,
Want to thank you very much for all your help, I was born in the year of dragon I guess I'm very slow, I have added the lines you have reconmended and so far so good. One warning related to "lpyr" :warning C4101: 'lpyr' : unreferenced local variable
Please tell me what do I need to do with leapyear() function, I know I need to do something with this function, I need to declare it or post it , but I do not know where in the code that I supost to do this.
Best Regards,
gazoo
Want to thank you very much for all your help, I was born in the year of dragon I guess I'm very slow, I have added the lines you have reconmended and so far so good. One warning related to "lpyr" :warning C4101: 'lpyr' : unreferenced local variable
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
//Declare the function
bool leapyear(int lpyr);
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, lpyr, lp_ck = 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);
}
lp_ck = yyyy%4; //Check If The Year Is A Leap Year Or Not
if(lp_ck=0)
months[1] = 29;
else
months[1] = 28; 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;
}Please tell me what do I need to do with leapyear() function, I know I need to do something with this function, I need to declare it or post it , but I do not know where in the code that I supost to do this.
Best Regards,
gazoo
•
•
•
•
line 47: get rid of the else. And check if dd is not greater than the number of days in the month, such asif( dd < 1 || dd > months[dd-1] )
line 65: delete it. Do not do that whether its a leap year or not. The only reason for checking for leap year is to determine whether the max days in Feb is 28 or 29.
You didn't post that leapyear() function which I guess is supposed to determine if the year is a leap year or not.
Last edited by gazoo; Apr 12th, 2008 at 10:04 pm.
First, the code you posted in red isn't needed.
C++ Syntax (Toggle Plain Text)
if( leapyear(year) ) cout << "is a leap year\n"; else cout << "is not a leap year\n"; bool leapyear( int year) { // its actually a little more complicated than this but will do for // years in the 20th and 21st centuries. return (year % 4) == 0 ? true : false; }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Apr 2008
Posts: 35
Reputation:
Solved Threads: 0
Hi Ancient Dragon;
I changes the line highlighted and I'm not sure if I need semicolon after the function :bool leapyear( int year) ??? it seem to complained :error C2143: syntax error : missing ';' before '<class-head>', maybe I have more then one return??. Please explain what is this line does: return (year % 4) == 0 ? true : false;. I understand return(year%4)==0, but nerver see the ( ? ) and ( true:fal; ) before. Thank in advance.
error C2143: syntax error : missing ';' before '<class-head>'
I changes the line highlighted and I'm not sure if I need semicolon after the function :bool leapyear( int year) ??? it seem to complained :error C2143: syntax error : missing ';' before '<class-head>', maybe I have more then one return??. Please explain what is this line does: return (year % 4) == 0 ? true : false;. I understand return(year%4)==0, but nerver see the ( ? ) and ( true:fal; ) before. Thank in advance.
#include <iostream> #include <string> #include <math.h> using namespace std; //Declare the function bool leapyear(int year) struct // struct declaration {#include <iostream> #include <string> #include <math.h> using namespace std; //Declare the function bool leapyear(int year) struct // struct declaration { <<<<<<<<<<<< error C2143: syntax error : missing ';' before '<class-head>' 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!"; return (year % 4) == 0 ? true : false; } cin.get(); return 0; } 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!"; return (year % 4) == 0 ? true : false; } cin.get(); return 0; }
error C2143: syntax error : missing ';' before '<class-head>'
•
•
•
•
First, the code you posted in red isn't needed.
if( leapyear(year) ) cout << "is a leap year\n"; else cout << "is not a leap year\n"; bool leapyear( int year) { // its actually a little more complicated than this but will do for // years in the 20th and 21st centuries. return (year % 4) == 0 ? true : false;}
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Apr 2008
Posts: 35
Reputation:
Solved Threads: 0
I see and very sorry,
Highlighted I have change the code as reconmended, I have error: [error]
error LNK2019: unresolved external symbol "bool __cdecl leapyear(int)" (?leapyear@@YA_NH@Z) referenced in function _main
[error]
Highlighted I have change the code as reconmended, I have error: [error]
error LNK2019: unresolved external symbol "bool __cdecl leapyear(int)" (?leapyear@@YA_NH@Z) referenced in function _main
[error]
#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!"; } cin.get(); return 0; }
![]() |
Other Threads in the C++ Forum
- Previous Thread: How can i save an object in the Server memory in CGI
- Next Thread: Help for comparing strings and copying strings please.
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






