4 Replies - 1418 Views - Last Post: 18 March 2012 - 05:21 PM Rate Topic: -----

#1 T4YLOR15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-March 12

Program writes calendar file - first day of month spacing issue

Posted 16 March 2012 - 06:08 AM

I've been working on this assignment for over a week and have rewritten twice. First I couldn't get the days of the week to line up, but now I can't get the first day of the week to "push" over. I've been browsing through a bunch of code and nothing seems to be correlating into mine. The program asks for the user to input the name of the file with all of the month names (attached) and then auto-generates all of the month calenders for 2012 (only 2012) into individual text files. I've tried creating an array to pass the day of the week into the createMonth function but it doesn't shit the numbers, in fact nothing ever moves anything :!. Any help is greatly appreciated.

when prompted, enter input.txt

code :

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

//Pass Variable for generation of calender
int _daysInMonth[] = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int dayOfWeek [] = { 0, 0, 3, 4, 0, 2, 5, 0, 3, 6, 1, 4, 6 };
string months [] = {" ", "January", "February", "March", "April", "May", "June", 
"July", "August", "September", "October", "November", "December"}; 


//Identify month function
int monthIndex ( string month ) {
	for (int i = 0; i <= 12; i++) 
		if (months[i] == month)
			return i;
		
		return 0;
		
}

//Return Number of Days in Month
int daysInMonth ( string month ) {

	return _daysInMonth [monthIndex (month)];

}


//Return position of first day
int firstDayOfMonth (string month) {

	return dayOfWeek [ monthIndex (month) ];

}

//Make the file for the month
void createMonth (string month) {
	
	// Open this file for output
	ofstream out;
	string output_file = month + ".txt";
	out.open ( output_file.c_str() );

	//Check to see if file is good
	if ( !out.is_open() ) {
		cout << "I had trouble opening this file " << endl;
		return;
	}


	// Write heading
	out << "          " << month << endl << endl;

	// Write "  S  M  T... "
	out << "  S   M   T   W   T   F   S  " ;

	// Start of the Month Spacing

	out << " ";
	for ( int i = 0; i <= firstDayOfMonth (month) ; i++) {
		out << " " ; 
		
	}
	

	// Iterate over the days in the month
	for ( int i = 1; i <= daysInMonth ( month ); i++ ) {

		if ( i % 7  == 1) {
				out << endl << " ";
		}
		if ( i < 10 ) {
			out << " " << i;
		} 
		else {
			out << i ;
		}

		out << "  ";
	}

	out << endl <<endl;

	out.close();

	return;
}



int main () {

	// Ask the user for a filename

	cout << "Enter name of the input file: " << endl;
	string file_name;
	cin >> file_name;

	// Open that file

	ifstream in;
	in.open ( file_name.c_str() );

	//Check for good file
	if ( !in.is_open() ) {
		cout << "Couldn't open " << file_name << endl;
		return 0;
	}

	// Read the file

	while ( in.good() ) {
		string month_name;
		in >> month_name;
		createMonth ( month_name );
	}
	

	return 0;

}



forgot to hit attach this file.. input.txt attached

Attached File(s)

  • Attached File  input.txt (96bytes)
    Number of downloads: 43


Is This A Good Question/Topic? 0
  • +

Replies To: Program writes calendar file - first day of month spacing issue

#2 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 857
  • Joined: 17-March 11

Re: Program writes calendar file - first day of month spacing issue

Posted 16 March 2012 - 06:51 AM

You need a newline here

out << "  S   M   T   W   T   F   S  \n" ;



Basically you were adding your spacing at the end of the header. This will mess up your lay-out somewhere else again though...

072	        if ( i % 7  == 1) {
073	                out << endl << " ";
074	        }



Here for example, you don't want to switch lines on the 7th/14th/... but on a sunday regardless of which day in the month it is.

This post has been edited by Karel-Lodewijk: 16 March 2012 - 06:59 AM

Was This Post Helpful? 2
  • +
  • -

#3 T4YLOR15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-March 12

Re: Program writes calendar file - first day of month spacing issue

Posted 16 March 2012 - 07:21 AM

Oh wow. Ha I see what you're saying. I got it to space properly, now I just have to redo the formatting, which I may be able to solve a little more efficiently. I'll post in here if I have any problems. Thank you very much.
Was This Post Helpful? 0
  • +
  • -

#4 T4YLOR15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-March 12

Re: Program writes calendar file - first day of month spacing issue

Posted 18 March 2012 - 08:41 AM

Hey man so I've been working around for a few hours with this, and I figured out a few of the kinks but can't get the lines to break after the end of the week. I've been working with trying to add some for loops, and trying to get it to break at the end, but what keeps happening is everything just goes to one number per line.
Ex:

January

S M T W T F S
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31



	// Start of the Month Spacing
	
	out << "";
	out << endl;
	for ( int i = 1; i <= firstDayOfMonth (month) ; i++) {
		out << " " ;
		
	}	


	// Iterate over the days in the month
	for ( int i = 1; i < daysInMonth (month) ; i++ ) {
		
		if ( i % 7  ==  7 - firstDayOfMonth ( month )) {
				out  << " " << i ;
				out << endl; 
	
		}
		if ( i < 10 ) {
			out  << " " << i;
			
		} 
		else {
			out  << i ;
		}
		

		out << "  " ;
			
	}

Was This Post Helpful? 0
  • +
  • -

#5 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: Program writes calendar file - first day of month spacing issue

Posted 18 March 2012 - 05:21 PM

Hi, when you try and print the days you have 7 - firstDayOfMonth ( month ). If the first day of the month function returns 0, then 7 - 0 = 7 so (i % 7 == 7) will always be false.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1