Question

In: Computer Science

This assignment uses data structures, selection, use, control structures, logic, computation, file I/O, etc. In C++...

This assignment uses data structures, selection, use, control structures, logic, computation, file I/O, etc. In C++

There should be 3 files for the project: theMain.cpp, dateClass.h, dateClass.cpp (or similar file names)

For input/output. Read at least 10 dates in a data file set up this way:

The first number is the number of data items in the file. Then after that, each row is one date in the format month day year.

here is a sample data file of 6 elements:

6

1 1 2016

12 31 2011

11 01 -5506

13 02 2002

2 31 2020

2 21 2015

the output would be:

1 1 2016 would be: 1/1/16, January 1, 2016 and 1 January 2016

12 31 2011 would be: 12/31/11, December 31, 2011 and 31 December 2011

11 01 -5506 is an unknown date

13 02 2002 is an unknown date

2 31 2020 is an unknown date

2 21 2015 would be: 2/13/15, February 21, 2015 and 13 February 2015

if you program the main program similar to the example below, the output will be close to the example in the textbook. (the example below does not use files or a loop to read data)

#include <iostream>

#include <string>

#include "dateClass.h"

using namespace std;

int main()

{

       Date aDate(3,15,2016);

       aDate.displayShort();

       aDate.displayLong();

       aDate.displayGlbl();

       cout << endl;

       system("pause");

       return 0;

} //end main

Solutions

Expert Solution

Here is the answer for your question in C++ Programming Language.

Kindly upvote if you find the answer helpful.

NOTE : I have used dev-c++ to compile the code, hence I had to include .cpp file instead of .h file in main.cpp file. (i.e., I had used #include "dateClass.cpp" instead of #include "dateClass.h" in main.cpp file). If including .h file works for you please include that only.

Please comment below if you have any doubts.

######################################################################

CODE :

dateClass.h

#ifndef MYDATE_H
#define MYDATE_H
#include<string>
using namespace std;
class Date{
   public:
       Date(int,int,int);
       string validateDate();
       void displayShort();
       void displayLong();
       void displayGlbl();
   private:
       int day;
       int month;
       int year;
       string months[12];
};
#endif

#############################################################

dateClass.cpp

#include "dateClass.h"

Date::Date(int month,int day,int year){
   months[0] = "January"; months[1] = "February"; months[2] = "March"; months[3] = "April";
   months[4] = "May"; months[5] = "June"; months[6] = "July"; months[7] = "August";
   months[8] = "September"; months[9] = "October"; months[10] = "November"; months[11] = "December";
   this->day = day;
   this->month = month;
   this->year = year;
}
string Date::validateDate(){
   if(this->month < 0 || this->month > 12)
       return "Invalid";
   else if(this->year < 0)      
       return "Invalid";
   else if(this->day < 0 || day > 31)
       return "Invalid";
   else{
       if(this->month > 0 && this->month <= 7){
           if(this->month % 2 == 1){
               if(this->day > 31){
                   return "Invalid";
               }
           }else if(this->month % 2 == 0){
               if(this->month == 2){
                   if(this->day > 28){
                       return "Invalid";
                   }
               }else if(this->day > 30)
                   return "Invalid";
           }
       }else if(this->month > 7){
           if(this->month % 2 == 1){
               if(this->day > 30){
                   return "Invalid";
               }
           }else if(this->month % 2 == 0){              
               if(this->day > 31)
                   return "Invalid";
           }
       }
   }
   return "Valid";
}
void Date::displayShort(){  
   cout << this->month << "/" << this->day << "/" << (this->year%100);              
}
void Date::displayLong(){  
   cout << this->months[this->month-1] << " " << this->day << ", " << this->year;  
}
void Date::displayGlbl(){      
   cout << this->day << " " << this->months[this->month-1] << " " << this->year;
              
}

####################################################################

main.cpp

#include <iostream>
#include <string>
#include<cstdlib>
#include<fstream>

#include "dateClass.cpp"
using namespace std;
int main()
{
   int n,day,month,year;
   ifstream inFile;
   inFile.open("DateFile.txt");
   if(!inFile){
       cout << "Error opening the file" << endl;
   }else{
       inFile >> n;          
   for(int i = 0;i<n;i++){
       inFile >> month >> day >> year;
       Date aDate(month,day,year);
       string isValid = aDate.validateDate();
       if(isValid.compare("Invalid") == 0)
          cout << "unknown date" << endl;
       else{
               cout << month << " " << day << " " << year << " would be: ";
       aDate.displayShort();
       cout << ", ";
       aDate.displayLong();
       cout << " and ";
       aDate.displayGlbl();
       cout << endl;
       //system("pause");  
           }  
       }
      
   }
return 0;
} //end main

##########################################################################

DateFile.txt

6
1 1 2016
12 31 2011
11 01 -5506
13 02 2002
2 31 2020
2 21 2015

##########################################################################

SCREENSHOTS :

Please see the screenshots of the code below for the indentations of the code.

dateClass.h

dateClass.cpp

###################################################################

main.cpp

###########################################################################

DateFile.txt

#####################################################################

OUTPUT :

Any doubts regarding this can be explained with pleasure :)


Related Solutions

The assignment: C++ program or Java You need to use the following programming constructs/data structures on...
The assignment: C++ program or Java You need to use the following programming constructs/data structures on this assignment. 1. A structure named student which contains:   a. int ID; student id; b. last name; // as either array of char or a string c. double GPA;    2. An input file containing the information of at least 10 students. 3. An array of struct: read the student information from the input file into the array. 4. A stack: you can use the...
Working on a c++ data structures assignment.   Linked List add node. I have the head case...
Working on a c++ data structures assignment.   Linked List add node. I have the head case and the tail case working but the middle/general case I can not get to work for the life of me. I have included the header file and the data struct file below   #ifndef LINKEDLIST_H #define LINKEDLIST_H #include "data.h" #include <iostream>   //take this out using std::cout; class LinkedList{     public:         LinkedList();         ~LinkedList();         bool addNode(int, string);         bool deleteNode(int);         bool getNode(int, Data*);         void printList(bool = false);         int getCount();         void...
This assignment will give you practice in Handling Exceptions and using File I/O. Language for this...
This assignment will give you practice in Handling Exceptions and using File I/O. Language for this program is JAVA Part One A hotel salesperson enters sales in a text file. Each line contains the following, separated by semicolons: The name of the client, the service sold (such as Dinner, Conference, Lodging, and so on), the amount of the sale, and the date of that event. Prompt the user for data to write the file. Part Two Write a program that...
c++ program You are to use a Heap data structure for this assignment I currently work...
c++ program You are to use a Heap data structure for this assignment I currently work for an investment/insurance company and I’m looking for clients to call, ones with funds.  I need to have a schedule that shows the list of customers to call and the order to be called.  The list of customers names and phone numbers are in the file ‘NamesAndPhoneV2.txt’.  A second file contains a net worth value for each client.  The files are separated for security and protection reasons, but...
Module/Week 3 ASSIGNMENT (CONTROL STRUCTURES . IF ..ELSE) Write a C++ program that computes a student’s...
Module/Week 3 ASSIGNMENT (CONTROL STRUCTURES . IF ..ELSE) Write a C++ program that computes a student’s grade for an assignment as a percentage given the student’s score and total points. The final score must be rounded up to the nearest whole value using the ceil function in the <cmath> header file. You must also display the floating-point result up to 5 decimal places. The input to the program must come from a file containing a single line with the score...
ASSIGNMENT: Enter the hypothetical data below in SPSS to use for the assignment.  The SPSS commands: 'file',...
ASSIGNMENT: Enter the hypothetical data below in SPSS to use for the assignment.  The SPSS commands: 'file', 'new', 'data' will create a spreadsheet in which to enter the data below (manually). Case Control Treatment 1                              5                              6 2                              4                              7 3                              5                              5              4                              4                              6 5                              5                              5 6                              6                              6 7                              5                              5 8                              4                              6 9                              5                              5 10                           5                              10 In this experiment, all participants rated the credibility of fake news stories on a scale of 1...
assignment in C I have a file that contains a lot of lines with words separated...
assignment in C I have a file that contains a lot of lines with words separated by spaces ( also contains empty lines as well). I need to read this file line by line and put each word into 2d array. NOTE: i need to skip spaces as well as empty lines. also I need to count each word.
****NOTE YOU MUST USE SYSTEM CALL I/O, meaning STANDARD I/O IS NOT ALLOWED Write a C...
****NOTE YOU MUST USE SYSTEM CALL I/O, meaning STANDARD I/O IS NOT ALLOWED Write a C program using system call I/O to determine how many lines there are in a text file.
Write a C program using system call I/O to ****NOTE YOU MUST USE SYSTEM CALL I/O,...
Write a C program using system call I/O to ****NOTE YOU MUST USE SYSTEM CALL I/O, meaning STANDARD I/O IS NOT ALLOWED a) open an existing text file passed to your program as a command-line argument, then b) display the content of the file, c) ask the user what information he/she wants to append d) receive the info from the user via keyboard e) append the info received in d) to the end of the file f) display the updated...
Write a C program, called reverse, using standard I/O functions, to take a file as input...
Write a C program, called reverse, using standard I/O functions, to take a file as input then copies it to another file in reverse order. That is, the last byte becomes the first, the byte just before the last one becomes the second, etc. The program call should look like: reverse fileIn fileOut
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT