473,322 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Write a program that reads a string from the keyboard and tests whether it contains a

i got problem of this question :

The input date will have the format mm/dd/yyyy. A valid month value mm must be from 1 to 12 (January is 1). The day value dd must be from 1 to a value that is appropriate for the given month. February has 28 days except for leap years when it has 29. A leap year is any year that is divisible by 4 but not divisible by 100 unless it is also divisible by 400.
Aug 30 '16 #1
3 2647
weaknessforcats
9,208 Expert Mod 8TB
What don't you understand about the question?
Aug 31 '16 #2
#include <iostream>
using namespace std;

int main()
{
int day, month, year;


cout << "Enter the day :";
cin >> day;
cout << "Enter the month :";
cin >> month;
cout << "Enter the year :";
cin >> year;

if (year % 4 == 0 || year % 100 != 0 || year % 400 == 0)
{
cout << "It`s valid date" <<endl;
}
else(year % 4 !=0 || year % 100 =0 ||year % 400 ==0);
{

cout << "It`s not valid date" <<endl;
}
return 0;
}
i got code like this but when i run it. it doesn`t work
Aug 31 '16 #3
weaknessforcats
9,208 Expert Mod 8TB
This code:
Expand|Select|Wrap|Line Numbers
  1. if (year % 4 == 0 || year % 100 != 0 || year % 400 == 0)
  2.  {etc...
says that if the year is divisible by 4 OR the year is not divisible by 100 OR the year is divisible by 400, then the year is a leap year.

The way the compiler codes this is to stop evaluating the IF when the expression becomes true.

In this case any year divisible by 4 will be a valid leap year. So 2012 is a leap year.

Then look at 2011. Not divisible by 4 so the evaluation of the IF continues to OR the year not divisible by 100. 2011 is not divisible by 100 so it is also a leap year.

Not good.

You have to check the year this way:

1) is the year divisible by 4? If yes go to step 2. Otherwise, the year is not a leap year.

2) is the year from step 1 divisible by 100. If not, the year is a leap year. If divisible by 100, go to step 3.

3) Is the year from step 2 divisible by 400? If it is, the year is a leap year. Otherwise it is not.

Post again if you need more help.
Aug 31 '16 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Jesper | last post by:
Hi What is the easiest way to write out a string to a printer than Jesper.
2
by: feng | last post by:
I think I didn't phrase my quetion clear enough in the last post. Here is what I need: In my VB.Net code, I already have a XML created in XMLDocument formate. I can also convert it into a string...
0
by: mimi | last post by:
#include <fstream> #include "stdio.h" int main() { // std::wofstream strm2("test.txt"); // strm2 << L"ÎÒ°®±±¾©Ìì°²ÃÅ"; FILE *hFile = fopen("test.txt", "wb+"); wchar_t *buffer =...
2
by: CuteBeginner | last post by:
I need to write a program that reads a text file, which contains words that are followed with a preposition. If the preposition is wrong for the word, the program is supposed to warn the reader and...
3
chandru8
by: chandru8 | last post by:
Hi everybody Can anyone suggest me where to write the connection string because am writing the connection string in each form load closing it in form_unload the problem is when i want to...
4
by: Samuel | last post by:
Hi I am trying to write to a string text encoded to utf8 as oppose to utf16 Since the data comes from an XML object (and I serialize it) I need to pass either StreamWriter or a StringWriter...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.