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

//Write in C++ //use this evote code and add File I/O operations (as specified in the...
//Write in C++ //use this evote code and add File I/O operations (as specified in the comments) to it. #include<iostream> using namespace std; int main() {int choice; int bezos = 0 , gates = 0 , bugs = 0 ; int vc = 0 ; do {    cout<<"\n\n\nEVOTE\n-----"    <<"\n1.Jeff Bezos    <<"\n2.Bill Gates    <<"\n3.Bugs Bunny" // 4. Print current tally [hidden admin option] // 5. Print audit trail [hidden admin option] // 6. mess with the vote...
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...
C++ Data Structures: Use Huffman coding to encode text in given file (Pride_and_Prejudice.txt). TO_DO: Define a...
C++ Data Structures: Use Huffman coding to encode text in given file (Pride_and_Prejudice.txt). TO_DO: Define a struct for Huffman tree node. This struct contains links to left/right child nodes, a character, and its frequency.Define a function for file reading operation. This function should take in a filename (string type) as parameter and return a proper data structure object that contains characters and their frequencies that will be used to generate Huffman tree nodes.The construction of Huffman tree requires taking two...
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...
This assignment covers file I/O. Must design and code a program that can accomplish four things:...
This assignment covers file I/O. Must design and code a program that can accomplish four things: copy one file to another (i.e., place the contents of file A into file B), ‘compress’ a file by removing all vowels (a, e, i, o, u, and y) from the file, merge two files, and finally double each line of an input file, saving into an output file. Also make it so the input file's content can be changed from the example given....
Selection sort on fstream data C++: Sort the given student information data inside a text file...
Selection sort on fstream data C++: Sort the given student information data inside a text file The format of the student data inside the text file is: Student Number, Surname, First Name, Middle Name, Birthday, Course, Year, School Year (The list can be updated over time so there is no limit for the size of the data) But I need to automatically sort the given the data in ascending alphabetical order based on their surnames when displaying the list. I...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT