GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 28-Nov-2004, 09:34
dontcare dontcare is offline
New Member
 
Join Date: Oct 2004
Posts: 12
dontcare is on a distinguished road

Problems with switch statement


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.

CPP / C++ / C Code:

#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;
		}
	}

Attached Files
File Type: txt io.txt (187 Bytes, 15 views)
  #2  
Old 28-Nov-2004, 12:41
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by dontcare
CPP / C++ / C Code:
		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;
			}
The % operator (in this case) returns the values 0-6, not 1-7. Therefore your days are going to be 1 off, and Tuesday won't exist.

To check if your computations are correct, search the web for a perpetual calendar and use that to test your program. There are lots of them around.
__________________

Age is unimportant -- except in cheese
  #3  
Old 29-Nov-2004, 08:12
dontcare dontcare is offline
New Member
 
Join Date: Oct 2004
Posts: 12
dontcare is on a distinguished road
Quote:
Originally Posted by WaltP
The % operator (in this case) returns the values 0-6, not 1-7. Therefore your days are going to be 1 off, and Tuesday won't exist.

To check if your computations are correct, search the web for a perpetual calendar and use that to test your program. There are lots of them around.

Thanks for the help, it fixed one my errors. However, the program still loops to one Day. I don't understand why.
  #4  
Old 29-Nov-2004, 09:36
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,693
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by dontcare
Thanks for the help, it fixed one my errors. However, the program still loops to one Day. I don't understand why.

Look at this:

CPP / C++ / C Code:
  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) {

You need to switch on the number of days since Jan 1, 1800. (But you made the daysSince element private.)

There are several computational errors that you should be able do discover as you run the program.

For starters: just use Jan 1, 1800. Concentrate on that. You must get the right answer here, otherwise, nothing can possibly work?

When you can get the right answer for that, then try Feb 2, 1800, Mar 3, 1800, etc. Finally, try other years.

Sprinkle cout<< throughout your routines to see where the calculations are taking you.

Regards,

Dave
  #5  
Old 29-Nov-2004, 18:28
dontcare dontcare is offline
New Member
 
Join Date: Oct 2004
Posts: 12
dontcare is on a distinguished road
Quote:
Originally Posted by davekw7x
Look at this:

CPP / C++ / C Code:
  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) {

You need to switch on the number of days since Jan 1, 1800. (But you made the daysSince element private.)

There are several computational errors that you should be able do discover as you run the program.

Dave

Thanks for the help. I made some changes, first I made the prototypes bool, and Prettyprint public members. Then I made them friend functions, to allow them access to the private member. Thus fixing my switch statement problem.
 
 

Recent GIDBlogFlickr uploads of IA pictures by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Command Line Arguments, Part 1 WaltP C Programming Language 0 10-Jul-2004 23:34
Chaintech Geforce 5600 FX problems bartster74 Computer Hardware Forum 8 04-May-2004 13:16
Help a C++ Idiot: I am trying to fill an array based on a switch statement. Psycop C Programming Language 2 14-Apr-2004 03:12
switch statement freedomJoe C Programming Language 0 27-Oct-2003 08:45

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:25.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.