Hi, I've this function in a class to update the total value.but when i try to remove the these row highlight in Bold it crash, what should i do????
void display_total_v alue()
{
double value;
double totalvalue=0;
int r, c;
setcolor (11);
for( r=0; r<row; r++ )
{
for( c=0; c<col; c++ )
{
if(warehouseshe lfTVs[r][c] == 0) // ** check if item on shelf
cout <<"\t"<<warehou seshelf[r][c] << " " << "none\t";
else
value=warehouse shelf[r][c]*warehouseshelf TVs[r][c]->cost;
totalvalue=valu e + warehouseshelf[r][c];
}
cout << " "<< endl;
}
cout <<"\t"<< "Total value is = $" << totalvalue << endl;
system("PAUSE") ;
}
below is the whole code
Thanks
void display_total_v alue()
{
double value;
double totalvalue=0;
int r, c;
setcolor (11);
for( r=0; r<row; r++ )
{
for( c=0; c<col; c++ )
{
if(warehouseshe lfTVs[r][c] == 0) // ** check if item on shelf
cout <<"\t"<<warehou seshelf[r][c] << " " << "none\t";
else
value=warehouse shelf[r][c]*warehouseshelf TVs[r][c]->cost;
totalvalue=valu e + warehouseshelf[r][c];
}
cout << " "<< endl;
}
cout <<"\t"<< "Total value is = $" << totalvalue << endl;
system("PAUSE") ;
}
below is the whole code
Code:
#include <iostream.h> #include <iomanip.h> #include <string.h> #include <process.h> #include <cstdlib> #include <windows.h> #define row 4 #define col 5 //The function that you'll use to //set the colour void setcolor(unsigned short color) { HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(hcon,color); } class TV { public : // ** made public for now int itemno; char description[100]; char model[100]; double cost; int quantity; // ** added // this function will prompt user to key in details void update() { setcolor (11); cout << " "<< endl; cout <<"\t"<< "Enter item no: ";cin >>itemno; cout <<"\t"<< "Enter description: "; cin.ignore(); cin.getline( description, 100 ); cout <<"\t"<<"Enter model: "; cin.getline( model, 100 ); cout <<"\t"<< "Enter cost: $";cin >> cost; quantity=0; // ** initialise } void display_item() { setcolor (11); cout << " "<< endl; cout <<"\t"<< "Item no: " << itemno << endl; cout <<"\t"<< "Description of item: " << description << endl; cout <<"\t"<< "Model: "<<model<<endl; cout <<"\t"<< "Cost of item: $ " << cost << endl; cout << " "<< endl; system("PAUSE"); } }; class shelf { int warehouseshelf[row][col]; // ** initialise to 0 here TV *warehouseshelfTVs[row][col];// ** added TVs double itemcost; int itemno; int quantity; double cost; public: void initial() { int r, c; for( r=0; r<row; r++ ) for( c=0; c<col;c++ ) { warehouseshelf[r][c] = 0; warehouseshelfTVs[r][c] = (TV *) 0; } } // this function will update the shelf quantity void update_shelfs(TV *brand) { int i, numitems, r , c, quantity; setcolor (11); cout << " "<< endl; { cout <<"\t"<< "Enter which row to update for item: "; cin >> r; cout <<"\t"<< "Enter which column to update for item: "; cin >> c; cout <<"\t"<< "Enter no of quantity to update for item: "; cin >> quantity; warehouseshelfTVs[r][c]=brand; // ** added TV to shelf warehouseshelf[r][c] = quantity; } setcolor (12); cout <<"\t"<< "Update completed................." << endl; setcolor (11); system("PAUSE"); } void display_shelfs() { setcolor (11); int r, c; for( r=0; r<row; r++ ) { for( c=0; c<col; c++ ) { if(warehouseshelfTVs[r][c] == 0) // ** check if item on shelf cout <<"\t"<<warehouseshelf[r][c] << " " << "none\t"; else cout <<"\t"<<warehouseshelf[r][c] << " " << warehouseshelfTVs[r][c]->itemno<<"\t"; } cout << " "<< endl; } system("PAUSE"); } void display_total_all_items_stored() { setcolor (11); int sum = 0; int r, c; for( r=0; r<row; r++ ) { for( c=0; c<col; c ++ ) { sum = sum + warehouseshelf[r][c]; } } cout <<"\t"<< "Total sum of items is = " << sum << endl; system("PAUSE"); } void display_total_value() { double value; double totalvalue=0; int r, c; setcolor (11); for( r=0; r<row; r++ ) { for( c=0; c<col; c++ ) { if(warehouseshelfTVs[r][c] == 0) // ** check if item on shelf cout <<"\t"<<warehouseshelf[r][c] << " " << "none\t"; else value=warehouseshelf[r][c]*warehouseshelfTVs[r][c]->cost; totalvalue=value + warehouseshelf[r][c]; } cout << " "<< endl; } cout <<"\t"<< "Total value is = $" << totalvalue << endl; system("PAUSE"); } }; class warehouse { public: // function to update address char tempaddress[200]; char *update_address (void) { setcolor (11); cout <<"\t"<< "Enter address: "; cin.getline (tempaddress, 200); cout << " "<< endl; cout <<"\t"<<"Address for the warehouse is "<<tempaddress <<endl; cout << " "<< endl; int size = strlen (tempaddress); //** change the string lenght char *test = new char[size]; //strcpy (test, tempaddress); //cout <<"\t"<<strlen (tempaddress); return test; delete test; } void display_Address (void) { setcolor (11); {cout <<"\t"<<"Update address is "<<tempaddress<<endl; cout << " "<< endl;} setcolor (14); } }; //function to show display menu void display_menu() { cout << " "<< endl; cout <<"\t"<< "Select option " << endl; cout <<"\t"<< "-----------------------" << endl; setcolor (11); cout <<"\t"<< "1) Update item details " << endl; cout <<"\t"<< "2) Display item details " << endl; cout <<"\t"<< "3) Update warehouse address" << endl; cout <<"\t"<< "4) Display Updated warehouse address" << endl; cout <<"\t"<< "5) Add items to the warehouse shelf" << endl; cout <<"\t"<< "6) Display warehouse detail" << endl; cout <<"\t"<< "7) Find total number of items in warehouse" << endl; cout <<"\t"<< "8) Find total value in warehouse" << endl; cout <<"\t"<< "9) Exit "<<endl; setcolor (14); cout <<"\t"<< "-----------------------" << endl; cout <<"\t"<< "Enter option: "; } void select_choice() { cout << " "<< endl; cout <<"\t"<<"Select TV "<<endl; cout <<"\t"<< "-----------------------" << endl; setcolor (11); cout <<"\t"<<"1 for TV1"<<endl; cout <<"\t"<<"2 for TV2"<<endl; cout <<"\t"<<"3 for TV3"<<endl; setcolor (14); cout <<"\t"<< "-----------------------" << endl; cout <<"\t"<< "Enter Brand using 1-3: "; } int main(void) { int i; int choice; char *address; TV brand[3]; //** tv class 3 object shelf s1; s1.initial(); warehouse w1; do { setcolor (14); system("cls"); display_menu(); cin >> choice; cin.ignore(); switch( choice ) { case 1: system("cls"); { cout << " "<< endl; cout <<"\t"<<"Update item details" << endl; cout <<"\t"<<"-----------------------"<<endl; select_choice(); do { cin >>i; if (i==0||i>3) { cout <<" "<<endl; setcolor (12); cout <<"\t"<<"Wrong choice. Pls enter 1-3: "; } else { brand[i-1].update(); } } while (i==0||i>3); } break; case 2: system("cls"); { cout << " "<< endl; cout <<"\t"<<"Display item details" << endl; cout <<"\t"<<"-----------------------"<<endl; select_choice(); do { cin >>i; if (i==0||i>3) { cout <<" "<<endl; setcolor (12); cout <<"\t"<<"Wrong choice. Pls enter 1-3: "; } else { brand[i-1].display_item(); } } while (i==0||i>3); } break; case 3: system("cls"); setcolor (14); cout << " "<< endl; cout <<"\t"<<"Update address" << endl; cout <<"\t"<<"-----------------------"<<endl; cout <<" "<<endl; address = w1.update_address(); system("PAUSE"); break; case 4: system("cls"); cout << " "<< endl; cout <<"\t"<<"Display Updated warehouse address" << endl; cout <<"\t"<<"-----------------------"<<endl; cout <<" "<<endl; w1.display_Address (); system("PAUSE"); break; case 5: system("cls"); { cout << " "<< endl; cout <<"\t"<<"Update Warehouse Shelfs" << endl; cout <<"\t"<<"-----------------------"<<endl; select_choice(); do { cin >>i; if (i==0||i>3) { cout <<" "<<endl; setcolor (12); cout <<"\t"<<"Wrong choice. Pls enter 1-3: "; } else { cout << " "<< endl; s1.update_shelfs(&brand[i-1]); // ** pass in TV item } } while (i==0||i>3); } break; case 6: system("cls"); { cout << " "<< endl; cout <<"\t"<<"Display Warehouse Selfs" << endl; cout <<"\t"<<"-----------------------"<<endl; { cout << " "<< endl; s1.display_shelfs(); } } break; case 7: system("cls"); { cout << " "<< endl; cout <<"\t"<<"Display Total Items" << endl; cout <<"\t"<<"-----------------------"<<endl; { cout<<" "<<endl; s1.display_total_all_items_stored(); } } break; case 8: system("cls"); { cout << " "<< endl; cout <<"\t"<<"Display Total Value" << endl; cout <<"\t"<<"-----------------------"<<endl; { cout<<" "<<endl; s1.display_total_value(); } } break; case 9: cout <<" "<<endl; setcolor (12); cout <<"\t"<< "Bye Bye. See you again"<<endl; cout<<" "<<endl; break; default: cout<<" "<<endl; setcolor (12); cout <<"\t"<<"Invalid choice.Pls re-enter again"<<endl; system("PAUSE"); } } while( choice != 9 ); return 0; }
Comment