Function Errors In Calendar Program

For everyone, just starting with C++ or programming at all. Ask newbie questions in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Function Errors In Calendar Program

Postby ethrtyiS » Mon May 14, 2012 4:02 am

Hey I'm new to C++ and I'm having an issue with the current program I am writing. I am getting only getting one error. Can someone tell me what I am doing wrong. I have been trying to fix it all night. I am not allowed to use global variables which has made it much more difficult on me. I know I need to declare it the three somewhere but even when I declare the 3 arguments in the spacer or monthNums functions, I am still getting the same error. Thanks in advance.

Here is the error:

Code: Select all
error C2660: 'monthNums' : function does not take 3 arguments - 212
.

Here is my full code:

Code: Select all
// This program will be used to print a calendar for any given year and the day of the week

#include <iostream>
#include <iomanip>

void printCal(int year, int start);

using namespace std;

//Function 1
//Prints Calendar
void printCal(int year, int start, int monthDays, int spacerDays, int prevSpacer, int dayCount = 1, int colCount = 0)
{
   void monthNames(int count);
   void dayNames();
   void monthNums(int count, int& monthDays, int& spacerDays);
   void spacer(int start, int& colCount, int count, int& prevSpacer);
   void wrapper(int dayCount, int monthDays);

   int count;
   for (count = 1; count <= 12; count++)
   {
      monthNames(count);
      dayNames();
      monthNums(count, monthDays, spacerDays);
      spacer(start, colCount, count, prevSpacer);
      wrapper(dayCount, monthDays);
   }
}

//Function 2
//Makes The Lines Return
void wrapper(int dayCount, int monthDays, int colCount)
{
   while (dayCount <= monthDays)
   {
      if (colCount == 7) 
      {
         cout << endl;
         colCount = 0;
      }
      cout << setw(3);
      cout << dayCount;
      colCount++;
      dayCount++;
   }
   colCount = 0;
   cout << endl << endl;
}

//Function 3
//Checks For Leap Year
bool leapYear(int year)
{
   if (year % 400 == 0)
   {
      return true;
   }
   if (year % 100 == 0)
   {
      return false;
   }
   if (year % 4 == 0)
   {
      return true;
   }
   return false;
}

//Function 4
//Previous And Current Days In Each Month
void monthNums(int& count, int& monthDays, int& spacerDays, int year, int monthCount)
{
   switch (count)
   {
   case 1:
      monthDays = 31;
      break;
   case 2:
      if(leapYear(year))
         //Check For Leap Year
         monthDays = 29;
      if(!leapYear(year))
         monthDays = 28;
      spacerDays = 31;
      break;
   case 3:
      monthDays = 31;
      if(leapYear(year))
         //Check For Leap Year
         spacerDays = 29;
      if(!leapYear(year))
         spacerDays = 28;
      break;
   case 4:
      monthDays = 30;
      spacerDays = 31;
      break;
   case 5:
      monthDays = 31;
      spacerDays = 30;
      break;
   case 6:
      monthDays = 30;
      spacerDays = 31;
      break;
   case 7:
      monthDays = 31;
      spacerDays = 30;
      break;
   case 8:
      monthDays = 31;
      spacerDays = 31;
      break;
   case 9:
      monthDays = 30;
      spacerDays = 31;
      break;
   case 10:
      monthDays = 31;
      spacerDays = 30;
      break;
   case 11:
      monthDays = 30;
      spacerDays = 31;
      break;
   case 12:
      monthDays = 31;
      spacerDays = 30;
      break;
   }
}

//Function 5
//Prints Out All Month Names
void monthNames(int count, int year)
{
   switch (count)
   {
   case 1:
      cout << "      January " << year << endl;
      break;
   case 2:
      cout << "      February " << year << endl;
      break;
   case 3:
      cout << "      March " << year << endl;
      break;
   case 4:
      cout << "      April " << year << endl;
      break;
   case 5:
      cout << "      May " << year << endl;
      break;
   case 6:
      cout << "      June " << year << endl;
      break;
   case 7:
      cout << "      July " << year << endl;
      break;
   case 8:
      cout << "      August " << year << endl;
      break;
   case 9:
      cout << "      September " << year << endl;
      break;
   case 10:
      cout << "      October " << year << endl;
      break;
   case 11:
      cout << "      November " << year << endl;
      break;
   case 12:
      cout << "      December " << year << endl;
      break;
   }
}

//Function 6
//Prints Days And Indents Them Accordingly
void dayNames()
{
   cout << " --------------------" << endl;
   cout << "  S  M  T  W  T  F  S" << endl;
}

//Function 7
//This Prints The Spaces Between Days and Months
void spacer(int start, int count, int& colCount, int& prevSpacer, int& monthDays, int& monthCount, int& spacerDays, int& year)
{
      if (count == 1)
   {
      int loopCount;
      for (loopCount = 0; loopCount < start; loopCount++)
      {
         cout << "   ";
         prevSpacer++;
      }
      colCount =+ start;
   }
   else if (count != 1)
   {
      int otherSpacer;
      int loopCount;
      int monthCount = count;
      
      monthNums(monthCount, monthDays, spacerDays);
      //Adds Number Of Spaces And Divides By 7
      otherSpacer = (prevSpacer + spacerDays) % 7;
      prevSpacer = 0;
      for (loopCount = 0; loopCount < otherSpacer; loopCount++)
      {
         cout << "   ";
         prevSpacer++;
         colCount =+ otherSpacer;
      }
   }
}

//Main Function
int main()
{
   int year;
   int start;

   cout << "Please Enter Year To View The Calender: ";
   cin >> year;

   while(year <= 0)
   {
      cout<<"Please Enter A Valid Year (Larger Than 0): ";
      cin >> year;
   }

   cout << endl << "0 - Sunday" << endl << "1 - Monday" << endl << "2 - Tuesday" << endl << "3 - Wednesday" << endl << "4 - Thursday" << endl << "5 - Friday" << endl << "6 - Saturday" << endl << endl;
   cout << "Please Enter The Day Of The Week January 1st Falls On (See Above): ";
   cin >> start;   
   cout << endl << endl << endl;

   //Check To Make Sure User Entered A Correct Day
   while ((start < 0) && (start > 6))
   {
      cout << endl << endl << endl;
      cout << "You Entered An Incorrect Day. Please Enter An Integer Between 0-6.";
      cin>>start;
   }

   printCal(year, start); // Calls The Function So That It Prints The Calender
   
   cout << endl;
   system("pause");
   return(0);
ethrtyiS
 
Posts: 7
Joined: Mon May 14, 2012 4:00 am

Re: Function Errors In Calendar Program

Postby exomo » Mon May 14, 2012 9:39 am

There are many errors, but only one compiler error. This is obviously because you monthNums() declared to have 5 parameters
Code: Select all
void monthNums(int& count, int& monthDays, int& spacerDays, int year, int monthCount) {...
and you call it with 3 parameters
Code: Select all
monthNums(count, monthDays, spacerDays);
, but there is no monthNums() function that accepts 3 parameters.

When you fix this error by adding the two additional parameters to the function call, you will get a bunch of linker errors, because you prototypes don't match your actual functions. (To be precise the errors state that there is no reference to the functions you call. This is very much like "there is no function xxx anywhere in your code that matches the function call". You don't get compiler errors on these because you have the prototypes, they tell the compiler "the function xxx is somewhere", but actually they are lying.)

Your main problem seems to be that you don't know where to define variables und which of them should be passed as arguments. I'll try to clear things up a little.
Let's start on printCal. printCal needs two parameters, the year and the day. That's why void printCal(int year, int start) is the way you want to define the function. You don't know any other values at the point you call the function, so you can't give any additional parameters.

If you really want to do it that way you have to pass the month and the year, and by reference the two parameters you want to fill in. (the 5. seems to be unused). Adjust the prototype. Whenever you need to call the function you have to define two local variables and pass them. But I would suggest restructuring the code anyway. Just make a function "int getDaysInMonth(int month, int year)" that returns the number of days in one month of a specified year. Don't care about the spacer days here.
You can avoid most of the problems with a better calling structure.
e.g. printCal() loops through the months, then make a function printMonth() that is responsible for printing a whole month. from there you can call getDaysInMonth() to get the days in this month and call it again with different parameters to get the days in the previous month. You could give your printMonth function a third parameter startDay and make it return the last day in the month, so you can use the value as starting day for the next month.

I hope you get the idea. It's not a good idea to have all the variables in one function and passing them by reference to all the others. That is close to not using functions at all.
Who needs a signature anyway.
User avatar
exomo
 
Posts: 894
Joined: Fri Sep 26, 2003 12:30 pm
Location: germany->baden

Re: Function Errors In Calendar Program

Postby ethrtyiS » Mon May 14, 2012 2:11 pm

Ah okay so that clears things up and you are right, when I corrected it, it was giving me all these errors I am unfamiliar with. Could you perhaps give me some advice or perhaps walk me through resolving this? I've re-written the program twice already and my professor is extremely unorganized and is very unclear with her teaching methods. I would greatly appreciate it.
ethrtyiS
 
Posts: 7
Joined: Mon May 14, 2012 4:00 am

Re: Function Errors In Calendar Program

Postby exomo » Tue May 15, 2012 5:16 am

Unfortunatley I think you have to rewrite it a third time.
I just made your program into something that works, and I don't use any by reference parameters. But I think I won't give you the complete code just now, there is not much left of your code and you are supposed to learn something. Here are some parts of the program, but you still have to implement the functions.
Code: Select all
#include <iostream>
#include <iomanip>

using namespace std;

void printCal(int year);
int printMonth(int month, int year, int startDay);

string getMonthName(int month);
int getDaysInMonth(int month, int year);
int getFirstDayOfYear(int year);

//Function 1
//Prints Calendar
void printCal(int year)
{
    int startDay = getFirstDayOfYear(year);

    int count;
    for (count = 1; count <= 12; count++)
    {
        startDay = printMonth(count, year, startDay);
    }
}

int getFirstDayOfYear(int year)
{
    int c = year / 100;
    int y = year % 100;

    int sum = 2*(3-(c%4)); // first day of century
    sum += y + (y-1)/4;    // add number of years and number of leap years
    if(c%4==0 && y>0) sum += 1; // add one if the century begins with a leap year

    return sum % 7;
}

int printMonth(int month, int year, int startDay)
{
    int days = getDaysInMonth(month, year);

    // do whatever needs to be done here

    return (startDay + days) % 7;
}

//Function 3
//Checks For Leap Year
bool isLeapYear(int year)
{
    if (year % 400 == 0)
    {
        return true;
    }
    if (year % 100 == 0)
    {
        return false;
    }
    if (year % 4 == 0)
    {
        return true;
    }
    return false;
}

//Returns The Number Of Days In A Month
int getDaysInMonth(int month, int year)
{
   
}

//Returns the name of the month
string getMonthName(int month)
{
   
}

//Main Function
int main()
{
    int year;

    cout << "Please Enter Year To View The Calender: ";
    cin >> year;

    while(year <= 0)
    {
        cout<<"Please Enter A Valid Year (Larger Than 0): ";
        cin >> year;
    }

    printCal(year); // Calls The Function So That It Prints The Calender

    cout << endl;
    return(0);
}

Fill out the empty functions and add more functions if you need them. But be careful not to put code into functions that reuqires the knowledge of too many variables or needs to modify them. I have used two small functions, but I could have added their code to the printMonth() function without making it look too bad.
Of course this code is just a suggestion, not an optimal solution or something. There may be better ways.

Using the complete version of this code I get an output like this:
Code: Select all
Please Enter Year To View The Calender:  2012
January 2012
--------------------
  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  1  2  3  4

    February 2012
--------------------
  S  M  T  W  T  F  S
29 30 31  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  1  2  3

    March 2012
--------------------
  S  M  T  W  T  F  S
26 27 28 29  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

    April 2012
--------------------
  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  1  2  3  4  5

    May 2012
--------------------
  S  M  T  W  T  F  S
29 30  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  1  2

    June 2012
--------------------
  S  M  T  W  T  F  S
27 28 29 30 31  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

    July 2012
--------------------
  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  1  2  3  4

    August 2012
--------------------
  S  M  T  W  T  F  S
29 30 31  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  1

    September 2012
--------------------
  S  M  T  W  T  F  S
26 27 28 29 30 31  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  1  2  3  4  5  6

    October 2012
--------------------
  S  M  T  W  T  F  S
30  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  1  2  3

    November 2012
--------------------
  S  M  T  W  T  F  S
28 29 30 31  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  1

    December 2012
--------------------
  S  M  T  W  T  F  S
25 26 27 28 29 30  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  1  2  3  4  5

I'm not exactly sure about how the calendar should look like, but you can write the output functions so that the calendar looks like you want.
Who needs a signature anyway.
User avatar
exomo
 
Posts: 894
Joined: Fri Sep 26, 2003 12:30 pm
Location: germany->baden

Re: Function Errors In Calendar Program

Postby ethrtyiS » Wed May 16, 2012 2:58 am

Exomo, thanks a lot of helping me out with this. The code you made for me looks great although I am supposed to use a minimum of 3 void functions. How would I go about making those int functions into void functions like my original code?

Here is what we are and aren't suppose to do as per my professor's wishes.

    No global variables
    Pass by parameters
    Leap year check (include century year)
    No Arrays
    Minimum of 3 functions

Basically the output must be like this (Sample Run):

Code: Select all
For what year do you want a calendar: 1900
Please enter the day of the week that January 1st falls on: 1
(Enter 0 for Sunday, 1 for Monday, and so on...)

     January 2012
-------------------------
  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

     February 2012
-------------------------
  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

so on and so forth...


Thanks!
ethrtyiS
 
Posts: 7
Joined: Mon May 14, 2012 4:00 am

Re: Function Errors In Calendar Program

Postby exomo » Wed May 16, 2012 11:53 am

Why do you want the functions to be void? It only says "Minimum of 3 functions", there is nothing about the return type here. Of course you can set the return type of any function to void and give it an additional "by reference" parameter, but I don't see why one would want to do this. You could just add some functions that don't need to return a value, such as printSpaces(int num), or your dayNames() function. I removed this one because I don't think printing two lines is a task worth a own function, but if you need more functions you can put like every line in a function of its own. Just try to avoid functions that require to change variables of the caller function.
"No Arrays", why that? I used some arrays to look up month names and days per month, it makes things a lot easier. But if you are not allowed to use them you have to go for endless switch-case or if else if ... code.

example of printMonth without return value:
Code: Select all
void printMonth(int month, int year, int & startDay)
{
    int days = getDaysInMonth(month, year);

    // do whatever needs to be done here

    startDay = startDay + days % 7;
}
Who needs a signature anyway.
User avatar
exomo
 
Posts: 894
Joined: Fri Sep 26, 2003 12:30 pm
Location: germany->baden

Re: Function Errors In Calendar Program

Postby ethrtyiS » Thu May 17, 2012 7:26 pm

We must use void functions because this is what our professor wants us to use. I'm not sure why she wants us to use such limited and difficult ways to approach this. We can't use arrays yet because she has not covered them yet. If I were to use arrays, I would get major points deducted off of my grade. We are something like 2 or 3 lectures behind and our final is like 2 weeks away.

I am going to try and rewrite my program for a third time and post it up here later, tonight or tomorrow, so that you may take a look at it and let me know if there are things I should simplify or change.

Thanks again!
ethrtyiS
 
Posts: 7
Joined: Mon May 14, 2012 4:00 am

Re: Function Errors In Calendar Program

Postby ethrtyiS » Fri May 18, 2012 3:06 am

Okay here is what I got after rewriting my program. I'm confused now on the last function that you put. Can you help me fill in what I am supposed to put in there? I have to turn this in tonight and I'm stressing out.

Code: Select all
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

void printCal(int);
int printMonth(int, int, int, int);

string getMonthName(int);
int getDaysInMonth(int, int, int, int);
int getFirstDayOfYear(int);


int main() //Main Function - Asking User And Error Checking As Well As Outputting
{
    int year;
   int startDay;

    cout << "Please Enter Year To View The Calender: ";
    cin >> year;

   //Check To Make Sure User Entered A Correct Year
    while(year <= 0)
    {
        cout<<"Please Enter A Valid Year (Larger Than 0): ";
        cin >> year;
    }

   cout << endl << "0 - Sunday" << endl << "1 - Monday" << endl << "2 - Tuesday" << endl << "3 - Wednesday" << endl << "4 - Thursday" << endl << "5 - Friday" << endl << "6 - Saturday" << endl << endl;
   cout << "Please Enter The Day Of The Week January 1st Falls On (See Above): ";
   cin >> startDay;   
   cout << endl << endl << endl;

   //Check To Make Sure User Entered A Correct Day
   while ((startDay < 0) && (startDay > 6))
   {
      cout << endl << endl << endl;
      cout << "You Entered An Incorrect Day. Please Enter An Integer Between 0-6.";
      cin >> startDay;
   }

    printCal(year); // Calls The Function So That It Prints The Calender

    cout << endl;
    system("pause");
}

void printCal(int year) //Prints Calendar
{
    int startDay = getFirstDayOfYear(year);

    int count;
    for (count = 1; count <= 12; count++)
    {
        startDay = printMonth(count, year);
    }
}

int getFirstDayOfYear(int year) //Checking The First Day Of The Year
{
    int c = year / 100;
    int y = year % 100;

    int sum = 2 * (3 - (c % 4)); // First Day Of Century
    sum += y + (y - 1) / 4;    // Add Number Of Years And Number Of Leap Years
    if(c % 4 == 0 && y > 0) sum += 1; // Add One If The Century Begins With A Leap Year

    return sum % 7;
}

int printMonth(int count, int month, int year, int startDay) //Prints Each Month And Year
{
    int days = getDaysInMonth(month, year);

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

    return (startDay + days) % 7;
}

bool leapYear(int year) //Checks For Leap Year
{
    if (year % 400 == 0)
    {
        return true;
    }
    if (year % 100 == 0)
    {
        return false;
    }
    if (year % 4 == 0)
    {
        return true;
    }
    return false;
}

int getDaysInMonth( int count, int month, int year, int spacerDays) //Returns The Number Of Days In A Month
{
   switch (count)
   {
   case 1:
      month = 31;
      break;
   case 2:
      if(leapYear(year))
         //Check For Leap Year
         month = 29;
      if(!leapYear(year))
         month = 28;
      spacerDays = 31;
      break;
   case 3:
      month = 31;
      if(leapYear(year))
         //Check For Leap Year
         spacerDays = 29;
      if(!leapYear(year))
         spacerDays = 28;
      break;
   case 4:
      month = 30;
      spacerDays = 31;
      break;
   case 5:
      month = 31;
      spacerDays = 30;
      break;
   case 6:
      month = 30;
      spacerDays = 31;
      break;
   case 7:
      month = 31;
      spacerDays = 30;
      break;
   case 8:
      month = 31;
      spacerDays = 31;
      break;
   case 9:
      month = 30;
      spacerDays = 31;
      break;
   case 10:
      month = 31;
      spacerDays = 30;
      break;
   case 11:
      month = 30;
      spacerDays = 31;
      break;
   case 12:
      month = 31;
      spacerDays = 30;
      break;
   }
}

string getMonthName(int month) //Returns The Name Of The Month
{
   
}
ethrtyiS
 
Posts: 7
Joined: Mon May 14, 2012 4:00 am

Re: Function Errors In Calendar Program

Postby exomo » Fri May 18, 2012 12:07 pm

You code doesn't much look like a redesign, it's more like the same code you posted the first timed moved into my functions.
Code: Select all
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

void printCal(int, int);
int printMonth(int, int, int);

string getMonthName(int);
int getDaysInMonth(int, int);


int main() //Main Function - Asking User And Error Checking As Well As Outputting
{
    int year;
    int startDay;

    cout << "Please Enter Year To View The Calender: ";
    cin >> year;

    //Check To Make Sure User Entered A Correct Year
    while(year <= 0)
    {
        cout<<"Please Enter A Valid Year (Larger Than 0): ";
        cin >> year;
    }

    cout << endl << "0 - Sunday" << endl << "1 - Monday" << endl << "2 - Tuesday" << endl << "3 - Wednesday" << endl << "4 - Thursday" << endl << "5 - Friday" << endl << "6 - Saturday" << endl << endl;
    cout << "Please Enter The Day Of The Week January 1st Falls On (See Above): ";
    cin >> startDay;
    cout << endl << endl << endl;

    //Check To Make Sure User Entered A Correct Day
    while ((startDay < 0) && (startDay > 6))
    {
        cout << endl << endl << endl;
        cout << "You Entered An Incorrect Day. Please Enter An Integer Between 0-6.";
        cin >> startDay;
    }

    printCal(year, startDay); // Calls The Function So That It Prints The Calender

    cout << endl;
//        system("pause");
}

void printCal(int year, int startDay) //Prints Calendar
{
    int count;
    for (count = 1; count <= 12; count++)
    {
        startDay = printMonth(count, year, startDay);
    }
}

void printSpacer(int days)
{
    for(int i=0; i<days; i++) cout << "   ";
}

void printWeek(int weekStart, int weekEnd)
{
    for(int d=weekStart; d<=weekEnd; d++)
    {
        cout << setw(3) << d;
    }
    cout << "\n";
}

int printMonth(int month, int year, int startDay) //Prints Each Month And Year
{
    int days = getDaysInMonth(month, year);

    cout << "      " << getMonthName(month) << " " << year << endl;
    cout << "----------------------\n"
            "  S  M  T  W  T  F  S \n";

    printSpacer(startDay);
    int weekStart = 1;
    int weekEnd = 7 - startDay;
    while(weekStart <= days)
    {
        printWeek(weekStart, weekEnd);
        weekStart = weekEnd + 1;
        weekEnd = min(weekStart + 6, days);
    }

    cout << endl;

    return (startDay + days) % 7;
}

bool leapYear(int year) //Checks For Leap Year
{
    if (year % 400 == 0)
    {
        return true;
    }
    if (year % 100 == 0)
    {
        return false;
    }
    if (year % 4 == 0)
    {
        return true;
    }
    return false;
}

int getDaysInMonth( int month, int year) //Returns The Number Of Days In A Month
{
    switch (month)
    {
    case 1:
        return 31;
        break;
    case 2:
        if(leapYear(year))
            //Check For Leap Year
            return 29;
        if(!leapYear(year))
            return 28;
        break;
    case 3:
        return 31;
        break;
    case 4:
        return 30;
        break;
    case 5:
        return 31;
        break;
    case 6:
        return 30;
        break;
    case 7:
        return 31;
        break;
    case 8:
        return 31;
        break;
    case 9:
        return 30;
        break;
    case 10:
        return 31;
        break;
    case 11:
        return 30;
        break;
    case 12:
        return 31;
        break;
    }
}

string getMonthName(int month) //Returns The Name Of The Month
{
    switch (month)
    {
    case 1:
        return "January";
        break;
    case 2:
        return "February";
        break;
    case 3:
        return "March";
        break;
    case 4:
        return "April";
        break;
    case 5:
        return "May";
        break;
    case 6:
        return "June";
        break;
    case 7:
        return "July";
        break;
    case 8:
        return "August";
        break;
    case 9:
        return "September";
        break;
    case 10:
        return "October";
        break;
    case 11:
        return "November";
        break;
    case 12:
        return "December";
        break;
    }
    return "invalid number";
}

Here I "fixed" your code. I added some void functions where I think it fits, just to make your prof happy.
The last function was just meant to return the name of a month by its number. You don't need it when you have the switch-case in printMonth(). And you don't need getFirstDayOfYear(). I used this one to get the first day of a year, so I don't need the input for it. But if you have to use the day input it's of no use for you.
Who needs a signature anyway.
User avatar
exomo
 
Posts: 894
Joined: Fri Sep 26, 2003 12:30 pm
Location: germany->baden

Re: Function Errors In Calendar Program

Postby ethrtyiS » Fri May 18, 2012 1:09 pm

Wow, thanks so much, you've really helped a lot. Two last questions, if I input a number other than 0-6 for the days, it still executes the output of the months. Not sure why it isn't picking up my check within the main function. Could you tell me why that is, it seems like it's just going right over the while loop.

Also, I am getting a warning that reads:

Code: Select all
warning C4715: 'getDaysInMonth' : not all control paths return a value - 160


It's weird because each return command has a value that it's returning.
ethrtyiS
 
Posts: 7
Joined: Mon May 14, 2012 4:00 am

Re: Function Errors In Calendar Program

Postby exomo » Fri May 18, 2012 1:38 pm

The input check fails because the condition is wron. No number can ever be <0 AND >6. You want the or || operator in there.

The warning is right, there are paths that don't end in a return statement. It works for every valid number, but there is no return statement for any other number, such as 13, .... To make the warning disappear you can add a return statement after the switch-case. The compiler does not know that the numbers will be within a small range. It is always important to have a well-defined return value, that's why you should never ignore warnings.
Who needs a signature anyway.
User avatar
exomo
 
Posts: 894
Joined: Fri Sep 26, 2003 12:30 pm
Location: germany->baden

Re: Function Errors In Calendar Program

Postby ethrtyiS » Fri May 18, 2012 2:44 pm

Doh, how did I miss that. Okay I will fix that and add a return statement after the switch-case. Thanks again Exomo, you have been a great help, I appreciate it.
ethrtyiS
 
Posts: 7
Joined: Mon May 14, 2012 4:00 am


Return to For Beginners

Who is online

Users browsing this forum: Google [Bot] and 2 guests