/*****************************************************
M
6.3 LeapYear.cpp
T
*****************************************************/
/* The program LeapYear.cpp prompts the user to input a year, then
calculates and displays a message if the year is a leap year.
The program utilizes user-defined functions.*/
/* A year and a character for a loop to determine if the program continues. */
/* A message displaying if the date is a leap year. */
//header files
/* use the correct preprocessor directives for input/output */
#include <iostream>
#include <string>
usingnamespace std;
bool isLeap();
// user-defined function prototypes
/* the function getYear returns an integer value
entered by the user */
int getYear(string message);
/* the function isLeap returns a Boolean value
after processing the value passed to the
function */
int isLeap(string message);
/* the function moreData returns a character
value entered by the user */
char moreData(string message);
int main()
{
// declare variables
/* declare a integer variable year to enter a year,
a Boolean variable leapYear to indicate whether
or not a year is a leap year, and a character
variable again to indicate if the program
should continue */
int year;
int leapYear;
char again;
// instruct the user the purpose of the program
cout << " This program will ask the user to enter a year and then determines whether it is a leap year. \n";
// loop until the user chooses to quit
while (again == 'y')
{
// assign to year the value returned from getYear
year = getYear(" Enter a 4 digit year you would like to check : ");
/* assign to leapYear the value returned from isLeap
with year as the argument */
leapYear = isLeap(" It is a leap.");
/* if leapYear is true
if (leapYear)
display that the year is a leap year
cout << " This is a leap year. " ;
else
display that the year is not a leap year */
cout << " This is not a leap year.";
/* assign to again the character returned from
moreData converted to lowercase */
again = tolower(again);
}
return 0;
/* the function getYear returns an integer value
entered by the user */
int getYear(string message);
{
// declare an integer variable year to enter a year
// prompt the user to enter a year
cout << " Enter a year : ";
// read in the value entered
cin >> year;
// return the value entered
return year;
}
/* the function isLeap returns a Boolean value
after processing the value passed to the
function */
bool isLeap(string message);
// if year is divisible by 400 it is a leap year
/* if year is divisible by 4 but not by 100
it is a leap year */
// otherwise the year is not a leap year
{
if ((year % 4 == 0) && (!(year % 100 == 0)))
cout << "It is a leap year!" << endl;
elseif (year % 400 == 0)
cout <<" It is a leap year";
return year;
}
/* the function moreData returns a character
value entered by the user */
char moreData(string message);
{
// declare variables
/* declare a character variable letter to
enter the user's response */
char letter;
// prompt the user if more data is to be entered
cout << "message";
// read in user's response
cin >> letter;
// return user's response
return letter;
}
}
The error i received is "Functions that differ only in their return type cannot be overloaded"
I have no clue in how to go about fixing that error. Please help
The first thing I see is that you seem to be trying to implement your functions inside main(), this is not allowed.
Next your isLeap() function prototype doesn't match your function implementation. You also have two different isLeap() prototypes, but only one isLeap() function implementation.
The first thing I see is that you seem to be trying to implement your functions inside main(), this is not allowed.
Next your isLeap() function prototype doesn't match your function implementation. You also have two different isLeap() prototypes, but only one isLeap() function implementation.
I got rid of the functions in the main. But im confused on the last part. What exactly do i have to change?
Oh ok i see what you mean. I changed it to match but now it says "Code will never be executed" For these lines of code
1 2 3 4 5 6 7 8
{
if ((year % 4 == 0) && !(year % 100 == 0))
cout << "It is a leap year";
elseif (year % 400 == 0)
cout <<" It is a leap year";
return year;
}
1 2 3 4 5 6 7 8 9 10 11 12
{
// declare variables
/* declare a character variable letter to
enter the user's response */
char letter;
// prompt the user if more data is to be entered
cout << "message";
// read in user's response
cin >> letter;
// return user's response
return letter;
}
1 2 3 4 5 6 7 8
{
// declare an integer variable year to enter a year
// prompt the user to enter a year
cout << " Enter a year : ";
// read in the value entered
cin >> year;
// return the value entered
return year;
I assumed maybe it was the ";" after each function definition but it doesnt help
// Enter your name as a comment for program identification
// Program assignment LeapYear.cpp
// Enter your class section, and time
/*****************************************************
M
6.3 LeapYear.cpp
T
*****************************************************/
/* The program LeapYear.cpp prompts the user to input a year, then
calculates and displays a message if the year is a leap year.
The program utilizes user-defined functions.*/
/* A year and a character for a loop to determine if the program continues. */
/* A message displaying if the date is a leap year. */
//header files
/* use the correct preprocessor directives for input/output */
#include <iostream>
#include <string>
usingnamespace std;
// user-defined function prototypes
/* the function getYear returns an integer value
entered by the user */
int getYear(string message);
/* the function isLeap returns a Boolean value
after processing the value passed to the
function */
bool isLeap(string message);
/* the function moreData returns a character
value entered by the user */
char moreData(string message);
int main()
{
// declare variables
/* declare a integer variable year to enter a year,
a Boolean variable leapYear to indicate whether
or not a year is a leap year, and a character
variable again to indicate if the program
should continue */
int year, leapYear;
char again;
// instruct the user the purpose of the program
cout << " This program will ask the user to enter a year and then determines whether it is a leap year. \n";
// loop until the user chooses to quit
while (again == 'y')
{
// assign to year the value returned from getYear
year = getYear(" Enter a year : ");
/* assign to leapYear the value returned from isLeap
with year as the argument */
leapYear = isLeap(" It is a leap.");
/* if leapYear is true
if (leapYear)
display that the year is a leap year
cout << “ This is a leap year. “;
else
display that the year is not a leap year */
cout << " This is not a leap year.";
/* assign to again the character returned from
moreData converted to lowercase */
again = tolower(again);
}
return 0;
/* the function getYear returns an integer value
entered by the user */
int getYear(string message);
{
// declare an integer variable year to enter a year
// prompt the user to enter a year
cout << " Enter a year : ";
// read in the value entered
cin >> year;
// return the value entered
return year;
}
/* the function isLeap returns a Boolean value
after processing the value passed to the
function */
bool isLeap(string message);
// if year is divisible by 400 it is a leap year
/* if year is divisible by 4 but not by 100
it is a leap year */
// otherwise the year is not a leap year
{
if ((year % 4 == 0) && !(year % 100 == 0))
cout << "It is a leap year";
elseif (year % 400 == 0)
cout <<" It is a leap year";
return year;
}
/* the function moreData returns a character
value entered by the user */
char moreData(string message);
{
// declare variables
/* declare a character variable letter to
enter the user's response */
char letter;
// prompt the user if more data is to be entered
cout << "message";
// read in user's response
cin >> letter;
// return user's response
return letter;
}
}
That's the problem, it shouldn't be there it should be much earlier, perhaps somewhere before your function implementations? Your functions must be implemented outside any other function, including main()!
/* The program LeapYear.cpp prompts the user to input a year, then
calculates and displays a message if the year is a leap year.
The program utilizes user-defined functions.*/
/* A year and a character for a loop to determine if the program continues. */
/* A message displaying if the date is a leap year. */
//header files
/* use the correct preprocessor directives for input/output */
#include <iostream>
#include <string>
usingnamespace std;
// user-defined function prototypes
/* the function getYear returns an integer value
entered by the user */
int getYear(string message);
/* the function isLeap returns a Boolean value
after processing the value passed to the
function */
bool isLeap(string message);
/* the function moreData returns a character
value entered by the user */
char moreData(string message);
int main()
{
// declare variables
/* declare a integer variable year to enter a year,
a Boolean variable leapYear to indicate whether
or not a year is a leap year, and a character
variable again to indicate if the program
should continue */
int year, leapYear;
char again;
// instruct the user the purpose of the program
cout << " This program will ask the user to enter a year and then determines whether it is a leap year. \n";
// loop until the user chooses to quit
while (again == 'y')
{
// assign to year the value returned from getYear
year = getYear(" Enter a year : ");
/* assign to leapYear the value returned from isLeap
with year as the argument */
leapYear = isLeap(" It is a leap.");
/* if leapYear is true
if (leapYear)
display that the year is a leap year
cout << “ This is a leap year. “;
else
display that the year is not a leap year */
cout << " This is not a leap year.";
/* assign to again the character returned from
moreData converted to lowercase */
again = tolower(again);
}
return 0;
}
/* the function getYear returns an integer value
entered by the user */
int getYear(string message);
{
// declare an integer variable year to enter a year
// prompt the user to enter a year
cout << " Enter a year : ";
// read in the value entered
cin >> year;
// return the value entered
return year;
}
/* the function isLeap returns a Boolean value
after processing the value passed to the
function */
bool isLeap(string message);
// if year is divisible by 400 it is a leap year
/* if year is divisible by 4 but not by 100
it is a leap year */
// otherwise the year is not a leap year
{
if ((year % 4 == 0) && !(year % 100 == 0))
cout << "It is a leap year";
elseif (year % 400 == 0)
cout <<" It is a leap year";
return year;
}
/* the function moreData returns a character
value entered by the user */
char moreData(string message);
{
// declare variables
/* declare a character variable letter to
enter the user's response */
char letter;
// prompt the user if more data is to be entered
cout << "message";
// read in user's response
cin >> letter;
// return user's response
return letter;
}
// Enter your name as a comment for program identification
// Program assignment LeapYear.cpp
// Enter your class section, and time
/*****************************************************
M
6.3 LeapYear.cpp
T
*****************************************************/
/* The program LeapYear.cpp prompts the user to input a year, then
calculates and displays a message if the year is a leap year.
The program utilizes user-defined functions.*/
/* A year and a character for a loop to determine if the program continues. */
/* A message displaying if the date is a leap year. */
//header files
/* use the correct preprocessor directives for input/output */
#include <iostream>
#include <string>
usingnamespace std;
// user-defined function prototypes
/* the function getYear returns an integer value
entered by the user */
int getYear(string message);
/* the function isLeap returns a Boolean value
after processing the value passed to the
function */
bool isLeap(string message);
/* the function moreData returns a character
value entered by the user */
char moreData(string message);
int main()
{
// declare variables
/* declare a integer variable year to enter a year,
a Boolean variable leapYear to indicate whether
or not a year is a leap year, and a character
variable again to indicate if the program
should continue */
int year, leapYear;
char again;
// instruct the user the purpose of the program
cout << " This program will ask the user to enter a year and then determines whether it is a leap year. \n";
// loop until the user chooses to quit
while (again == 'y')
{
// assign to year the value returned from getYear
year = getYear(" Enter a year : ");
/* assign to leapYear the value returned from isLeap
with year as the argument */
leapYear = isLeap(" It is a leap.");
/* if leapYear is true
if (leapYear)
display that the year is a leap year
cout << “ This is a leap year. “;
else
display that the year is not a leap year */
cout << " This is not a leap year.";
/* assign to again the character returned from
moreData converted to lowercase */
again = tolower(again);
}
return 0;
}
/* the function getYear returns an integer value
entered by the user */
int getYear(string message)
{
// declare an integer variable year to enter a year
// prompt the user to enter a year
cout << " Enter a year : ";
// read in the value entered
cin >> year;
// return the value entered
return year;
}
/* the function isLeap returns a Boolean value
after processing the value passed to the
function */
bool isLeap(string message)
// if year is divisible by 400 it is a leap year
/* if year is divisible by 4 but not by 100
it is a leap year */
// otherwise the year is not a leap year
{
if ((year % 4 == 0) && !(year % 100 == 0))
cout << "It is a leap year";
elseif (year % 400 == 0)
cout <<" It is a leap year";
return year;
}
/* the function moreData returns a character
value entered by the user */
char moreData(string message)
{
// declare variables
/* declare a character variable letter to
enter the user's response */
char letter;
// prompt the user if more data is to be entered
cout << "message";
// read in user's response
cin >> letter;
// return user's response
return letter;
}