Thursday, July 9, 2009

Please help with C++ Array?

For some reason what ever value (1-12) I enter for the month I have a return of “Jan” It builds fine … with no errors…but my array subscripts are not matching





#include %26lt;iostream%26gt;


#include %26lt;string%26gt;


using std::cout;


using std::cin;


using std::endl;


using std::string;











int main()





{


int days[12] = {31, 28, 31, 30, 31, 30 , 31, 31 , 30, 31, 30, 31};


string month [12]= {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};


string searchForMonth = "";





cout %26lt;%26lt; "Enter Month ID: ";


getline(cin, searchForMonth);





while (searchForMonth != "12")


{


int y = 0; //keeps track of array subscripts


while (y %26lt; 12 %26amp;%26amp; month[y] %26lt; searchForMonth)


y = y + 1;





if (y %26lt;= 12)





cout %26lt;%26lt; "Month: " %26lt;%26lt; month[y] %26lt;%26lt; endl;


cout %26lt;%26lt; "Enter Month ID: ";


getline (cin, searchForMonth);





}











return 0;


} //end of main function

Please help with C++ Array?
The reason you're getting "Jan" all the time is due to the second while loop condition. The string month[0] ("Jan") will always be greater than searchForMonth (assuming you're entering a number for searchForMonth).





So the second loop condition will always be false, and y will stay at 0.
Reply:I see a couple of problems.





string searchForMonth = "";





cout %26lt;%26lt; "Enter Month ID: ";


They are entering a STRING HERE


getline(cin, searchForMonth);





I understand "12" but why not use a compare method?


Its a string, not the ascii value of 12.


what happens if they enter a -1??


I think this is your reason right here.





while (searchForMonth != "12")


{


int y = 0; //keeps track of array subscripts








while (y %26lt; 12 %26amp;%26amp; month[y] %26lt; searchForMonth)


y = y + 1;





The IF below is not part of WHile


if (y %26lt;= 12)


cout %26lt;%26lt; "Month: " %26lt;%26lt; month[y] %26lt;%26lt; endl;





The statement below are not part of above IF


cout %26lt;%26lt; "Enter Month ID: ";


getline (cin, searchForMonth);





}//beginning while ends





I didnt get a chance to run it, so I just did this by sight
Reply:the man above me took the words right out of my mouth !
Reply:It could be these lines:





if (y %26lt;= 12)





cout %26lt;%26lt; "Month: " %26lt;%26lt; month[y] %26lt;%26lt; endl;





In your condition, you are accepting 12 as a valid value. So what is happening is you are getting





cout %26lt;%26lt; "Month: " %26lt;%26lt; month[12] %26lt;%26lt; endl; That's an overflow problem because the array subscript should only be between 0 and 11.





***********


You could try this in your loop:





int monthSearch;





cin%26gt;%26gt;monthSearch;





int index=monthSearch-1;


cout%26lt;%26lt;month[index] %26lt;%26lt; " " %26lt;%26lt; days[index]%26lt;%26lt;endl;





output of 3 entered into program:





Mar 31

columbine

No comments:

Post a Comment