A couple errors in printmonth program c++

I'm having some trouble figuring out how to fix my errors.
Here is my program being used to print a calendar for any year after 1582
http://pastebin.com/gGk50Vdm
I'm receiving the errors at line(s):

Line (74) warning C4800: 'const int' : forcing value to bool 'true' or 'false' (performance warning)

Line (90) error C2065: 'd' : undeclared identifier

Any help would be appreciated
The only problem is you aren't assigning your return values from your functions to anything, so they never get counted.

For example at line 74 it should be bool leap = isleapyear(year); instead of bool isleapyear(year); . Now you have a variable called 'leap' in main that you can use (which is all set up correctly as it is now).

On line 77 change if(leap = true) to if(leap == true) so it is comparing the 2 values not assigning them.

Line 87 is the same thing as 74. Simply do int d = dayofweek(1, i, year);

Everything else works correctly by the look of it.
(Apart from the formatting only seems to work for the first few months, but I am sure that should be pretty easy to fix)
Last edited on
Short lesson here!

All variables in C++ have what is called 'scope'. This basically means that variables are only valid within certain sections of code.

The most basic example of scope is within functions.

For example:

1
2
3
4
5
6
7
8
9
10
11
12
void lolfunction()
{
  int a = 5; // This variable is only accessible WITHIN this function.
  return;
}

int main()
{
  lolfunction();
  // You can't access 'a' here! Its scope is only in lolfunction.
  return 0;
}


Multiple times in your code, you've tried to call functions, before using variables whose scope is only within the functions you just called. You're doing things like this:

1
2
3
4
5
6
7
8
9
10
11
12
void lolfunction()
{
  int a = 5; // This variable is only accessible WITHIN this function.
  return;
}

int main()
{
  lolfunction();
  int b = a; // the variable 'a' can't be used here (out of scope), so this is an error.
  return 0;
}


Instead, your functions should return a value which can then be stored in another variable. For example, at line 74 in your code, you should use this:

 
bool leap = isleapyear(year);


Where leap is a new variable, assigned to the value returned by isleapyear. You can then use this variable for code in printyear.

I think you can read more about variable scope in the C++ Documentation on this website. (http://www.cplusplus.com/doc/)
Thanks for the responses, not at my home computer but will try these methods when I get home.
Hi kbc08,

This first point is a technical style one:

 
using namespace std;


This brings in all kinds of things into your program that aren't neccessary. If you are only using cout and cin, then you do this instead:

1
2
using std::cout;
using std::cin;


This won't make any difference to the running of your program, but makes it easier on the compiler / optimiser.

Now for the array of month names:

1
2
3
const string MONTHS[13] = {" ", "Januray", "February", "March", "April", "May", "June", "July",
                                                   "August", "September", "October", "November", "December"};


The best thing to do, is declare them as an enum with january = 1

If you did want to keep them as an array and you declare and initialise on the same line, you can leave the index size in the [] blank, as the compiler will count them for you.

I am guessing you had the size as 13 to get over the problem of the array starting at zero. Using the enum will get over this problem, although it's not that hard to code with arrays that start at 0.

With the isleapyear function, it is more complicated than every 4 years. A clue is that the rules are different every 100 & 400 years. Look on Wiki.

With the dayofweek function there is a problem with integer division.


What does this do?

1
2
for startday>>num.days


In function printyear, look at where the variable i is set, then look at the other places it is used. Where is the value of i changed? Is that what you want? Look at the index expressions of the for loops, and decide if they are being used properly inside the for loops.

One problem is the names you have for variables. I never use i (or any single letter)for a loop counter, it is too easily confused with one or j. It is much better to use a word (like you have with index etc) but that it is even better to qualify it further by saying what sort of counter or index that it is. E.g. use DayCounter, MonthCounter etc. Google C++ coding standard (not the C++ ANSI standard) to see how professional programmers do it. There is one at LinuxQuestions.org

Let us know how you go.

TheIdeasMan
Topic archived. No new replies allowed.