Monday, May 17, 2010

C++ array?

#include %26lt;string%26gt;


#include %26lt;iostream%26gt;





using std::cout;


using std::cin;


using std::endl;


using std ::string;





int main ()


{





string numberMonth[12]= {"Janurary","Feburary","March","April","...


string flowerOfmonth [12]={" "};


flowerOfmonth[0]="Carnation";


flowerOfmonth[1]="Violet";


flowerOfmonth[2]="Daffodile";


flowerOfmonth[3]="Daisy";


flowerOfmonth[4]="Lily-of-the-Valley";


flowerOfmonth[5]="Rose";


flowerOfmonth[6]="Water Lily";


flowerOfmonth[7]="Gladiolus";


flowerOfmonth[8]="Aster";


flowerOfmonth[9]="Cosmos";


flowerOfmonth[10]="Chrysanthemum";


flowerOfmonth[11]="Narcissus";





//get number of month


cout %26lt;%26lt; "Enter month (1-12) :" %26lt;%26lt; endl;


cin %26gt;%26gt;numberMonth;


if (month %26gt;= 1 %26amp;%26amp; month %26lt;= 12)


cout %26lt;%26lt; flowerOfmonth[numberMonth - 1] %26lt;%26lt; endl;


//else


cout %26lt;%26lt; "Invalid month" %26lt;%26lt; endl;


//end if


//end display salary





return 0;


} //end of main function








why cant i get this to work..thanks

C++ array?
You are expecting users to enter NUMERIC representation of the months. The wording of the months is NEVER used. So, numberMonths array definition is not needed.





Define numberMonths as an INTEGER.





Look what exactly you are doing with the numberMonth variable! It's an index for an array. Yet, you defined it as string.
Reply:try to chaneg this line


string flowerOfmonth [12]={" "};


to


string flowerOfmonth [12];


OR


initialize the second array as you have done with the first one


string flowerOfmonth = {"Carnation", "Violet", "Daffodile", ... }


No comments:

Post a Comment