Hi!!! I need some help on a project I'm that calculates leap years; I'm
getting errors that I have no idea what they mean; here's the code:
#include <iostream>
#include <cstdlib>
using namespace std;
class Year
{
public:
Year(int y=0):year(new int) *year(y){}
~Year(){delete year;}
Year(const Year& rhs)*year(rhs.* year){}
Year operator=(const Year& rhs)
{
if(this!=&rhs)
{
*year=rhs->year;
delete year;
}
else return this;
}
void SetYear(int y):year(new int),*year(y){}
int GetYear()const{ return *year;}
bool IsLeapYear(cons t Year& y)const
{
if(y->year%400==0& &y->year%4==0)
return true;
else if(y->year%100)
return false;
else return false
}
friend ostream& operator<<(ostr eam& os,const Year& y);
private:
int* year;
};
ostream& operator<<(ostr eam& os,const Year& y)
{
os << y.GetYear();
return os;
}
}
int main()
{
int date;
for(;;)
{
cout << "Enter a year: " << endl;
cin >> date;
Year year(date);
if(year.IsLeapY ear)
cout << year << " is a leap year. " << endl;
else
cout << year << " is not a leap year. " << endl;
bool cont;
cout << "Continue?[1=yes|0=no]: " << endl;
cin >> cont;
if(cont==true)c ontinue;else break;
system("PAUSE") ;
return 0;
}
And as a side note, it's not letting me use constructor inizilization
syntax. Can you help me (this isn't homework)? Thanks for your help!!! 14 4762
And I'm sure this _is_ a school work ;>
No it's not; my friend, Jonathon Mastron at BIOLA, asked me to it for
him. My school work is trig stuff, not calendars and dates. Just help
me please.
OK, I fixed it; it compiles and runs now; here's the code:
#include <iostream>
#include <cstdlib>
using namespace std;
class Year
{
public:
Year(int y=0): year(y){}
~Year(){}
Year(const Year& rhs):year(rhs.y ear){}
Year operator=(const Year& rhs)
{
if(this!=&rhs)
year=rhs.year;
return *this;
}
void SetYear(int y){year=y;}
int GetYear(void)co nst{return year;}
bool IsLeapYear(void )const
{
if(year%400==0& &year%4==0)
return true;
else if(year%100)
return false;
else return false;
}
friend ostream& operator<<(ostr eam& os,const Year& y);
private:
int year;
};
ostream& operator<<(ostr eam& os,const Year& y)
{
os << y.GetYear();
return os;
}
int main()
{
int date;
for(;;)
{
cout << "Enter a year: " << endl;
cin >> date;
Year year(date);
if(year.IsLeapY ear())
cout << year << " is a leap year. " << endl;
else
cout << year << " is not a leap year. " << endl;
bool cont;
cout << "Continue?[1=yes|0=no]: " << endl;
cin >> cont;
if(cont==true)c ontinue;else break;
}
system("PAUSE") ;
return 0;
}
Anyway I can make it more efficient? And other than calculating leap
years, what other uses can I have for this class? And what other ops
should I overload? Thanks for the help!!!
I'm trying to overload operator>>, but my program keeps aborting;
here's the code for operator>>:
istream& operator>>(istr eam& is,const Year& y)
{
is >> y.year;
return is;
}
Why does it keep aborting?
On 18 Sep 2005 15:35:16 -0700, "Protoman" <Pr**********@g mail.com> wrote: I'm trying to overload operator>>, but my program keeps aborting; here's the code for operator>>:
istream& operator>>(istr eam& is,const Year& y) { is >> y.year;
You are trying to modify a const value. In fact, the const qualifier is
not appropriate for this kind of function.
return is; }
Why does it keep aborting?
If it aborts at that region, then perhaps there might be a logic error in
your code. Best to find some form of debugger.
to encapsulate, you should really make the leap year identification a
non-friend, non-member, standalone function.
like:
bool is_leapyear(Yea r year){/*...*/}
ben
Protoman wrote: OK, I fixed it; it compiles and runs now; here's the code: .. bool IsLeapYear(void )const { if(year%400==0& &year%4==0) return true; else if(year%100) return false; else return false; }
This function is clearly not correct. It will return false for nearly
every year that is a leap year. The first clue that something is amiss,
is the fact that it tests whether a year is divisible by 4 after it
knows it is divisible by 400. Anyway, here is a corrected version:
bool IsLeapYear() const
{
if ( year%4 ) // not divisible by 4
return false;
if ( year%100 ) // not a century year
return true;
if ( year%400 ) // not a fourth century
return false;
return true;
}
Also, I would work on writing more readable code. The layout of the
code in the original program is atrocious. Do spaces cost extra?
Greg This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Se'noj |
last post by:
Hi all,
In a program I am writing, I need to convert a date stored as an
integer number of days into a standard gregorian format, with day zero
being 1/1/1800. Can anyone help me with this?
Bryan Jones
NightDaemon Enterprises
nightdaemon.co.nr
|
by: vgame64 |
last post by:
Hi, I have been struggling with writing a program for a few hours. The
requirements are that: """You will be writing a program which will
determine whether a date is valid in
terms of days in that month. We are assuming that the year will be
valid 4 digit integer.
So you don't have to think much about that(in terms of validation)
except for the month
of February. If the month is February, then you have to check whether
that year is Leap...
|
by: deepak.rathore |
last post by:
can someone give the regular expr. to validate leap yr like
02/29/2000,02/29/00
Thanks
|
by: rn216_ccc |
last post by:
Hi there all,
I found a web site,. http://www.onlineconversion.com/leapyear.htm,
where it can give a list of leap years by within the given range by
the user, or the user may enter a year and the program will make the
decision if it is a leap year or not. In addition, I kind of
understand that there are certain rules to calculating leap years. For
instance, if the year is divisable by 4 and if the year is not
divisable by 100 but is...
|
by: xGx |
last post by:
Hi please could someone help. i have created this programming so you can work out if there is a LEAP YEAR. But i need it to handle centuries. heres the code
def isLeapYear (year):
result = year % 4
if result > 0:
print "It is NOT a leap year"
else:
print "It IS a leap year"
| |
by: Brigitte Behrmann |
last post by:
Can anyone assist with my code. I just cannot get this to work:
I have to write a script that allows the user to enter a year & then determine whether it is a leap year or not. The request is for a form with a single text box and using an alert dialog box stating if the entered year is a standard or leap year.
Code so far:
<HTML>
<HEAD>
<TITLE>Is it a leap year?</TITLE>
</HEAD>
<SCRIPT LANGUAGE="JAVASCRIPT>
|
by: mazwolfe |
last post by:
I'm new here, so excuse me if my style is incorrect. Can anyone come
up with a better method for this calculation?
Code:
int is_leap(int year)
{
switch (year % 19) {
case 0: case 3: case 6: case 8:
case 11: case 14: case 17: return 1;
default: return 0;
|
by: jimix |
last post by:
here's what i have. using microsoft studio
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
int main( void )
{
int year, b, c, e;
b = 4;
c = 100;
|
by: gubbachchi |
last post by:
Hi all,
In my application I need to display the data fetched from mysql database after the user selects date from javascript calender. I have written the code in which after the user selects the date from calender and clicks search button it will fetch data from database. But what I need is as soon the user clicks the date, it should fetch data for that date from database. How to do it. Here is the code I am using
<html>
<script...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |