Help writing a program...

I need to write this program for class... will someone help me write it or as lease give me a guideline on the structure. Thanks

Leap year?

You have been asked to construct a program, making extensive use of functions, that will determine if a given year is a leap year. You will need to prompt the user to enter a year and then determine if it is a leap year. The
program should check to ensure that a valid year has been entered. Since the modern (Gregorian) calendar was
accepted in 1582, we would like to warn the user that the prediction of leap years before this date will be
inaccurate. We can determine if a year is a leap year based on the rules:

A year is a leap year if it is divisible by 4, but it is not a leap year if it is also divisible by 100, except when it is also divisble by 400 (The year 2000 was a leap year).

====================================================================================================================

Deliverables:
Write a program using good style, indents, descriptive variable names, comments, etc..
Greet the user and provide a few lines about what the program is/does.
Prompt the user to enter a year and then determine if the year falls after the acceptance of the modern calendar.
If the year is before 1582, warn the user that the results may not be accurate.
Determine if the year is a leap year and output a message that it is or is not.
The program should have a loop and continue while the user enters the ‘y’ or 'Y' characters.
The program should exit with a parting message.

====================================================================================================================

You should write three functions according to the following descriptions:

The function GetYear should have no parameters, ask the user to enter a year, and return an integer value (the year).

The function IsLeap should have an integer parameter (the year), and should validate the year and determine whether the year is a leap year or not. It should return the Boolean value true if the year is a leap year and false if it
is not a leap year. A year is a leap year if it is divisible by 4, but is not divisible by 100 except when it is
divisble by 400.

The function MoreData should have no parameters, prompt the user to continue and returns a Boolean value based on the user's input.

====================================================================================================================

Example Output:

---------- Leap-year-o-matic ----------
This program will determine if a year you enter is a leap year.
Note: Years before the modern calendar adoption in 1582 may have inaccurate results.

Enter a year: 1492
Warning: 1492 is before the modern calender adoption in 1582, results may be inaccurate!
1492 is a leap year.

Do you want to enter more data? (y/n) y

Enter a year: 2000
2000 is a leap year.

Do you want to enter more data? (y/n) Y

Enter a year: 2005
2005 is not a leap year.


Do you want to enter more data? (y/n) y

Enter a year: 1800
1800 is not a leap year.

Do you want to enter more data? (y/n) n

Thank you for using Leap-year-o-matic!
this is what I have so far, can someone help me from here?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int GetYear();
bool IsLeap(int);
bool MoreData();

void main ()
{
	GetYear();

	
	system("pause");
}

int GetYear()
{
	int year;
	cout << "Please Enter a Year (1582 or above)" << endl;
	cin >> year;
	if (year <= 1581)
		cout << "Note: Years before the modern calendar adoption in 1582 may have inaccurate results." << endl;
}

bool IsLeap(int year)
{
	if( year%4 == 0 && year%100 == !0 )
return 1;
	else if (year%100 == 0 && year%400 == 0) 
return 1;
	else 
return 0;

}


bool MoreData()
{
	int answer;
	cout << "Do you want to enter more data? (y/n)";
	cin >> answer;
		if (answer == 'y' || answer == 'Y')
			return 1;
		if (answer == 'n' || answer == 'N')
			return 0;
		else
			cout << "Please enter a valid response"; 

}
Do you have anything right now? it didnt take me more than 15 minutes to write. I'd post the code but you wouldnt learn anything, ill post a structure of the program i wrote to hopefully give you some guidelines. Just post what you dont understand

Here is a shell:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
//PROTOTYPE THE FUNCTIONS
short GetYear(); //you can use int instead
bool IsLeap(short); 
bool MoreDate(); 
int main()
{
    //declare year variable
    //print out the opening text on the screen

    //start a DO WHILE loop, because you want the loop to run at least ONCE
        //set the variable equal to the getyear function you created
        //if function returns true
		//print out its true!
	//if function returns false
		//print out its false!
    //repeat while moredate function returns true
    //place ending message here after loop finishes
}
short GetYear() //or int
{
    //declare temp variable
    //ask user to enter year
    //read in variable	
    //if the variable is below 1582
	//print out a warning to the user
    //return temp variable
}

bool IsLeap(short year)
{
    //if the year is divisible by 100
	//if year is divisible by 400(the exception)
		//return true
	//otherwise return false
    //this narrows out all of the years divisible by 100, if the function
    //reaches this point the number is not divisible by 100, so now worries.
    //if the year is divisible by 4
	//leap year!
    //otherwise return false
}

bool MoreDate()
{
    //create temp variable(youll want a char)
    //display promt asking for input if they wish to play again
    //if you've learned the toupper function, set the char to uppercase
    //using the function
    //case statement using temp
	//is 'Y'?
		//return true(loop continues to run)
	//is 'N'?
		//return false(loop terminates)
	//neither?
		//tell user invalid input and terminate loop
    //end case
}
Last edited on
i dont think you program called IsLeap(year), its just there also MoreData can just be a loop to re run the program where n or N would be a break out of the loop
while you were writing that post, I posted what I had written so far, I am sure there are errors in the logic...
also you declared answer an integer when i think you meant char
Following on from rro0035, you need to sort out your bools!

A bool var is either true or false. When cast to an int is does end up as 1 and 0, but you should stick to the correct key words. For example:

1
2
3
4
5
6
7
8
9
bool IsLeap(int year)
{
	if( year%4 == 0 && year%100 == !0 )
return true; // not 1
	else if (year%100 == 0 && year%400 == 0) 
return true; // not 1
	else 
return false; // not 0
}

Last edited on
You don't have to include <ctime> since your program won't be comparing the input with todays' date or anything like that. (unless you want to be able to say something like "That leapday was 40 years ago", or "It will be 24 years before that leapday")

you should place isleap() and getyear() and moredata() in a loop in main that ends if moredata() returns 0;

moredata() will need its own loop since you want that bit of data to repeat if they give a non-answer. This loop should end if answer=="n"||answer=="y"

rro0035 also mentioned above that whole int and char thing, it also needs to be changed but I'm hesitant to give you the "answer" since debugging is part of the learning curve.

also my compiler always throws an error if main() returns void, I'm guessing your compiler just ignores this.

That's all the hints I can give since I'm away from my compiler at the moment, good luck.
Last edited on
ok im trying to progress... I have a problem getting my syntax perfect and remember exactly how to lay things out... any more help would be great, ill continue by myself as well...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "stdafx.h"
#include <iostream>

using namespace std;

int GetYear();
bool IsLeap(int);
bool MoreData();

void main ()
{
	int year = 0;
	char answer;

	cout << "\t\t\t --------------Leap-Year-O-Matic--------------" << endl;
	cout << "This program will determine if a year you enter is a leap year." << endl;
	cout << "Note: Years before the modern calendar adoption in 1582 may have inaccurate results." << endl;

	do
	
	{
	
	GetYear();
	

	}
	
	while(MoreData()==true);
	
	system("pause");
}

int GetYear()
{
	int year;
	cout << "Please Enter a Year (1582 or above)" << endl;
	cin >> year;
	if (year <= 1581)
		cout << "Note: Years before the modern calendar adoption in 1582 may have inaccurate results." << endl;
}

bool IsLeap(int year)

{

if( year%100 == 0 && year%400 == !0 )
return true;
	else
return false;

}


bool MoreData()
{
	char answer;
	cout << "Do you want to enter more data? (y/n)";
	cin >> answer;
		if (answer == 'y' || answer == 'Y')
			return true;
		if (answer == 'n' || answer == 'N')
			return false;
		else
			cout << "Please enter a valid response"; 

}
i've actually gotten the program to run now!!! YAY!!! the only thing I have left to figure out, is the IsLeap Function...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "stdafx.h"
#include <iostream>

using namespace std;

int GetYear();
bool IsLeap(int);
bool MoreData();

void main ()
{
	int year = 0;

	cout << "\t\t\t --------------Leap-Year-O-Matic--------------" << endl;
	cout << "This program will determine if a year you enter is a leap year." << endl;
	cout << "Note: Years before the modern calendar adoption in 1582 may have inaccurate results." << endl;

	do
	
	{
	
	year = GetYear();
	IsLeap(year);
	

	}
	
	while(MoreData()==true);
	
	system("pause");
}

int GetYear()
{
	int year;
	cout << "Please Enter a Year (1582 or above)" << endl;
	cin >> year;
	if (year <= 1581)
		cout << "Note: Years before the modern calendar adoption in 1582 may have inaccurate results." << endl;
	return year;
}

bool IsLeap(int year)

{

if( year%100 == 0 && year%400 == !0 )
return true;
	else
return false;

}


bool MoreData()
{
	char ans;
	cout << "Do you want to enter more data? (y/n)";
	cin >> ans;
		if (ans == 'y' || ans == 'Y')
			return true;
		if (ans == 'n' || ans == 'N')
			return false;
		else
			cout << "Please enter a valid response"; 

}
did you take a look at the commented code i wrote above? it explains how to write the function, its just about translating the logic into computer code.
its different than yours for some reason i cant use void main() also not sure what library stdafx.h is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
using namespace std;
bool IsLeap(int);

int main()
{
  char MoreData='y';
  unsigned int year=0;
  while (MoreData=='y'){
    cout<<"whats the year?"<<endl;
    cin>>year;
    if (IsLeap(year)==true){
    cout<<year<<" Is a leap year!"<<endl;
    cout<<"continue?"<<endl;
    cin>>MoreData;
      if (MoreData=='n')
        break;
   }
    else if (IsLeap(year)==false)
    {
    cout<<year<<" is not a leap year"<<endl;
    cout<<"Continue?"<<endl;
    cin>>MoreData;
       if (MoreData=='n')
         break;
    }

  }
}
bool IsLeap(int year)
{
if(year%400 ==0 || (year%100 != 0 && year%4 == 0))
return true;
	else
return false;
}
for some reason i cant use void main()

The reason is that void main() is not C++.

also not sure what library stdafx.h

It's a non-standard header used commonly in Visual Studio, to do with precompiled headers (you can ignore it). Most people who use it don't know what it is either.
Topic archived. No new replies allowed.