I don't know if I read the book right but my array is not working on my months .My instructions is

this assignment uses an array of strings:

write a C program that asks the user for the month, day, and year

read in the month, day and year (as ints)

the function header should be void PrintMonth(int month)

the function accepts the month as a parameter and prints out the proper month name

the function does not have to verify a valid date

best approch is to declare an array of 12 strings and initialize the elements of the array with the proper month name

/****************************
*    prototypes    //declared function so main can find
*****************************/
void printMonth(int m);


/**************************************
*
***************************************/


char month[13] =
{"January","Febuary","March", "April", "May", "June", "July", "August", 
"September", "October", "November", "December"};
 
 int main()
{
     int month;
     int day;
     int year;
   cout << "enter the month: ";
   cin >> month;
   cout << "enter the day: ";
   cin >> day;
   cout << "enter the \year: ";
   cin >> year;
   cin.ignore();
   
   cout << month << " " << day << " " << year << "\n";
   
   cin.get();
 
system("pause");
}
void printMonth(int m)
{
     cout << month[m] << "\n";
}

Recommended Answers

All 10 Replies

would anything happen if I change my array to a constant?

char* month[100] =
{"January","Febuary","March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"};

> char month[13]
Try

char *month[12]={"January","Febuary","March", "April", "May", "June", "July", "August", 
"September", "October", "November", "December"};

> cout << month << " " << day << " " << year << "\n";
You also need to actually call the function as well, say printMonth( month );

I don't know if I read the book right but my array is not working on my months .My instructions is

this assignment uses an array of strings:

write a C program that asks the user for the month, day, and year

read in the month, day and year (as ints)

the function header should be void PrintMonth(int month)

the function accepts the month as a parameter and prints out the proper month name

the function does not have to verify a valid date

best approch is to declare an array of 12 strings and initialize the elements of the array with the proper month name

/****************************
*    prototypes    //declared function so main can find
*****************************/
void printMonth(int m);


/**************************************
*
***************************************/


char month[13] =
{"January","Febuary","March", "April", "May", "June", "July", "August", 
"September", "October", "November", "December"};
 
 int main()
{
     int month;
     int day;
     int year;
   cout << "enter the month: ";
   cin >> month;
   cout << "enter the day: ";
   cin >> day;
   cout << "enter the \year: ";
   cin >> year;
   cin.ignore();
   
   cout << month << " " << day << " " << year << "\n";
   
   cin.get();
 
system("pause");
}
void printMonth(int m)
{
     cout << month[m] << "\n";
}

:confused:
Cann't understand ur question & the code u hav given.
U r asking for a 'C' program, but i think cin & cout are used in C++
:confused:
Moreover...
U are using char month[13] to store names of 12 months.
but this will store a string of length 12 characters only?
:confused:

First, don't have two variables with the same name:

int month;
char * month[12];

So let's say you change:

int month;

to

int num;

Then to use the cout << syntax it would be

cout << month[num] << " " << day << " " << year << \n;

or if you want to use your function the equivalent syntax to the above would be:

printMonth(num);
cout << " " << day << " " << year << \n;

assuming you drop the newline char at the end of the cout statement in printMonth()

o gotcha thanks

ok I'm trying to compile the leap year thing but the if else not showing?

here my compile

#include <iostream>                       // needed for cin/count
#include <cmath>                         // needed for math functions
using namespace std;

/****************************
*    prototypes    //declared function so main can find
*****************************/
void printMonth(int m);
void calcDOY(int month, int day, int year); 


/**************************************
*
***************************************/


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


 
 int main()
{
     int month;
     int day;
     int year;
   cout << "enter the month: ";
   cin >> month;
   cout << "enter the day: ";
   cin >> day;
   cout << "enter the year: ";
   cin >> year;
   
   if ((year % 400)==0){
   cout << "This  a leap year\n";
   } 

   else if ((year % 100)==0){
   cout << "This is not a leap year \n";
   }

   

cin.get();
 
system("pause");
}
void printMonth(int m)
{
     cout << day[m] << "\n";
}

Can you paste a log of your run, showing the prompts being printed and the values you typed in?

hmm I fix it alittle but the leap years still printing

#include <iostream>                       // needed for cin/count
#include <cmath>                         // needed for math functions
using namespace std;

/****************************
*    prototypes    //declared function so main can find
*****************************/
void printMonth(int m);
void getDOY(int m, int d, int y);



/**************************************
*
***************************************/


int month[] = {31,28,31,30,31,30,31,31,30,31,30,31};


 
 int main()
{
   int month;
   int day;
   int year;
   cout << "enter the month: ";
   cin >> month;
   cout << "enter the day: ";
   cin >> day;
   cout << "enter the year: ";
   cin >> year;
   
   getDOY( month, day, year);
   
   return 0;

 
system("pause");
}
void printMonth(int m)
{
     cout << month[m] << "\n";
    
}
void getDOY(int m, int d, int y)
{
   if ((year % 400)==0){
   cout << "This  a leap year\n";
   } 

   if ((year % 100)==0){
   cout << "This is not a leap year \n";
   }  
   return 0;
}

> if ((year % 400)==0)
How is this even compiling, when all you have is a parameter called 'y' ?

> return 0;
How is this even compiling, when you declared it as returning void?

after a whole week. I found out the problem and I can't believe it was simple.

#include <iostream>                       // needed for cin/count
#include <cmath>                         // needed for math functions
using namespace std;

/****************************
*    prototypes    //declared function so main can find
*****************************/
int isLeapYear(int year); 

/**************************************
*
***************************************/


int main()
{
          int x;
          int y; 
          int z;
          cout << " Enter the month, day, and year\n ";
	      cin  >> x >> y >> z;
	      
	      if (isLeapYear(z))
	      cout << "Leap Year\n";
	      else 
          cout << "Not Leap Year\n";
          
	      cin.get();
	      system("pause");
}
int isLeapYear(int year)
    {
    bool statement;
       if ( (year%400) == 0 )                
          statement = true;  
       else if ( (year%100) == 0 )        
          statement = false;                 
       else if ( (year%4) == 0 )       
             statement = true;                             
       else                              
           statement = true;    
       return statement;            
           }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.