C++ Calendar, year and first day of week given/

Functional Decomp seems okay, I just lost my way on my usually favorit

Page 1 of 1

5 Replies - 4191 Views - Last Post: 03 April 2007 - 05:11 AM Rate Topic: -----

#1 megenk11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 31-March 07

C++ Calendar, year and first day of week given/

Posted 01 April 2007 - 01:05 AM

I am to write a calendar given the year and the day of the week that january 1 falls on, The user is asked and gives this.
It should be simple my decomp looks okay, but when I start to code I try to do like ten functions at once!. My brain isn't working, the assighnment was due 3/30/07 so it's not a matter of a grade, it's being the only non-programmer in the class and I REFUSE to give up! I still have a 4.0 and if I even try and turn something in I'll get some credit 'cause 40 students are now 8 and 7 of them KNOW C++! But mostly I really want to learn, and I learn even or especially from my mistakes. I have an important program to write < see my intro. I'm even taking typing for the summer semester!

#include <iostream>
#include <fstream>

void printDay (startDay, daysInMonth)

void main();
{
	for (weekDay = 1; weekDay <=a; weekDay++)
	printDay(startDay, daysInMonth)

	void printDay(startDay, daysInMonth)
	{	
		int weekDay; 
				  
		for ( int weekDay = 1; weekDay = daysInMonth; weekDay++) 
	  {
		  cout << setw(3) << weekDay																																				   
	  
	 cout << weekDay + 1; 
	 if (weekDay == 7) {
	 cout << weekDay = 0
	 
	 }
}

please look at my text

See, I'm trying to look at too much at once! If I could trace functions for a living I would be in heaven. Following is my Functional Decomposition.

This was all due last Friday, so it's just the point that I WILL NOT GIVE UP! I am the only "non-programmer" in a class of 8 that atarted at40 students and I am NOT going to give them the satisfaction of "see, I tols you an artist could never learn C++". It's really not hard, there is just so much of it to learn1 Here's my decomp as a jpg . I don't care if I'm late, Iv'e really been getting it, I just had a tragic death in the family and it made me sleep more and care less, and this has been my do all and be all for months. I can't quit now! Teach gave us a easy leap year code, but thid first day of the week and the next month following, My brain isnt working right. Last week I wrote a total math tutor in 5 hours, great explainations and all!

Please help me someone..

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: C++ Calendar, year and first day of week given/

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: C++ Calendar, year and first day of week given/

Posted 01 April 2007 - 08:04 AM

Are you having troubles with specific functions? Or overall program structure?
Was This Post Helpful? 0
  • +
  • -

#3 megenk11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 31-March 07

Re: C++ Calendar, year and first day of week given/

Posted 01 April 2007 - 08:44 PM

View PostAmadeus, on 1 Apr, 2007 - 08:04 AM, said:

Are you having troubles with specific functions? Or overall program structure?

As I try to write any or every function, I get lost in try to have them all do everything. It is like I went from genius to idiot over night. I just need a tiny push as in "try doing this first, then when that works, do this,"
Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: C++ Calendar, year and first day of week given/

Posted 02 April 2007 - 11:35 AM

First lets start by fixing the code you have, before we dig into some of the other problems.

The first thing is prototypes. Prototypes need to have a semi-colon after the end of the statement. Otherwise the compiler will think you are trying to define the function, when in reality a prototype is just telling the compiler what to expect before it actually gets to the function definition. Also, because your function is taking paramenters you need to specify what data type they will be. In this case they will be of type integer.
#include <iostream>
#include <fstream>

void printDay (int startDay, int daysInMonth);


Next problem is main, main is a function definition and as such it will not have a semi-colon at the end of the line. But main will also always return an integer. So the return type of main should be int not void. Because of this at the end of the main function you will need a return statement. Return 0 for successful completion of the application and return 1 for unsuccessful completion. The reason I added the getchar() is to keep the console window open after completion of your program, otherwise it will close and you won't see the results.
#include <iostream>
#include <fstream>

void printDay (startDay, daysInMonth);

int main()
{
	for (weekDay = 1; weekDay <=a; weekDay++)
	printDay(startDay, daysInMonth)


	 getchar();
	 return 0;
}


Next step is that functions are located after your main function, not inside.
#include <iostream>
#include <fstream>

void printDay (startDay, daysInMonth);

int main()
{
	for (weekDay = 1; weekDay <=a; weekDay++)
	printDay(startDay, daysInMonth)


	 getchar();
	 return 0;
}

void printDay(int startDay, int daysInMonth)
{	
	int weekDay;
				  
	for ( int weekDay = 1; weekDay = daysInMonth; weekDay++)
	{
		cout << setw(3) << weekDay																																				  
	  
		cout << weekDay + 1;
		if (weekDay == 7) {
			cout << weekDay = 0	
		}
	}
}


This code still will not compile the way it is currently written. There are more problems with your code than just what I've explained here. Missing semi-colons at the end of statements. Using variables that have not been declared in main. Just to name a couple.

But I think this will get you headed in the right direction. If you have any further questions post them in the topic, but do try to describe the specific problem in as much detail as you can. It makes it easier for us to help you.
Was This Post Helpful? 0
  • +
  • -

#5 megenk11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 31-March 07

Re: C++ Calendar, year and first day of week given/

Posted 03 April 2007 - 02:07 AM

Quote

so if anyone is still speaking to me ive come a ways on my calendar in the last two hours but now I dont knoe how to make the bool leap year function work with my switch statement and it's all just not quite right. On top of you guys laughing at me, my classmates nicely said, "well at least youve got a good start on next semester" like I should just quit now.


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


void printMonth(int numDays, int dayStart);
void getNumDays(int month, int &numDays);
bool isLeapYear(int year);
void getData(int &year, int &dayStart); 
void printTitle(int &month);

int main()
{
	

	int dayStart;
	int year;
	int month;
	int numDays;
	
	
	getData(year, dayStart); 
	printTitle(month);
	isLeapYear(year);
	getNumDays( month, numDays);
	printMonth( numDays, dayStart);

  system("pause");
}



void printTitle(int &month)
{
  
   month =+ 1;

		switch(month)
		{
			case 1 : cout << "	   January" << endl;
					 cout << " S  M  T  W  T  F  S" << endl;
					 cout << "--------------------" << endl;
					 break;
			case 2 : cout << "	   February" << endl;
					 cout << " S  M  T  W  T  F  S" << endl;
					 cout << "--------------------" << endl;
					 break; 
			case 3 : cout << "	   March" << endl;
					 cout << " S  M  T  W  T  F  S" << endl;
					 cout << "--------------------" << endl;
					 break;
			case 4 : cout << "	   April" << endl;
					 cout << " S  M  T  W  T  F  S" << endl;
					 cout << "--------------------" << endl;
					 break;
			case 5 : cout << "	   May" << endl;
					 cout << " S  M  T  W  T  F  S" << endl;
					 cout << "--------------------" << endl;
					 break;
			case 6 : cout << "	   June" << endl;
					 cout << " S  M  T  W  T  F  S" << endl;
					 cout << "--------------------" << endl;
					 break;
			case 7 : cout << "	   July" << endl;
					 cout << " S  M  T  W  T  F  S" << endl;
					 cout << "--------------------" << endl;
					 break; 
			case 8 : cout << "	   August" << endl;
					 cout << " S  M  T  W  T  F  S" << endl;
					 cout << "--------------------" << endl;
					 break;
			case 9 : cout << "	   September" << endl;
					 cout << " S  M  T  W  T  F  S" << endl;
					 cout << "--------------------" << endl;
					 break;
			case 10 : cout << "	   October" << endl;
					 cout << " S  M  T  W  T  F  S" << endl;
					 cout << "--------------------" << endl;
					 break;
			case 11 : cout << "	   November" << endl;
					 cout << " S  M  T  W  T  F  S" << endl;
					 cout << "--------------------" << endl;
					 break;
			case 12 : cout << "	   December" << endl;
					 cout << " S  M  T  W  T  F  S" << endl;
					 cout << "--------------------" << endl;
					 break;
						  
		}  
}




void getData(int &year, int &dayStart)
{
	 cout << "What year do you want a calendar for?" << endl;
	 cin >> year;
	 cout << "What day of the week does January 1 fall on?" << endl;
	 cout << "(Enter 0 for Sunday, 1 for Monday, etc.)" << endl;
	 cin >> dayStart;
}

bool isLeapYear(int year)
{
	 if (year % 400 == 0) {
		 return true;
	 }
	 if (year % 100 == 0) {
		 return false;
	 }
	 return year % 4 == 0;
}

void getNumDays(int month, int &numDays)
{
	 switch (month)
	 {
		 case 1 :
		 case 3 :
		 case 5 :
		 case 7 :
		 case 8 :
		 case 10:
		 case 12: numDays = 31;
		 break;
		 case 4 :
		 case 6 :
		 case 9 :
		 case 11: numDays = 30;
		 break;
		 case 2 == true : numDays = 29;	   
		 break;
	 }
}
		 
 void printMonth(int numDays, int dayStart)
 {
	  int daycount;
	  int weekDay;
	  
	  
	  if (dayStart == daycount) {
	  
	  
		  for (daycount = 0; daycount <=6; daycount++)
		  weekDay++;
	  }
	 else 
		 for (daycount = 0; daycount <= dayStart; daycount++)
		   cout << " " << "1";
	  
	   cout << setw(3) << daycount + 1; 
	  if (weekDay == 7) {
	 cout << weekDay == 0;
	 }	
 }


Was This Post Helpful? 0
  • +
  • -

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: C++ Calendar, year and first day of week given/

Posted 03 April 2007 - 05:11 AM

Topics merged for continuity.

I'd suggest that you not call your isLeapYear function from main...call it from the switch statement itself. your case would then look something like the following:
case 2:
   {
	  if(isLeapYear(year)){numdays=29;}
	}
   break;


Of course, this will also necessitate passing the year variable to the function to be used in comparison.

Your main function ends with a system call to the pause command. While I would normally not recommend such an approach, the final line in your main function should be a return statement, such as return 0;...it can be placed after the system call.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1