Thread: can't get it to reassign the private variable...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    69

    can't get it to reassign the private variable...

    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;
    }

  2. #2
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Code:
    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;
    /me winces

    use strcmp() for char* or operator== with string objects
    hello, internet!

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    69
    sorry 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.....)

  4. #4
    ..............
    Guest
    Originally posted by talz13
    sorry 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.....)
    I know what you mean there. I never took the course at my high school (heh - I dropped out to get my GED before I was old enough to take it), but everything I heard about it made me want to cry. Those things generallly do much more harm than good. Anyway, a much easier way to write that program would be do make an array of strings, and have the element number (plus 1, of course) correspond to the month abbreviation. Then you could loop through the array to find either the index or value, and output the other. Do you see what I mean?

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    What's happening is cin.get() is reading the newline from the last cin. You can verify this by adding cout's:
    Code:
    void Month::input_month_alpha(istream& cin)
    {
    	char first, second, third;
    	cin.get(first);
    	cin.get(second);
    	cin.get(third);
    	cout << "first:" << first << endl;
    	cout << "second:" << second << endl;
    	cout << "third:" << third << endl;
    One way to correct this is add an ignore() before the get()'s.
    Code:
    void Month::input_month_alpha(istream& cin)
    {
    	char first, second, third;
    	cin.ignore();
    	cin.get(first);
    	cin.get(second);
    	cin.get(third);
    	cout << "first:" << first << endl;
    	cout << "second:" << second << endl;
    	cout << "third:" << thrid << endl;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  2. global and static variable in a class delivered in a DLL
    By George2 in forum C++ Programming
    Replies: 16
    Last Post: 04-13-2008, 08:19 AM
  3. Replies: 9
    Last Post: 03-27-2008, 10:25 PM
  4. variable being reset
    By FoodDude in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2005, 12:30 PM
  5. webBrowser problem
    By algi in forum C# Programming
    Replies: 7
    Last Post: 08-19-2005, 09:35 AM