store mamagement system with fstream and iostream

0 ragmar thomas · July 12, 2014
<?prettify lang=html linenums=true?>
#include #include<fstream>int addITEM(int quantity);int addNAME(int numberOFname);int searchITEM(int searchITEMs);void quitMENU(int quit);using namespace std;int main(){     int add;     int numberOFname, quit, quantity, searchITEMs;     do{    cout << endl << endl;menu:    cout << "press 1 for display added items item\n";    cout << "press 2 for adding new item\n";    cout << "press 3 for searching added item\n";    cout << "press 4 for exit program\n" << endl;    cout << "please enter numbers from the menu: ";    cin >> add;cin.ignore();    if(add==1)        addITEM(quantity);    if(add==2){        addNAME(numberOFname);}    if(add==3)        searchITEM(searchITEMs);break;    if(add==4)        quitMENU(quit);    if(add!=1&&2&&3){        cout << "please enter only from the menu" << endl;goto menu;}        }while(add!=4);}//the adding functionint addITEM(int quantity){cout << "enter the total amount of items you want to ADD\n";cin >> quantity;int quantID[quantity], i, j, k, l, m;int quantAMOUNT[quantity];string quantNAME[quantity];double quantPRICE[quantity], total;for(i=0; i<quantity; i++){cout << "enter the id of the item: ";cin >> quantID;cout << "enter the the name of the item: ";cin >> quantNAME;cout << "enter the the amount of the item: ";cin >> quantAMOUNT;cout << "enter the price of the item: ";cin >> quantPRICE;cout << "----------------------------------\n";}cout << endl;cout << "ID" << "\t\t" << "NAME" << "\t\t" << "AMOUNT" << "\t\t" << "PRICE" << "\t\t" << "SUB TOTAL" << endl;   for(m=0; m<quantity; m++){        for(i=m; i<=m; i++)            cout << quantID << "\t\t";        for(j=m; j<=m; j++)            cout << quantNAME[j] << "\t\t";        for(k=m; k<=m; k++)            cout << quantAMOUNT[k] << "\t\t";        for(l=m; l<=m; l++)            cout << quantPRICE[l] << "\t\t";        for(k=0; k<=m; k++){            for(l=0; l<=m; l++)                total = quantAMOUNT[k] * quantPRICE[l];            }cout << total;        cout << endl;}        cout << "-------------------------------------------------------------------------" << endl;        cout << "TOTAL" << "\t\t\t\t\t\t\t\t" << total+total << endl;return quantity;}//for adding name for items in the listint addNAME(int numberOFname){        int addId, quantity, shelfNumber;    string addName;    double price;    char filename[15];    ofstream addme;    cout << "please enter your file name with TXT extension: ";    cin.getline(filename, 15);    addme.open(filename, ios_base::app);    cout << "after finishing input press ctrl+z" << endl;    cout << "ID\t" << "NAME\t"  << "QUANTITY\t" << "PRICE\t" << "SHELF" << endl;    while(cin >> addId >> addName >> quantity >> price >> shelfNumber){        addme << addId  << "\t" << addName << "\t" << quantity << "\t" << price << "\t" << shelfNumber << endl;}    return numberOFname;}//for searching items from the listint searchITEM(int searchITEMs){    char filename[15];    cout << "please enter your file name to search for saved files: ";    cin.getline(filename, 15);    ifstream read(filename);int itemID, itemAMOUNT, itemSHELF;    string itemNAME;    double itemPRICE;while(read >> itemID >> itemNAME >> itemAMOUNT >> itemPRICE >> itemSHELF){    cout << itemID << ".\t" << itemNAME  << "\t" << itemAMOUNT << "\t" << itemPRICE << "\t" << itemSHELF << endl;}    return searchITEMs;}//for quiting the functionvoid quitMENU(int quit){quit=0;}

Post a Reply

Replies

Oldest  Newest  Rating
+1 Franz Schmidt · July 12, 2014
I think you mean the part in the main function should loop?!?

Your code:

do{    


	cout << endl << endl;


	menu: cout << "press 1 for display added items item\n";    


	cout << "press 2 for adding new item\n";    


	cout << "press 3 for searching added item\n";    


	cout << "press 4 for exit program\n" << endl;    


	cout << "please enter numbers from the menu: ";    


	cin >> add;


	cin.ignore();    


	if(add==1)        


		addITEM(quantity);    


	if(add==2){        


		addNAME(numberOFname);


	}    


	if(add==3)        


		searchITEM(searchITEMs);


	break;    


	if(add==4)        


		quitMENU(quit);    


	if(add!=1&&2&&3){        


		cout << "please enter only from the menu" << endl;


		goto menu;


	}        


}while(add!=4);

this loop will crash when you tipe 3 because you have a break in there.
And also if you tipe 1 or 2 the loop will crash because you have no brackets for the if-statement 
(tipp: no brackets like the if-statement add==1 need no brackets, I though use them so these misstakes can't occur)
And you should use "else if" not just if statements.

Reccomendet code:

do{    


	cout << endl << endl;


	menu: cout << "press 1 for display added items item\n";    


	cout << "press 2 for adding new item\n";    


	cout << "press 3 for searching added item\n";    


	cout << "press 4 for exit program\n" << endl;    


	cout << "please enter numbers from the menu: ";    


	cin >> add;


	cin.ignore();    


	if (add == 1){


		addITEM(quantity);


	}


	else if (add == 2){


		addNAME(numberOFname);


	}    


	else if (add == 3){


		searchITEM(searchITEMs);


	}   


	else if (add == 4){


		quitMENU(quit);


	}


	if(add!=1&&2&&3){        


		cout << "please enter only from the menu" << endl;


		goto menu;


	}        


} while (add != 4);

0 ragmar thomas · July 13, 2014
i added the the break statement for the sake of system pause other wise it will run forever without stop in the console the menu section
and for the if it with the if else and the braces thanks i'll reach u if something went wrong thanks in advance.
0 ragmar thomas · July 13, 2014
and the problem sustains the second function call with out the break it keeps running in the IDE the break helps but it terminate  the program but it shouldn't
only the user should terminate the program and thanks for the third function call it is working with the braces
or is there any disagreement between fstream and function calls?
  • 1

C++

107,227 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator