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...
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...
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...
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...
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.
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
Write a C program using system call I/O to a) open an existing text file passed...
Write a C program using system call I/O to 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 content of the file
Using C programming I have a file that contains earthquake data that I will copy and...
Using C programming I have a file that contains earthquake data that I will copy and paste below. I want to use either bubble or insertion sort to sort the file by latitude in ascending order, then create a new file containing the sorted data. example file to sort: time,latitude,longitude,depth,mag,magType,nst,gap,dmin,rms,net 2020-10-17T17:22:03.840Z,32.877,-116.2991667,0.31,1.16,ml,21,119,0.07747,0.26,ci 2020-10-17T17:17:29.980Z,34.1611667,-116.452,2.75,0.87,ml,17,66,0.05224,0.22,ci 2020-10-17T17:03:54.460Z,33.5396667,-116.4613333,8.66,0.63,ml,18,126,0.06084,0.16,ci 2020-10-17T16:55:01.080Z,63.254,-151.5232,8,1.4,ml,,,,0.9,ak
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT