Tuesday, July 14, 2009

Anyone, kind enough to help with a C++ parallel array?

I need to display the price and quantity of the item whose ID is entered by the user. Below is what I have but it only displays some number I have no idea where it is coming from. -858993460 for both prices quantities





#include %26lt;iostream%26gt;


#include %26lt;string%26gt;





using std::string;


using std::cout;


using std::cin;


using std::endl;





int main()


{


//cin %26gt;%26gt;


//declare arrays


string id[5] = {"10", "14", "34", "45", "78"};


int prices[5] = {125, 600, 250, 350, 225};


int quantities[5] = {5, 3, 9, 10, 2};


string searchForID = "";





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


getline(cin, searchForID);





while (searchForID != "5")


{


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


while (y %26lt; 5 %26amp;%26amp; id[y] != searchForID)


y = y + 1;





if (y %26lt;= 5)


cout %26lt;%26lt; "Prices: $ " %26lt;%26lt; prices[y] %26lt;%26lt; endl;


cout %26lt;%26lt; "Quanty: $ " %26lt;%26lt; quantities[y] %26lt;%26lt; endl;








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


getline (cin, searchForID);





}





return 0;


} //end of main function

Anyone, kind enough to help with a C++ parallel array?
#include %26lt;iostream%26gt;


#include %26lt;string%26gt;





using std::string;


using std::cout;


using std::cin;


using std::endl;





int main()


{


//cin %26gt;%26gt;


//declare arrays


string id[5] = {"10", "14", "34", "45", "78"};


int prices[5] = {125, 600, 250, 350, 225};


int quantities[5] = {5, 3, 9, 10, 2};


string searchForID = "";





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


getline(cin, searchForID);





while (searchForID != "5")


{


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


while (y %26lt; 5 %26amp;%26amp; id[y] != searchForID)


%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;


wats this loop for


%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;


y = y + 1;


.........................................


after this while loop terminates y=5


so


.........................................





if (y %26lt;= 5)


cout %26lt;%26lt; "Prices: $ " %26lt;%26lt; prices[y] %26lt;%26lt; endl;


cout %26lt;%26lt; "Quanty: $ " %26lt;%26lt; quantities[y] %26lt;%26lt; endl;


.........................................


price[5] and qualities [5] are displayed which are garbage values


.........................................





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


getline (cin, searchForID);


.........................................


and wats this for


%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;


}





return 0;


} //end of main function

gladiolus

No comments:

Post a Comment