| | |
Array help
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Feb 2006
Posts: 23
Reputation:
Solved Threads: 1
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
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
C++ Syntax (Toggle Plain Text)
/**************************** * 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"; }
Last edited by jlouang; Apr 6th, 2006 at 1:18 am. Reason: help
•
•
•
•
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
C++ Syntax (Toggle Plain Text)
/**************************** * 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"; }
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?
•
•
Join Date: Jul 2005
Posts: 1,678
Reputation:
Solved Threads: 263
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()
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()
•
•
Join Date: Feb 2006
Posts: 23
Reputation:
Solved Threads: 1
ok I'm trying to compile the leap year thing but the if else not showing?
here my compile
here my compile
C++ Syntax (Toggle Plain Text)
#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"; }
•
•
Join Date: Feb 2006
Posts: 23
Reputation:
Solved Threads: 1
hmm I fix it alittle but the leap years still printing
C++ Syntax (Toggle Plain Text)
#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; }
![]() |
Similar Threads
- Can I ghost a RAID array??? (Windows NT / 2000 / XP)
- Creating dynamic array structures (C++)
- Array limit (C)
- struct dynamic 2d array alloc (C)
- string to integer array transformation (C)
- Array (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: Does anyone have the stomach to look over my program?
- Next Thread: C++ HTTP post/get submitting and communication
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






