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



LinkBack URL
About LinkBacks


