0

Hello everyone! I have here a code with a random-access file that lets you delete and update any information in the file. Please run the file to see what I'm talking about.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

#include <cstdlib>

int choice;

void displaysmenu(void) {
    cout << "M E N U" << endl;
    cout << "[1] List" << endl;
    cout << "[2] Delete" << endl;
    cout << "[3] Update" << endl;
    cout << "[4] Add" << endl;
    cout << "[5] Quit" << endl;
}

void getchoice(void) {
    cout << "Your choice: " << endl;
    cin >> choice;
}

void outputLine (int record, char tool[30], int quantity, double cost) {
    cout << setiosflags (ios::left) << setw(10) << record
        << setw(20) << tool
        << setw(10) << quantity
        << resetiosflags (ios::left)
        << cost << '\n';
}

void listH () {
    ifstream inHardware ("hardware.txt", ios::in);
    if (!inHardware) {
        cerr << "File could not be opened\n";
        exit(1);
    }
    int record;
    char tool[30];
    int quantity;
    double cost;

    cout << setiosflags(ios::left) << setw(10) << "Record#"
        << setw(20) << "Tool name" << setw(10) << "Quantity" << setw(10) << "Cost\n"
        << setiosflags(ios::fixed | ios::showpoint);

    while (inHardware >> record >> tool >> quantity >> cost) {
        outputLine (record, tool, quantity, cost);
    }
}

void deleteH () {
    int deleteRecord;
    string line;
    cout << "Enter the record number you want to delete: " << endl;
    cin >> deleteRecord;

    ifstream in("hardware.txt");
    if( !in.is_open()) {
        cout << "File could not be opened."<< endl;
    }

    /*while( getline(in,line) ) {

	   if ( (line = line.find(deleteRecord, 0)) == 0 )) {
            out << line << "\n";
	   }
    }
        if ( (line = line.find(deleteRecord), 0)) != 0 ){
            cout<<"\n"<<deleteRecord<<" not found"<<endl;
        }*/
}

void updateH () {
    ofstream outHardware ("hardware.txt", ios::app);

    if (!outHardware) {
        cerr << "File could not be opened." << endl;
        exit(1);
    }

    int updateRecord;

    cout << "Enter the record number you want to update: " << endl;
    cin >> updateRecord;

}

void addH () {
    ofstream outHardware ("hardware.txt", ios::app);

    if (!outHardware) {
        cerr << "File could not be opened." << endl;
        exit(1);
    }

    int record;
    char tool[30];
    int quantity;
    double cost;

    cout << "Enter record number, tool name, quantity, and cost. Press enter per info." << endl;
    cin >> record >> tool >> quantity >> cost;
    outHardware << record << ' ' << tool << ' ' << quantity << ' ' << cost << '\n';
}

int main () {

    int record;
    char tool[30];
    int quantity;
    double cost;

    char temp;

    do {
        displaysmenu();
        getchoice();

        switch (choice) {
            case 1: listH(); break;
            case 2: deleteH; break;
            case 3: updateH(); break;
            case 4: addH(); break;
            case 5: break;
            default: cout << "There's no such choice in the menu!" << endl;
        }

        if (choice!=3) {
   	  	 	cout << "Press any key to continue" << endl;
   	  	 	cin >> temp;
      	}

    } while (choice!=5);
}

As you can see, the functions listH, deleteH, updateH, and addH are important.
listH - lets you view the file in the command prompt
addH - lets you add an item (record number, tool name, quantity, and cost) and if you wanna make sure that the item is added, choose List

However, I'm having problems with updateH and deleteH.
deleteH - the user should input the record number and all the info (tool name, quantity, and cost) will be deleted also. I commented the part which makes the program non-run-able, if there's such a word.
updateH - for example, the user wants to edit, not delete, the info (tool name, quantity, and cost)
/*Linked list shouldn't be used*/

Another problem is the spacing but I guess it's tolerable.

But please help me with the functions updateH and deleteH

Example (when the program runs)
(choose) 4 (to add because initially, the info is empty)
(record#) 1
(tool name) chainsaw
(quantity) 3
(cost) 890.54
//press any key to continue like "c"
(choose) 1 (to view)
..and so on.

Please experts I need your help. I will appreciate them so much! Thank you!

2
Contributors
1
Reply
10
Views
5 Years
Discussion Span
Last Post by Moschops
0

To change the contents of a file, the easiest way is to copy everything (but changing the bit you want changed, or just not copying the bit you want deleted) to a new file, delete the old one, and rename the copy.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.