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;
}

except that the calculation part gives me different figures

Please try to narrow the error down to a section of code. I can't compile the whole thing because it's been Borlandated.

hi.. yes, i use borland turbo.. there are 5 functions in the above code..
mainMenu() handles the main page
menuVal() handles the menu validation
setPro() handles writing data to a text file
display() reads the data out
profitRep() shows profit report of data in text file... but, i get different mathematical results whenever the function is called...

i want to be able to "find" and "delete" items in the text file.....
appreciate your response

There is a problem with line 112 if ((prodname[i] == '9')||(noOfItem == '9')||(priceOfItem[i] == '9') ||(cprice[i] == '9')){ in that noOfItem, priceOfItem, and cprice are integer arrays, and you're testing them for equality with '9' which is equal to 57 (check your ASCII table).

I wasn't able to trace through where you went wrong with the profitRep(), as your code is kind of all over the place, and honestly not well formatted.

In terms of finding and deleting the items from the file, the most efficient way with a small file is to read it in, and not write out the stuff you need to delete. Finding where something is in the file and removing it, while shifting everything else is not effective.

hi, thanks again.. i know i was supposed to comment my codes for better understanding... okay, what if I decide not to save my items in a text file but rather, save them in an array or string.. would that still work for me??

Well, when you read them in, they do need to be stored in memory somewhere. You could use an array of strings. If you don't know how many you have, you can either try to overestimate how many slots you need, or (and this depends on your progress in your course) try a container like a vector.

no idea on on how to use 'vectors'..
okay, i am going to try to use an array of strings... one thing though is that my borland compiler doesn't recognize the find() and erase() function whenever it's used even when i applied the <string.h> or <cstring.h> file..

no idea on on how to use 'vectors'.

Yeah, then don't worry about that.

my borland compiler doesn't recognize the find() and erase() function

Yes. I don't know that it supports std::string, that's why I was surprised to see "string" in your program. One possibility is to change everything into C-strings (char *, null terminated) so you can employ the functions from the C standard library (in <cstring>), or you could write your own find and erase functions. Again, there's no real need to erase anything, as you just ignore the ones you're erasing when you're writing the rest out.

the thing is, i am stuck with the current code and can't progress right now... the find() function i used was an example i got from the net and it didn't work.. i really need help.. i am not asking you to solve this for me, but just give me a guideline.. my course didn't even cover strings and i really do not know how to use the find and earse functions..... guide me through something i can write.. how do i write my own find and erase functions?
the point is, i am writing a program for a small store... the manager of staff should be able to delete items in the program or something related... thanks a lot for the attention....

You can't erase some data from the file. You must copy the updated data(text in this case) to a new file. You can get the data to the memory then close the file then open the same file with "write" attribute. This deletes all the data in the file. Then flush the updated data to the file. Then you are done.

Do you know where the header files are located on your system? (probably under an include subdirectory of where your compiler is). Open up and look into <cstring.h> and see how "string" is defined. If "string" in this context is just an alias for a c-string, you can use all of the c-string functions on it. Look into strstr() http://www.cplusplus.com/reference/clibrary/cstring/strstr/, which functions a bit like find.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.