i have this assignment to submit tomorrow(friday) and i am having difficulties updating my text files. actually, i have to be able to update the text files by searching and deleting items. please, check the code below.. so far, i am receiving no errors except that the calculation part gives me different figures whenever it is run.... i need help soon.
ps: i am trying to create a text file to store information of a store that sells computers.
#include <iostream.h>
#include <conio.h>
#include <cstring.h>
#include <stdio.h>
#include <fstream.h>
class products{
public:
string prodname;
string prodName[25];
int noOfItem;
int priceOfItem[25];
int cprice[25];
int proProfit, sumcprice, sumpriceOfItem;//variables to calculate profit
//string x, inp1;
//defines the main menu/ main page
int mainMenu() {
//sets width and height of texts...
gotoxy(24,3);
cout<<"\xDB\xDB\xDB\xDB\xB2 SAMMY COMPUTERS \xB2\xDB\xDB\xDB\xDB";
gotoxy(3,4);
cout<<"--------------------------------------------------------------------------";
gotoxy(35,5);
cout<<("MAIN MENU");
gotoxy(26,8);
cout<<" 1 - INFORMATION ABOUT PRODUCTS ";
gotoxy(26,10);
cout<<" 2 - ENTER PRODUCTS FOR SALE ";
gotoxy(26,12);
cout<<" 3 - VIEW INFO ON SALES PROFITS AND REPORTS";
gotoxy(26,14);
cout<<" 4 - HELP ";
gotoxy(26,15);
cout<<" 0 - EXIT ";
//validates items on main menu
return menuVal();
}
int menuVal() {//validates the mainMenu() function
int inp;
cout<< "\n\n";
cout<< "\nENTER YOUR CHOICE \n";
cin>> inp;
switch(inp) {
case 1:
display();
break;
case 2:
setPro();
break;
case 3:
profitRep();
break;
case 4:
cout<< "displays help tips and more";
break;
case 0:
exit(0);
break;
default: {
//clrscr();
cout<< "WRONG INPUT, TRY AGAIN, PRESS ANY KEY TO RETURN TO MENU";
clrscr();
getche();
mainMenu();
break;}
}
return 0;
}
int display() {
char ch;
clrscr();
ifstream compstore;
compstore.open("sammycomps1.txt");
if(!compstore)
{
cout<<"UNABLE TO OPEN FILE!!";
goto end;
}
while(compstore)
{
compstore.get(ch);
cout<<ch;
}
getch();
end:
compstore.close();
cout<< "PRESS ANY KEY TO RETURN TO MAIN MENU";
getche();
clrscr();
return mainMenu();
}
int setPro(){
//remove after first input of data... use ios::app
cout<< "TO QUIT THE INPUT PROCESS DURING LOOP, ENTER '9' AND HIT THE RETURN KEY.";
for (int i=0; i<4; i++){
cout<< "ENTER DESCRIPTION. MUST BE TEN(10) CHARACTERS LONG\n";
cin>> prodname;
prodName[i]=prodname;
cout<< "ENTER ID NUMBER \n";
cin>> noOfItem;
cout<< "PRICE \n";
cin>> priceOfItem[i];
cout<< "COST PRICE \n" ;
cin>> cprice[i];
if ((prodname[i] == '9')||(noOfItem == '9')||(priceOfItem[i] == '9') ||(cprice[i] == '9')){
return mainMenu();
}
else{
ofstream compstore("sammycomps1.txt", ios::app); //create a file for output..
//COMMENT NEXT TWO LINES TO APPEND NEW DATA.
compstore<< "\n\n";
//compstore<< " ---------------------------------------------------------------------" << endl<<endl ;
compstore<< prodName[i]<< " ";
compstore<< noOfItem<< " ";
compstore<< priceOfItem[i]<< " ";
compstore<< cprice[i] << endl;
compstore.close();
}
}
cout<<"ENTER ANY KEY TO RETURN TO MAIN MENU";
getche();
clrscr();
return mainMenu();
}
int profitRep() {
proProfit=0;
for (int x=0; x<2; x++){
cout<< priceOfItem[x] << " "<< cprice[x]<< endl;
sumpriceOfItem = ((sumpriceOfItem) + (priceOfItem[x]));
sumcprice = ((sumcprice) + (cprice[x])) ;
}
proProfit= ((sumpriceOfItem)-(sumcprice));
cout<< "Total profit is : $"<< proProfit << endl;
cout<< "PRESS ANY KEY TO RETURN TO MENU ";
getche();
clrscr();
return mainMenu();
}
};
int main() {
products inventory;
inventory.mainMenu() ;
//inventory.menuVal() ;
return 0;
}