Fix this code so that if a user inputs an invalid date, it outputs \"inalid date
ID: 3573011 • Letter: F
Question
Fix this code so that if a user inputs an invalid date, it outputs "inalid date" EX. 02-29-2001 is invalid
code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int reg[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int leap[12]={31,29,31,30,31,30,31,31,30,31,30,31};
int checkleapYear(int y) {
if(((y % 4 == 0) || (y % 400 == 0)) && (y % 100 != 0)) {
return 1;
}
else{
return 0;
}
}
void findDays(int m, int d, int y) {
int c = checkleapYear(y);
int tdays = 0;
int kk;
if(c == 1) {
for(kk = 0; kk < m - 1; kk++) {
tdays = tdays + leap[kk];
}
}
else {
for(kk = 0 ; kk < m - 1; kk++) {
tdays = tdays + reg[kk];
}
}
tdays = tdays + d;
printf(" There are %d days passed in the year %d", tdays, y);
}
void findDate(int tdays, int y) {
int m = 0;
int c = checkleapYear(y);
int kk = 0;
if(c == 1) {
while(tdays > leap[kk])
{
m = m + 1;
tdays = tdays - leap[kk];
kk++;
}
}
else {
while(tdays > reg[kk]) {
m = m + 1;
tdays = tdays - reg[kk];
kk++;
}
}
m += 1;
printf(" The date is %d-%d-%d", m,tdays,y);
}
int main() {
int c;
char c1,c2 = 'N';
char more = 'Y';
int tdays, d,m,y;
while (more == 'y' || more == 'Y') {
printf(" This program will find days passed or date in the year");
printf(" 1) Input date(mm/dd/yyyy) to find days passed");
printf(" 2) Input passed days to find date in the year");
printf(" Your choice (1/2):");
scanf("%d", &c);
switch(c){
case 1:
printf(" Please input data (mm-dd-yy):");
scanf("%d%c%d%c%d", &m, &c1, &d, &c1, &y);
findDays(m,d,y);
break;
case 2:
printf(" Input days:");
scanf("%d", &tdays);
printf(" Years:");
scanf("%d", &y);
findDate(tdays, y);
break;
default:
printf("INVALID");
}
printf (" Do more (Y/N) ? ");
scanf ("%S", &more);
}
return 0;
}
Explanation / Answer
PROGRAM CODE:
/*
* main.cpp
*
* Created on: 29-Nov-2016
* Author: kasturi
*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int reg[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int leap[12]={31,29,31,30,31,30,31,31,30,31,30,31};
int checkleapYear(int y) {
if(((y % 4 == 0) || (y % 400 == 0)) && (y % 100 != 0)) {
return 1;
}
else{
return 0;
}
}
void findDays(int m, int d, int y) {
int c = checkleapYear(y);
int tdays = 0;
int kk;
if(c == 1) {
if(d<=leap[m-1])
{
for(kk = 0; kk < m - 1; kk++) {
tdays = tdays + leap[kk];
}
}
else
{
printf(" Invalid date !");
return;
}
}
else {
if(d<=reg[m-1])
{
for(kk = 0 ; kk < m - 1; kk++) {
tdays = tdays + reg[kk];
}
}else
{
printf(" Invalid date !");
return;
}
}
tdays = tdays + d;
printf(" There are %d days passed in the year %d", tdays, y);
}
void findDate(int tdays, int y) {
int m = 0;
int c = checkleapYear(y);
int kk = 0;
if(c == 1) {
while(tdays > leap[kk])
{
m = m + 1;
tdays = tdays - leap[kk];
kk++;
}
}
else {
while(tdays > reg[kk]) {
m = m + 1;
tdays = tdays - reg[kk];
kk++;
}
}
m += 1;
printf(" The date is %d-%d-%d", m,tdays,y);
}
int main() {
int c;
char c1,c2 = 'N';
char more = 'Y';
int tdays, d,m,y;
while (more == 'y' || more == 'Y') {
printf(" This program will find days passed or date in the year");
printf(" 1) Input date(mm/dd/yyyy) to find days passed");
printf(" 2) Input passed days to find date in the year");
printf(" Your choice (1/2):");
scanf("%d", &c);
switch(c){
case 1:
printf(" Please input data (mm-dd-yy):");
scanf("%d%c%d%c%d", &m, &c1, &d, &c1, &y);
findDays(m,d,y);
break;
case 2:
printf(" Input days:");
scanf("%d", &tdays);
printf(" Years:");
scanf("%d", &y);
findDate(tdays, y);
break;
default:
printf("INVALID");
}
printf (" Do more (Y/N) ? ");
scanf ("%S", &more);
}
return 0;
}
OUTPUT:
This program will find days passed or date in the year
1) Input date(mm/dd/yyyy) to find days passed
2) Input passed days to find date in the year
Your choice (1/2):1
Please input data (mm-dd-yy):03-01-2001
There are 60 days passed in the year 2001
Do more (Y/N) ? Y
This program will find days passed or date in the year
1) Input date(mm/dd/yyyy) to find days passed
2) Input passed days to find date in the year
Your choice (1/2):1
Please input data (mm-dd-yy):02-29-2001
Invalid date !
Do more (Y/N) ? N
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.