Question

Write a C++ program accepts a three-integer date in the following formats: mm/dd/yyyy or mm-dd-yyyy. Write...

Write a C++ program accepts a three-integer date in the following formats: mm/dd/yyyy or mm-dd-yyyy. Write a Boolean function that validates the date. Note: account for leap years. After testing with single dates and verifying the algorithm works, set your program up to accept a file called dates.dat. Read the dates into your program and output bad dates to a file called bad_dates.dat.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

//date type structure

struct date

{

    int dd;

    int mm;

    int yyyy;

};

//return true if year is a leap year

bool isLeapYear(int year)

{

    return (((year % 4 == 0) &&  (year % 100 != 0)) || (year % 400 == 0));

}

bool checkDate(int dd, int mm, int yyyy)

{

    // check if year is between 1800 and 9999

    if (yyyy > 9999 ||  yyyy < 1800)

        return false;

    if (dd < 1 || dd > 31)

        return false;

    if (mm < 1 || mm > 12)

        return false;

   

    

  

    // for even months date cannot be greater than 30

    if (mm == 4 || mm == 6 || mm == 9 || mm == 11)

        return (dd <= 30);

    //For february month

    if (mm == 2)

    {

        if (isLeapYear(yyyy))

        return (dd <= 29);

        else

        return (dd <= 28);

    }

  

    return true;

}


int main(){

    //input file

    fstream file;

    file.open("dates.dat",ios::in|ios::binary);

    //output file

    ofstream outFile;

    outFile.open("bad_dates.dat", ios::binary | ios::app);

    

    //check if any error while opening file

    if(!outFile || !file ){

        cout<<"cannot open file"<<endl;

        return 1;

    }

    //date object for read and write of dates

    date d;

    file.read((char*)&d,sizeof(date));

    while(file){

        

        //check if date is not valid then write in bad_date.dat

        if(!checkDate(d.dd, d.mm, d.yyyy))

            outFile.write((char*)&d, sizeof(date));

        

    file.read((char*)&d,sizeof(date));

    }

    outFile.close();

    return 0;

}

  

Add a comment
Know the answer?
Add Answer to:
Write a C++ program accepts a three-integer date in the following formats: mm/dd/yyyy or mm-dd-yyyy. Write...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that accepts a date from the user in the form mm/dd/yyyy

    # C PROGRAMMING LANGUAGEWrite a program that accepts a date from the user in the form mm/dd/yyyy and then dis- plays it in the form month dd, yyyy, where month is the name of the month: Enter a date (mm/dd/yyyy): 2/17/2011 You entered the date February 17, 2011 Store the month names in an array that contains pointers to strings.

  • Write a program that prompts the user to enter the date in mm/dd/yyyy format. The program...

    Write a program that prompts the user to enter the date in mm/dd/yyyy format. The program should use a single input statement to accept the date, storing each piece of the date into an appropriate variable. Demonstrate that the input was obtained correctly by outputting the month, day, and year to the screen. Sample output: Enter (date (mm/dd/yyyy): 02/08/2011 Month entered: 2 Day entered: 8 Year entered: 2011 Challenge: Although the user entered 02, C++ drops the 0 as insignificant...

  • Write a java program Problem 4 involves a date in the form of mm/dd/yyyy. You must...

    Write a java program Problem 4 involves a date in the form of mm/dd/yyyy. You must write a java program that checks if a date is valid or not , make sure that the program tells you where the error is in the program if there is one . I want you to work with characters and the switch statement, and write out a reason for each incorrect input value. .

  • Question 2: WRITE A C PROGRAMME TO Accept dates in ‘dd-mm-yyyy’ format into string variables and...

    Question 2: WRITE A C PROGRAMME TO Accept dates in ‘dd-mm-yyyy’ format into string variables and perform the following Validate the given date Find the difference between two dates Find whether date d1 is earlier than or later than or equal to the date d2 Total Marks : 35 USE THE FOLLOWING GUIDLINES String usage 10 marks Control Structures usage 10 marks Outputs Format 2.5 marks Correctness 2.5 marks Variables usage marks

  • Create class Date with the following capabilities: a) Output the date in multiple formats, such as...

    Create class Date with the following capabilities: a) Output the date in multiple formats, such as MM/DD/YYYY June 14, 1992 DDD YYYY b) Use overloaded constructors to create Date objects initialized with dates of the formats in part (a). In the first case the constructor should receive three integer values. In the second case it should receive a String and two integer values. In the third case it should receive two integer values, the first of which represents the day...

  • CODING IN JAVA Dates are printed in several common formats. Two of the more common formats...

    CODING IN JAVA Dates are printed in several common formats. Two of the more common formats are: 07/21/55 and July 21, 1955 Write a program that reads a date in the first format and prints that date in the second format. Prompt the user and accept user input for the date in MM/DD/YYYY format. Retrieve the month portion of the date entered and convert it to an integer. Do the same thing for day and year Create a string named...

  • C Program Question 1: Write a program that reads a date from the keyboard and tests...

    C Program Question 1: Write a program that reads a date from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format: mm/dd/yyyy Note that there is no space in the above format. A date in this format must be entered in one line. A valid...

  • I need this in Java, please! The ISO 8601 Standard date format for Information Interchange indicates...

    I need this in Java, please! The ISO 8601 Standard date format for Information Interchange indicates that a date be written as such: yyyy-MM-dd (eg. 2012-07-02, 1999-12-05, 1998 -01-27 ) where yyyy represents the four digit year MM represents a two digit numerical month dd represents a two digit numerical day Chinese date format is specified as: yyyy-M-d Macedonean date format is specified as: d.M.yyyy where yyyy represents the four digit year M represents a one or two digit numerical...

  • Question 1: Write a program in C that reads a date from the keyboard and tests...

    Question 1: Write a program in C that reads a date from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format: mm/dd/yyyy Note that there is no space in the above format. A date in this format must be entered in one line. A valid...

  • C Programming Quesition (Structs in C): Write a C program that prompts the user for a...

    C Programming Quesition (Structs in C): Write a C program that prompts the user for a date (mm/dd/yyyy). The program should then take that date and use the formula on page 190 (see problem 2 in the textbook) to convert the date entered into a very large number representing a particular date. Here is the formula from Problem 2 in the textbook: A formula can be used to calculate the number of days between two dates. This is affected by...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT