Array

Trying to complete a program that displays number of days in a month using an array. Need to display the number of days in the month corresponding to the number entered example 1 is 31 and 2 is 28 etc.Here is what I have so far:

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

//function prototypes
void displayDays (int month);

int main()
{
//declare array and variable
int daysArray[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };



//call function
displayDays [int month];
return 0;
} //end of main function


//*****function definition*****
void displayDays(int month)

{
int month= 0;
int daysArray[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
//get month, then display
cout << "Enter number of the month: ";
cin >> month;
while (month != -1)
{
else if (month >=1 && <= 12)
cin<< daysArray[month -1] << endl;
else
cout << " Invaild month number please use 1-12 " << endl;
}//end if
} //end of displayDays function



return 0;
} //end of main function
Unused array:
1
2
3
4
int main()
{
//declare array and variable
int daysArray[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

Syntax error:
1
2
//call function
displayDays [int month];

Logic error:
1
2
3
4
5
6
7
8
void displayDays(int month)

{
int month= 0; //this is the parameter, don't declare it again
int daysArray[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
//get month, then display
cout << "Enter number of the month: ";
cin >> month;

You should read input outside the function and pass it as argument
Redid it but now i'm getting a syntax error on 'else'.

#include <iostream>

using std::cout;
using std::cin;
using std::endl;


int main()
{
//declare array and variable
int month = 0;
int daysArray[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

//get month, then display

cout << "Enter number of the month: ";
cin >> month;
while (month != -1)
{
if (month >= 1 && month <= 12 )

cout << daysArray[month -1] <<

else
cout << " Invaild month number please use 1-12 " << endl;
//end if
}//end while



return 0;
} //end of main function
looks like you forgot an endl at the end of the cout after the if check.
Yeah; Warnis is right. Remember: just because the compiler says the error is on line n; doesn't mean it is. It's very possible that the mistake is on line n - 1 ( the line before n) but the compiler reports it as being on the next line.
that was it thanks all.
Topic archived. No new replies allowed.