This program displays the information of an input file containing dates and strings, the output is in the following form On day, January 1, 1800 The first day begins. [number of days since 1/1/1800; (1)]. My problem is implementing a correct switch statement to display the Day of the week. The day ot the week is found using the number of days since 1.1.1800, here's the code and an attached file. Also I' not sure if the correct output in the number of days is right. If someone could check my code, I would appreciate it.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <iomanip>
using namespace std;

// DaysFrom1800 Class Definition
	class DaysFrom1800 {
	public:
	DaysFrom1800();
	int numDaysFrom1800 (int day, int month, int year);
	int fillDaysArray (int numberOfDates, int day[], int month[], int year[], string textArray[], DaysFrom1800 numbers []);
	void DaysFrom1800::printDaysSince1800 () {
	cout << daysSince;
	}


	private:
	int daysSince;
	};

// Prototypes
	bool leapCheck (int year);
	void prettyPrint (int numberOfDates, string textArray[], DaysFrom1800 numbers[], int month[], int day[], int year[]);


	int main () {
		int day[200];
		int month [200];
		int year [200];
		int numberOfDates = 0;
		DaysFrom1800 numbers[200];
		DaysFrom1800 dummy;
		string textArray [200];

		numberOfDates = dummy.fillDaysArray (numberOfDates, day, month, year, textArray, numbers);

		prettyPrint(numberOfDates, textArray, numbers, month, day, year );
		return 0;
		}
	int DaysFrom1800::fillDaysArray (int numberOfDates, int day[], int month[], int year[], string textArray[], DaysFrom1800
	numbers []) {

		ifstream inStream;
		inStream.open("io.txt");

			if (inStream.fail()) {
			cerr << "Can't open file!\n";
			exit(1);
			}


		int i=0;

		char temp [100];

			while (!inStream.eof()) {
			inStream >> month[i] >> day[i] >> year[i];
			inStream.get(temp,100, '\n');
			textArray[i]=temp;
			i++;


		numberOfDates = i;
		for (i=0; i<numberOfDates; i++) {
		numbers[i].daysSince = numDaysFrom1800 (day[i], month[i], year[i]);

		}
	}

return numberOfDates;

}

// Defualt Constructer for DaysFrom1800 Class
DaysFrom1800::DaysFrom1800() : daysSince (0) {
// Nothing to be done here...
}
int DaysFrom1800::numDaysFrom1800 (int day, int month, int year) {
// Declare leapCount variable to keep track of number of leap years and days variable
//    to store number of days

  bool thisYear = leapCheck(year);
	int StartOfYear (int year);
		{
		int leapCount =0;
		int days = 0;
		// For loop to find the number of leap years
		for (int i=1800; i < year; i++){
			if (leapCheck(i))
				leapCount ++;
		}

	//Find the Number of Days From Complete Years
	days = ((year-1800-leapCount)*365) + (leapCount*366);
	switch (month) {
		case 1:
			days = days + day;
				break;
		case 2:
			if (day < 28)
				days = days + 31 + day;
					if (thisYear)
						days = days + 31 + 29;
			break;
		case 3:
			if (thisYear)
				days = days +31+29+ day;
			else
				days = days + 31 + 28 + day;
			break;
		case 4:
			if (thisYear)
				days = days + 31 + 29 + 31 + day;
			else
				days = days + 31 + 28 + 31 + day;
			break;
		case 5:
			if (thisYear)
				days = days + 31 + 29 + 31 + 30 + day;
			else
				days = days +31 + 28 + 31 + 30 + day;
			break;
		case 6:
			if (thisYear)
				days = days +31 + 29 + 31 + 30 + 31 + day;
			else
				days = days + 31 + 28 + 31 + 29 + 31 + day;
			break;
		case 7:
			if (thisYear)
				days = days + 31 + 29 + 31 + 30 + 31 + 30 + day;
			else
				days = days + 31 + 28 + 31 + 30 + 31 + 30 + day;
			break;
		case 8:
			if (thisYear)
				days = days + 31 + 29 + 31 + 30 + 31 + 30 + 31 + day;
			else
				days = days + 31 + 28 + 31 + 30 + 31 + 30 + 31 + day;
			break;
		case 9:
			if (thisYear)
				days = days + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + day;
			else
				days = days + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + day;
			break;
		case 10:
			if (thisYear)
				days = days + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day;
			else
				days = days + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day;
			break;
		case 11:
			if (thisYear)
				days = days + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day;
			else
				days = days + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day;
			break;
		case 12:
			if (thisYear)
				days = days + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day;
			else
				days = days + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day;
			break;
		}

return days;

	}

}

	bool leapCheck(int year){
		// if the year is not 0 mod 4 then it is not a leap year
		if ((year % 4) != 0)
		return false;
		// every 4'th century IS a leap year
		if ((year % 400) == 0)
		return true;
		// every 4'th year that is not a century year is a leap year
		if ((year % 100) != 0)
		return true;
		else
		return false;
		}

	void prettyPrint (int numberOfDates, string textArray[], DaysFrom1800 numbers[], int month[], int day[], int year[]) {
		for (int i=0; i<numberOfDates; i++) {
		cout << "On " ;
		switch (numberOfDates% 7) {
			case 1:cout <<	"Wenesday"; break;
			case 2: cout << "Thursday"; break;
			case 3: cout<< "Friday"; break;
			case 4: cout<< "Saturday"; break;
			case 5: cout<< "Sunday"; break;
			case 6: cout<<"Monday"; break;
			case 7: cout<<"Tuesday"; break;
			}
		cout<<","<< " ";
		switch(month[i]){
			case 1:   cout << "January"; break;
			case 2:   cout << "February";  break;
			case 3:   cout << "March";     break;
			case 4:   cout << "April";     break;
			case 5:   cout << "May";       break;
			case 6:   cout << "June";      break;
			case 7:   cout << "July";      break;
			case 8:   cout << "August";    break;
			case 9:   cout << "September"; break;
			case 10:  cout << "October";  break;
			case 11:  cout << "November";  break;
			case 12:  cout <<"December";  break;
			}
		cout<<" "<<setw(2)<< day[i] << "," <<" "<< year[i];
		// Call function to print date from day number
		cout << textArray[i] << '(';
		numbers[i].printDaysSince1800();
		cout << ')' << endl;
		}
	}

Dani AI

Generated

A few focused notes that address what discovered and the remaining problems seen in the thread.

The immediate logic bug was the switch operand: use the per-date day count, not the file-count. After that the repeated "Sunday" came from an inconsistent days-since calculation and some file-reading/loop mistakes. Keep these rules: compute a single, consistent definition of "days since 1/1/1800" (prefer zero-based: 1/1/1800 -> 0), compute it once per record (after the file is read), and index into a small weekday table using that remainder.

A robust way to compute days-since is to sum full years then months and days. This example uses a month-length array and makes Jan 1, 1800 return 0 (so remainder 0 maps to Wednesday if that is your epoch):

int daysSince1800(int d, int m, int y) {
    if (y < 1800 || m < 1 || m > 12 || d < 1) return -1;
    static const int mdays[] = {31,28,31,30,31,30,31,31,30,31,30,31};
    int days = 0;
    for (int yr = 1800; yr < y; ++yr) days += (leapCheck(yr) ? 366 : 365);
    for (int mm = 1; mm < m; ++mm) {
        days += mdays[mm-1];
        if (mm == 2 && leapCheck(y)) ++days;
    }
    days += d - 1; // zero-based within the year
    return days;
}

To print weekday without a long switch, use an array indexed by remainder. If you only want weekday and not days-since, using std::tm + mktime is simpler and less error-prone (note tm_wday: 0=Sunday).

File-reading and loop tips: avoid while(!eof()); prefer while (in >> month >> day >> year) { std::getline(in, text); ... }. Read all records first, then compute daysSince for each record in a separate loop. Add a small accessor (getDaysSince) rather than exposing internals or making many friend declarations. Test with known dates and leap-year boundaries to catch off-by-one errors.

I fixed one error in the switch statement, it should be 0-6, wen. is the first day (1,1,1800). But my problem still isn't fixed, the output is a repeated day, "Sunday". The program should execute the number of days since 1/1/1800 and then divide that number by 7, and finally use the remainder as the switch for the correct output. (%) Please help, need to turn program in today. My next assignment is due Friday and I need this program completed first before preceding to it.

switch (numberOfDates% 7) {
                        case 1:cout <<  "Wenesday"; break;
                        case 2: cout << "Thursday"; break;
                        case 3: cout<< "Friday"; break;
                        case 4: cout<< "Saturday"; break;
                        case 5: cout<< "Sunday"; break;
                        case 6: cout<<"Monday"; break;
                        case 0: cout<<"Tuesday"; break;
                        }
// Prototypes
        bool leapCheck (int year);
        void prettyPrint (int numberOfDates, string textArray[], 		  DaysFrom1800 numbers[], int month[], int day[], int year[]);

Needed to be put into public and made friend functions. In order to use the switch statement for days. The switch should of have been daysSince and not what I had.

for (int i=0; i<numberOfDates; i++) {
cout << "On " ;
switch (numbers[i].daysSince % 7) {
case 1:cout <<	"Wednesday"; break;
case 2: cout << "Thursday"; break;
case 3: cout<< "Friday"; break;
case 4: cout<< "Saturday"; break;
case 5: cout<< "Sunday"; break;
case 6: cout<<"Monday"; break;
case 0: cout<<"Tuesday"; break;

}

Now i need to work on the computational errors I made.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.