I've got a program that's supposed to take a description, serial number and price for an item. Then sort by highest price, and display the new order.
Everything works(thanks to several dozen searches through the board/source codes here) except for displaying the description string at the end. I'm thinking(and I may be totally off base here) that it's something to do with buffers, but I just can't figure it out.
Any help would be greatly appreciated. THANKS!
Code:#include <iostream> #include <string> #include <iomanip> #define SWAP(a,b) { double t; t=a; a=b; b=t; } // Macros for Bubble Sort swapping #define STRINGSWAP(c,d) {string t; t=c; c=d; d=t;} #define INDEX 8 using namespace std; const int MAX_NUM_PURCHASES = 50; void get_all_purchases(double price[], double serialNum[], string descript[], int numPurchases); void bubble_srt(double price[], double serialNum[], string descript[], int numPurchases); void print_top_purchases(double price[], double serialNum[], string descript[], int numPurchases); int main() { double price[MAX_NUM_PURCHASES]; double serialNum[MAX_NUM_PURCHASES]; string descript[MAX_NUM_PURCHASES]; int numPurchases; do { cout << "How many purchases do you have? "; cin >> numPurchases; if (numPurchases <= 0 || numPurchases > MAX_NUM_PURCHASES) cout << "Must be between 1 and "<< MAX_NUM_PURCHASES << "\n"; }while (numPurchases > MAX_NUM_PURCHASES || numPurchases <= 0); get_all_purchases(price, serialNum, descript, numPurchases); system("pause"); return (0); } void get_all_purchases(double price[], double serialNum[], string descript[], int numPurchases) { for (int i=0; i < numPurchases; i++) { cout << "\n\nInput the description: "; getline(cin, descript[i]); std::cin.clear(); std::cin.sync(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); cout << "Input the serial number: "; cin >> serialNum[i]; do { cout << "Input the price: "; cin >> price[i]; if(price <= 0) cout << "Must be greater than zero\n"; }while(price[i] <=0); }//! End of for loop over items-purchased input. bubble_srt(price, serialNum, descript, numPurchases); return; } /* Bubble Sort source code found at cprogramming.com http://www.cprogramming.com/source/bubblesort.c */ void bubble_srt( double price[], double serialNum[], string descript[], int numPurchases ) { int i, j; for(i = 0; i < numPurchases; i++) // Make a pass through the array for each element { for(j = 1; j < (numPurchases-i); j++) // Go through the array beginning to end { if(price[j-1] < price[j]) // If the the first number is greater, swap it { SWAP(price[j-1],price[j]); SWAP(serialNum[j-1],serialNum[j]); STRINGSWAP(descript[j-1],descript[j]); } } }//! End of for loop passing through each element print_top_purchases(price, serialNum, descript, numPurchases); } void print_top_purchases(double price[], double serialNum[], string descript[], int numPurchases) { int numPrint; if(numPurchases < 5) numPrint = numPurchases; else numPrint = 5; for(int i = 0; i < numPrint; i++) { cout << (i+1) << ". " << setw(6) << price[i] << " " << setw(6) << serialNum[i] << " " << setw(15) << descript[i] << endl; /*this is where it doesn't work, searched for hours, can't figure it out.*/ } }



LinkBack URL
About LinkBacks



CornedBee
. 