CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Hybrid View

  1. #1
    Join Date
    Oct 2012
    Posts
    1

    Function not executing cin-statement

    I have this problem for an assignment, and one of the functions that I created for it is nothing fancy; just a function that'll take in a number and error-check to make sure the input is valid. However,
    after doing it, when the program calls that function, it skips the cin statement and ends the program, without letting the user input his number. The function in question is called getSubLength.

    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    #include <cstdlib>
    
    using namespace std;
    
    const string JANUARY = "January";
    const string FEBRUARY = "February";
    const string MARCH = "March";
    const string APRIL = "April";
    const string MAY = "May";
    const string JUNE = "June";
    const string JULY = "July";
    const string AUGUST = "August";
    const string SEPTEMBER = "September";
    const string OCTOBER = "October";
    const string NOVEMBER = "November";
    const string DECEMBER = "December";
    
    void getMonth(char first, char second, char third, int& monthNumber);
    void convertMonthNumber(int monthNumber, string& month);
    void getYear(int& yearNumber);
    void currentDate(string& month, int& yearNumber);      
    void getName(string& firstName, string& lastName);            
    void getSubLength(int& subscription); 
             
    int main()
    {
        char first, second, third;
        int monthNumber = 0;
        int yearNumber = 0;
        int subscription;
        string month, firstName, lastName;      
        
        cout << "Subscription Evaluation Program";
        cout << endl << endl;
        
        getMonth(first, second, third, monthNumber);
        getYear(yearNumber);
        convertMonthNumber(monthNumber, month);
        currentDate(month, yearNumber);
        getName(firstName, lastName);
        getSubLength(subscription);                     
        
        system("PAUSE");
        return 0;
    }
    
    void getMonth(char first, char second, char third, int& monthNumber)
    {   
        cout << "Enter first letter of the current month: ";
        cin >> first;
               
        switch(first)
        {
            case 'F':
    	    case 'f':
    		    {
                     monthNumber = 2;
                }   
                break;                   
    	    case 'S':
    	    case 's':
                 {
                     monthNumber = 9;
                 }
    		    break;
    	    case 'O':
    	    case 'o':
                {
                     monthNumber = 10;
                }
    		    break;
    	    case 'N':
    	    case 'n':
    		    {
                     monthNumber = 11;
                }     
    		    break;
    	    case 'D':
    	    case 'd':
    		    {
                     monthNumber = 12;
                }
    		    break;
    	    case 'A':
    	    case 'a':
    	    {
    		    cout << "Enter second character of month: ";
    		    cin >> second;
    		    
    		    switch(second)
    		    {
    			    case 'P':
    			    case 'p':
    				    {
                             monthNumber = 4;
                        }
    				    break;
    		 	    case 'U':
    			    case 'u':
    				    {
                             monthNumber = 8;
                        }
    				    break;
    			    default:
    				    cout << "Unknown Month";                         
    				    cout << endl;
                  }
            }
    	break;
    	case 'J':
    	case 'j':
    	{
    		cout << "Enter second character of month : ";
    		cin >> second;
    		switch(second)
    		{
    			case 'A':
    			case 'a':
    				{
                         monthNumber = 1;
                    }
    				break;
    			case 'U':
    			case 'u':
    				cout<<"\nEnter third character: ";
    				cin >> third;
    				switch(third)
    				{
    					case 'L':
    					case 'l':
    						{
                                 monthNumber = 7;
                            }    
    						break;
    					case 'N':
    					case 'n':
    						{
                                 monthNumber = 6;
                            }
    						break;
    					default:
    						cout << "\nUnknown Month";
    				}
    				break;
    			default:
    				cout << "\nUnknown Month";
    				cout << endl;                         
    		}
    		break;
    		case 'M':
    		case 'm':
    			cout << "Enter second and third characters: ";
    			cin >> second;
    			cin >> third;
    			switch(second)
    			{
    				case 'A':
    				case 'a':
    					{
    						switch(third)
    						{
    							case 'R':
    							case 'r':
    								{
                                         monthNumber = 3;
                                    }     
    								break;
    							case 'Y':
    							case 'y':
    								{
                                         monthNumber = 5;
                                    }    
    								break;
    							default:
    								cout << endl << "Unknown Month";
    								cout << endl;
    						}
    					}
    					break;
    				default:
    					cout << endl << "Unknown Month";
    					cout << endl;
    			}
    		break;
    		default:
    			cout << endl << "Unknown Month";
    			cout << endl;
    			return;
            }                                                       
        }           
    }
    
    void convertMonthNumber(int monthNumber, string& month)     
    {  
        if (monthNumber == 1)
           month = JANUARY;
        else if (monthNumber == 2)
             month = FEBRUARY;
        else if (monthNumber == 3)
             month = MARCH;
        else if (monthNumber == 4)
             month = APRIL;
        else if (monthNumber == 5)
             month = MAY;
        else if (monthNumber == 6)
             month = JUNE;
        else if (monthNumber == 7)
             month = JULY;
        else if (monthNumber == 8)
             month = AUGUST;
        else if (monthNumber == 9)
             month = SEPTEMBER;
        else if (monthNumber == 10)
            month = OCTOBER;
        else if (monthNumber == 11)
            month = NOVEMBER;
        else if (monthNumber == 12)
            month = DECEMBER;      
        return;
    }
    
    void getYear(int& yearNumber)
    {
        const int LOW_YEAR_LIMIT = 2012;
        const int HIGH_YEAR_LIMIT = 2017;
        
        do{
        cout << "\nEnter current year (4 digits): ";
        cin >> yearNumber;
        if (yearNumber < LOW_YEAR_LIMIT || yearNumber >= HIGH_YEAR_LIMIT){
            cout << endl;
            cout << "Invalid year. Please enter again.";
            cout << endl;
            }
        }while (yearNumber < LOW_YEAR_LIMIT || yearNumber >= HIGH_YEAR_LIMIT);
        return;
    }
    
    void currentDate(string& month, int& yearNumber) 
    {    
         cout << "Current Date: " << month << " " << yearNumber << endl << endl;                               
         
         return;
    }
    
    void getName(string& firstName, string& lastName)
    {
         cout << "Enter subscriber first name: ";
         getline (cin, firstName);
         cin.ignore();
         
         cout << "Enter subscriber last name: ";
         getline (cin, lastName);  
         cin.ignore();
          
         return;
    }
    
    void getSubLength(int& subscription)
    {     
          cout << "\nEnter number of years subscribed: ";
          cin >> subscription;
          
          return;
    }

  2. #2
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Function not executing cin-statement

    warning C4700: uninitialized local variable 'third' used
    warning C4700: uninitialized local variable 'second' used
    warning C4700: uninitialized local variable 'first' used
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  3. #3
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Function not executing cin-statement

    when the program calls that function, it skips the cin statement and ends the program
    Not here it doesn't.

    BTW the function 'getName' doesn't.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured