when i run my program, it lets me input the first value for the month (of type int) but then when i go to input the 3 character line it just gives me the same result as per the first value entered. here's what the output looks like:
-----------------------------------------------------------
Enter the month as a number:
5
May
Enter the month as the first three letters:
Jan
The month is 5
Press any key to continue
-----------------------------------------------------------
i'll attach my code so you guys can try to help me:
Code:#include <iostream> using namespace std; class Month { public: Month(char first, char second, char third); Month(int number); Month(); void input_month_int(istream& cin); void input_month_alpha(istream& cin); void output_month_int(ostream& cout); void output_month_alpha(ostream& cout); private: int month_number; }; int main() { Month a_month; cout<<"Enter the month as a number:" <<endl; a_month.input_month_int(cin); a_month.output_month_alpha(cout); cout<<"Enter the month as the first three letters:" <<endl; a_month.input_month_alpha(cin); cout<<endl; a_month.output_month_int(cout); return 0; } Month::Month(char first, char second, char third) { if (((first=='J')||(first=='j')) && (second=='a') && (third=='n')) month_number=1; if (((first=='F')||(first=='f')) && (second=='e') && (third=='b')) month_number=2; if (((first=='M')||(first=='m')) && (second=='a') && (third=='r')) month_number=3; if (((first=='A')||(first=='a')) && (second=='p') && (third=='r')) month_number=4; if (((first=='M')||(first=='m')) && (second=='a') && (third=='y')) month_number=5; if (((first=='J')||(first=='j')) && (second=='u') && (third=='n')) month_number=6; if (((first=='J')||(first=='j')) && (second=='u') && (third=='l')) month_number=7; if (((first=='A')||(first=='a')) && (second=='u') && (third=='g')) month_number=8; if (((first=='S')||(first=='s')) && (second=='e') && (third=='p')) month_number=9; if (((first=='O')||(first=='o')) && (second=='c') && (third=='t')) month_number=10; if (((first=='N')||(first=='n')) && (second=='o') && (third=='v')) month_number=11; if (((first=='D')||(first=='d')) && (second=='e') && (third=='c')) month_number=12; }; Month::Month(int number) { month_number=number; }; Month::Month() { }; void Month::input_month_int(istream& cin) { cin>>month_number; } void Month::input_month_alpha(istream& cin) { char first, second, third; cin.get(first); cin.get(second); cin.get(third); Month some_month(static_cast<char>(first), static_cast<char>(second), static_cast<char>(third)); if (((first=='J')||(first=='j')) && (second=='a') && (third=='n')) month_number=1; if (((first=='F')||(first=='f')) && (second=='e') && (third=='b')) month_number=2; if (((first=='M')||(first=='m')) && (second=='a') && (third=='r')) month_number=3; if (((first=='A')||(first=='a')) && (second=='p') && (third=='r')) month_number=4; if (((first=='M')||(first=='m')) && (second=='a') && (third=='y')) month_number=5; if (((first=='J')||(first=='j')) && (second=='u') && (third=='n')) month_number=6; if (((first=='J')||(first=='j')) && (second=='u') && (third=='l')) month_number=7; if (((first=='A')||(first=='a')) && (second=='u') && (third=='g')) month_number=8; if (((first=='S')||(first=='s')) && (second=='e') && (third=='p')) month_number=9; if (((first=='O')||(first=='o')) && (second=='c') && (third=='t')) month_number=10; if (((first=='N')||(first=='n')) && (second=='o') && (third=='v')) month_number=11; if (((first=='D')||(first=='d')) && (second=='e') && (third=='c')) month_number=12; } void Month::output_month_int(ostream& cout) { cout<<"The month is " <<month_number <<endl; } void Month::output_month_alpha(ostream& cout) { if (month_number==1) cout<<"Jan" <<endl; if (month_number==2) cout<<"Feb" <<endl; if (month_number==3) cout<<"Mar" <<endl; if (month_number==4) cout<<"Apr" <<endl; if (month_number==5) cout<<"May" <<endl; if (month_number==6) cout<<"Jun" <<endl; if (month_number==7) cout<<"Jul" <<endl; if (month_number==8) cout<<"Aug" <<endl; if (month_number==9) cout<<"Sep" <<endl; if (month_number==10) cout<<"Oct" <<endl; if (month_number==11) cout<<"Nov" <<endl; if (month_number==12) cout<<"Dec" <<endl; }



LinkBack URL
About LinkBacks



i just started taking programming about 2 months ago, and i haven't had much time to go beyond our classwork yet...... so i'm just doing this with what we've been taught so far. apparently, from talking to my programming friends, there are many easier ways to do many of the things that we've done in class so far (kind of like in my calc class, where i've taken it before...... hate those integrals as limits of sums.....)