Need help with simple coding... Days, Months problem

Hello, I am in need of help with a function. I wrote this program in order to display the number of days in a month according to my 12 element array. I wrote it using an if else statement, however, I have to alter it into a function named "displayMonthDays". The output should look like this "Month #2 has 28 days."

This is the program before any alterations:
#include <iostream>
using namespace std;

int main()
{

int num;
int days[12] = {31,28,31,30,31,30,31,31,30,31,30,31};


cout << "Enter the month number: ";
cin >> num;

// display days per month

if(num > 0 && num <= 12)
{
--num;
cout << "Month #" << num+1 << " has " << days[num] << " days." << endl;
}
else
{
cout << "Invalid month" << endl;
}

system("pause");
return 0;
}

This is my first attempt at it:(have no idea what I'm doing here)
#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}

#include <iostream>
using namespace std;

void displayMonthDays(int days[]);

int main()
{

int num = 0;
int days[12] = {31,28,31,30,31,30,31,31,30,31,30,31};


cout << "Enter the month number: ";
cin >> num;

// display days per month
num += 1;
displayMonthDays(num);

system("pause");
return 0;
}
void displayMonthDays(int days[], int num)
{

if(num > 0 && num <= 12)
{
--num;
cout << "Month #" << num << " has " << days[num] << " days." << endl;
}
else
{
cout << "Invalid month" << endl;
}
Well you're not far off, assuming you're working from your #include <iostream> and down.

First thing your function declaration is off, your actual implementation has displayMonthDays taking two parameters, your first declaration has it taking in one, so that should be changed to:
void displayMonthDays(int days[], int num);

Second, when you call your function you're only passing it num, when you need to pass it the array AND num, so:
displayMonthDays(days, num);

And lastly you don't need to increment num by 1 right before your function call so you might want to take out the num += 1; line
Thanks a ton! I'm self taught so a lot of this is new to me, just got started on arrays.
Topic archived. No new replies allowed.