Array help

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2006
Posts: 23
Reputation: jlouang is an unknown quantity at this point 
Solved Threads: 1
jlouang jlouang is offline Offline
Newbie Poster

Array help

 
0
  #1
Apr 6th, 2006
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





  1. /****************************
  2. * prototypes //declared function so main can find
  3. *****************************/
  4. void printMonth(int m);
  5.  
  6.  
  7. /**************************************
  8. *
  9. ***************************************/
  10.  
  11.  
  12. char month[13] =
  13. {"January","Febuary","March", "April", "May", "June", "July", "August",
  14. "September", "October", "November", "December"};
  15.  
  16. int main()
  17. {
  18. int month;
  19. int day;
  20. int year;
  21. cout << "enter the month: ";
  22. cin >> month;
  23. cout << "enter the day: ";
  24. cin >> day;
  25. cout << "enter the \year: ";
  26. cin >> year;
  27. cin.ignore();
  28.  
  29. cout << month << " " << day << " " << year << "\n";
  30.  
  31. cin.get();
  32.  
  33. system("pause");
  34. }
  35. void printMonth(int m)
  36. {
  37. cout << month[m] << "\n";
  38. }
Last edited by jlouang; Apr 6th, 2006 at 1:18 am. Reason: help
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 23
Reputation: jlouang is an unknown quantity at this point 
Solved Threads: 1
jlouang jlouang is offline Offline
Newbie Poster

Re: Array help

 
0
  #2
Apr 6th, 2006
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"};
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Array help

 
0
  #3
Apr 7th, 2006
> 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 );
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 598
Reputation: anupam_smart is an unknown quantity at this point 
Solved Threads: 4
anupam_smart's Avatar
anupam_smart anupam_smart is offline Offline
Posting Pro

Re: Array help

 
0
  #4
Apr 7th, 2006
Originally Posted by jlouang
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





  1. /****************************
  2. * prototypes //declared function so main can find
  3. *****************************/
  4. void printMonth(int m);
  5.  
  6.  
  7. /**************************************
  8. *
  9. ***************************************/
  10.  
  11.  
  12. char month[13] =
  13. {"January","Febuary","March", "April", "May", "June", "July", "August",
  14. "September", "October", "November", "December"};
  15.  
  16. int main()
  17. {
  18. int month;
  19. int day;
  20. int year;
  21. cout << "enter the month: ";
  22. cin >> month;
  23. cout << "enter the day: ";
  24. cin >> day;
  25. cout << "enter the \year: ";
  26. cin >> year;
  27. cin.ignore();
  28.  
  29. cout << month << " " << day << " " << year << "\n";
  30.  
  31. cin.get();
  32.  
  33. system("pause");
  34. }
  35. void printMonth(int m)
  36. {
  37. cout << month[m] << "\n";
  38. }


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++

Moreover...
U are using char month[13] to store names of 12 months.
but this will store a string of length 12 characters only?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Array help

 
0
  #5
Apr 7th, 2006
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()
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 23
Reputation: jlouang is an unknown quantity at this point 
Solved Threads: 1
jlouang jlouang is offline Offline
Newbie Poster

Re: Array help

 
0
  #6
Apr 11th, 2006
o gotcha thanks
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 23
Reputation: jlouang is an unknown quantity at this point 
Solved Threads: 1
jlouang jlouang is offline Offline
Newbie Poster

Re: Array help

 
0
  #7
Apr 12th, 2006
ok I'm trying to compile the leap year thing but the if else not showing?

here my compile
  1.  
  2. #include <iostream> // needed for cin/count
  3. #include <cmath> // needed for math functions
  4. using namespace std;
  5.  
  6. /****************************
  7. * prototypes //declared function so main can find
  8. *****************************/
  9. void printMonth(int m);
  10. void calcDOY(int month, int day, int year);
  11.  
  12.  
  13. /**************************************
  14. *
  15. ***************************************/
  16.  
  17.  
  18. int day[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
  19.  
  20.  
  21.  
  22. int main()
  23. {
  24. int month;
  25. int day;
  26. int year;
  27. cout << "enter the month: ";
  28. cin >> month;
  29. cout << "enter the day: ";
  30. cin >> day;
  31. cout << "enter the year: ";
  32. cin >> year;
  33.  
  34. if ((year % 400)==0){
  35. cout << "This a leap year\n";
  36. }
  37.  
  38. else if ((year % 100)==0){
  39. cout << "This is not a leap year \n";
  40. }
  41.  
  42.  
  43.  
  44. cin.get();
  45.  
  46. system("pause");
  47. }
  48. void printMonth(int m)
  49. {
  50. cout << day[m] << "\n";
  51. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Array help

 
0
  #8
Apr 12th, 2006
Can you paste a log of your run, showing the prompts being printed and the values you typed in?
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 23
Reputation: jlouang is an unknown quantity at this point 
Solved Threads: 1
jlouang jlouang is offline Offline
Newbie Poster

Re: Array help

 
0
  #9
Apr 12th, 2006
hmm I fix it alittle but the leap years still printing

  1. #include <iostream> // needed for cin/count
  2. #include <cmath> // needed for math functions
  3. using namespace std;
  4.  
  5. /****************************
  6. * prototypes //declared function so main can find
  7. *****************************/
  8. void printMonth(int m);
  9. void getDOY(int m, int d, int y);
  10.  
  11.  
  12.  
  13. /**************************************
  14. *
  15. ***************************************/
  16.  
  17.  
  18. int month[] = {31,28,31,30,31,30,31,31,30,31,30,31};
  19.  
  20.  
  21.  
  22. int main()
  23. {
  24. int month;
  25. int day;
  26. int year;
  27. cout << "enter the month: ";
  28. cin >> month;
  29. cout << "enter the day: ";
  30. cin >> day;
  31. cout << "enter the year: ";
  32. cin >> year;
  33.  
  34. getDOY( month, day, year);
  35.  
  36. return 0;
  37.  
  38.  
  39. system("pause");
  40. }
  41. void printMonth(int m)
  42. {
  43. cout << month[m] << "\n";
  44.  
  45. }
  46. void getDOY(int m, int d, int y)
  47. {
  48. if ((year % 400)==0){
  49. cout << "This a leap year\n";
  50. }
  51.  
  52. if ((year % 100)==0){
  53. cout << "This is not a leap year \n";
  54. }
  55. return 0;
  56. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Array help

 
0
  #10
Apr 12th, 2006
> 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?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC