Thread: Cleaning up array code

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    3

    Cleaning up array code

    Hello all. I need a little help on the following code. I do not like the if statements I have but I can not think of a way to get rid of them...anyway to re-write it?

    Code:
    //displays number of days in corresponding month.
    
    #include <iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main()
    {
    	//declaires variables and array
    	int searchFor = 0; //number to search for
    	int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    
    	//get number to search for
    	cout << "Enter month to display number of days." << endl;
    	cout << "i.e. 1 = January." << endl;
    	cin >> searchFor;
    	if (searchFor > 12 || searchFor < 1)
    		cout << "Error. You entered an invalid number." << endl;
    
    	//searches for match
    	for(int x = 1; x <= 12; x = x + 1)
    		if (x == searchFor)
    			cout << "Month " << searchFor << " has " << days[x-1] << " days."<< endl;
    
    			//end if
    	//end for
    	return 0;
    }//end of function

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    my lazy attempt

    Code:
    //displays number of days in corresponding month.
    
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	//declaires variables and array
    	int searchFor = 0; //number to search for
    	int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    
    	//get number to search for
    	cout << "Enter month to display number of days." << endl;
    	cout << "i.e. 1 = January." << endl;
    	cin >> searchFor;
    	switch(searchFor)
    	{
    		case 1: cout << "Month " << searchFor << " has " << days[searchFor-1] << " days" << endl;
    			break;
    		case 2: cout << "Month " << searchFor << " has " << days[searchFor-1] << " days" << endl;
    			break;
    		case 3: cout << "Month " << searchFor << " has " << days[searchFor-1] << " days" << endl;
    			break;
    		case 4: cout << "Month " << searchFor << " has " << days[searchFor-1] << " days" << endl;
    			break;
    		case 5: cout << "Month " << searchFor << " has " << days[searchFor-1] << " days" << endl;
    			break;
    		case 6: cout << "Month " << searchFor << " has " << days[searchFor-1] << " days" << endl;
    			break;
    		case 7: cout << "Month " << searchFor << " has " << days[searchFor-1] << " days" << endl;
    			break;
    		case 8: cout << "Month " << searchFor << " has " << days[searchFor-1] << " days" << endl;
    			break;
    		case 9: cout << "Month " << searchFor << " has " << days[searchFor-1] << " days" << endl;
    			break;
    		case 10: cout << "Month " << searchFor << " has " << days[searchFor-1] << " days" << endl;
    			break;
    		case 11: cout << "Month " << searchFor << " has " << days[searchFor-1] << " days" << endl;
    			break;
    		case 12: cout << "Month " << searchFor << " has " << days[searchFor-1] << " days" << endl;
    			break;
    		default: cerr<<"Invalid number"<<endl; return 0;
    	}
    	return 0;
    }//end of function

  3. #3
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Your first if statement was a very good idea! But, you have no else with it, so after it outputted the error it would just try and continue with the program.
    But if statements are great things! Perhaps if you work on your syntax some more (more consistent and neater) you would not dislike them so.

    The seconds if statement and for loop is completely useless.



    Here it is a little better:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    
    	int days_per_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    	int user_selection;
    
    	cout << "Enter month to display number, Jan = 1, July = 7, ect :";
    	cin >> user_selection;
    
    	if (user_selection > 12 || user_selection < 1) //This is a great if statement you have!
    	{
    	
    		cout << "Error: Invalid selection.";
                    return 1;
    		
    	}											
    
    
    	else
    
    	{
    		cout << "Month " << user_selection << " has " << days_per_month[user_selection-1] << " days." << endl;
    
    	}
    
    
    	return 0;
    
    }
    
    }
    Last edited by Enahs; 11-16-2005 at 07:13 PM.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    3
    Quote Originally Posted by Enahs
    Your first if statement was a very good idea! But, you have no else with it, so after it outputted the error it would just try and continue with the program.
    But if statements are great things! Perhaps if you work on your syntax some more (more consistent and neater) you would not dislike them so.

    The seconds if statement and for loop is completely useless.



    Here it is a little better:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    
    	int days_per_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    	int user_selection;
    
    	cout << "Enter month to display number, Jan = 1, July = 7, ect :";
    	cin >> user_selection;
    
    	if (user_selection > 12 || user_selection < 1) //This is a great if statement you have!
    	{
    	
    		cout << "Error: Invalid selection.";
                    return 1;
    		
    	}											
    
    
    	else
    
    	{
    		cout << "Month " << user_selection << " has " << days_per_month[user_selection-1] << " days." << endl;
    
    	}
    
    
    	return 0;
    
    }
    
    }

    That's more like it! It was so simple and right in front of me. I was so set on the for(). Thanks.

    BTW what is using namespace std; used for?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It has the same purpose as using std::cout and the other statements like it you had. It is just more general- it allows the use of any name in the std namespace, not just the specific ones mentioned. Your version is fine (and sort of preferred), so you don't need to change it.

  6. #6
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    BTW what is using namespace std; used for?
    Instead of this like you have:
    Code:
    using std::cout;
    using std::cin;
    using std::endl;

    When your programs are very simple and you will only be using the standard definitions of functions; then instead of setting every individual function you use you can just set everything to use the standard definitions. The different definitions reside in different “namespaces” (do not worry, you will get to it). So by calling it like that is just simpler.

    But when your programs get more complex, you will not always want to use the standard name space, so specifying each thing you use to a specific name space is good to know.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trimming the fat out of my code for an Array
    By katipo in forum C Programming
    Replies: 17
    Last Post: 12-09-2008, 01:47 PM
  2. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  3. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 07:14 PM
  4. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  5. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM