really need help

done.
Last edited on
A way to improve it would be to not use global variables, those are bad. I would move the array of months into main, and pass it to the function.
http://bfy.tw/xAT


Another thing is using std:: instead of using namespace std;
http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice


Example:
1
2
3
int x;
std::cin >> x;
std::cout << "Hello World" << x << std::endl;
Last edited on
thank you for your help however i am required to have a global variable. Just wondering if there are anything that i can change it to make it simpler?
Comment your code.

Make totalDays a static. That way it is only initialized once. That means that you'll need a separate variable to hold the number of days for the current month. E.g.:
1
2
3
4
int numDays = totaldays[month];
if (month == 2 && leap) {
    numDays++;
}


End a line with \n instead of endl. endl flushes the stream which really slows the program down. When I put a loop in main to run your code 100,000 times and send the output to /dev/null, the run time is:
real    0m22.305s
user    0m10.717s
sys     0m11.450s

Replacing endl the time changes to:
real    0m8.640s
user    0m8.533s
sys     0m0.000s


Move system("PAUSE") outside of printmonth(); (put it at the end of main() if you really need it)
Topic archived. No new replies allowed.