CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2017
    Posts
    2

    c++ database.it should let you enter an account but doesnt save what i put in i think

    Code:
    #include<iostream>
    #include<fstream>
    #include<iomanip>
    #include<cstdlib>
    #include<cstring>
    using namespace std;
    
    const int NAME_SIZE = 25;
    const int ADDR_SIZE = 30;
    const int CSZ_SIZE = 30;
    const int PHONE_SIZE = 15;
    const int DLP_SIZE = 20;
    int NUM_RECORDS = 0;
    
    
    
    void write_account();
    void display(int num);
    void display_all();
    void display_sp(int num);
    void delete_account(int num);
    
    struct Account{
        char name[NAME_SIZE];
        char addr[ADDR_SIZE];
        char csz[CSZ_SIZE];
        char phone[PHONE_SIZE];
        double balance;
        char dlp[DLP_SIZE];
    };
    
    int main()
    {
    	char ch;
    	int num;
    
    	do
    	{
    		cout<<"\n**********************MAIN MENU*********************************";
    		cout<<"\n1. Enter a New Customer Account";
    		cout<<"\n2. Display a Customer Account";
    		cout<<"\n3. Delete a Customer Account";
    		cout<<"\n4. Change a customer account";
    		cout<<"\n5. Display All Customer Accounts";
    		cout<<"\n6. EXIT THE PROGRAM";
    		cout<<"\nEnter Your Option (1-6) ";
    		cin>>ch;
    
    		switch(ch)
    		{
                case '1':
                    write_account();
                    break;
    
                case '2':  // display account number
                    cout<<"\n\n\tEnter The account No. : ";
                    cin>>num;
                    display(num);
                    break;
    
                case '3':
                    cout<<"\n\n\tEnter The account No. : ";
                    cin>>num;
                    delete_account(num);
                    break;
    
                case '4':
                    cout<<"\n\n\tEnter The account No. : ";
                    cin>>num;
                    display_sp(num);
                    break;
    
                case '5':
                    display_all();
                    break;
    
                case '6':
                    cout<<"\n\n\tThank you come again";
                    break;
    
                default :cout<<"\a";
    		}
            cin.ignore();
    		cin.get();
    	}while(ch!='6');
    
    
    	return 0;
    
    }
        void write_account()
        {
            Account ac = {"","","","",0.0,""};
            double tempBalance;
            cout << "Name: ";
            cin.ignore();
            cin.getline(ac.name, NAME_SIZE);
            cout << "Address: ";
            cin.getline(ac.addr, ADDR_SIZE);
            cout << "City, State, ZIP ";
            cin.getline(ac.csz, CSZ_SIZE);
            cout << "PHONE # : ";
            cin.getline(ac.phone, PHONE_SIZE);
            cout << "Balance: ";
                cin >> tempBalance;
                if (tempBalance < 0 ){
                    ac.balance = tempBalance;
                }
            cout << "Date of Last Payment: ";
            cin.getline(ac.dlp, DLP_SIZE);
    
            fstream customers("cust.dat", ios::app | ios::binary);
            customers.write(reinterpret_cast<char *> (&ac), sizeof(ac));
            customers.close();
            NUM_RECORDS++;
        }
    
        void display(int num){
            Account record;
            fstream inventory("cust.dat", ios::in | ios::binary);
            // Now read and display the records.
            inventory.seekg(num* sizeof(record), ios::beg);
            inventory.read(reinterpret_cast<char *>(&record), sizeof(record));
    
            cout<< "Name: " <<  record.name << endl;
            cout<< "Address: " <<  record.addr << endl;
            cout<< "City, State, Zip: " <<  record.csz << endl;
            cout<< "Phone Number: " <<  record.phone << endl;
            cout<< "Current Balance: $" <<  record.balance << endl;
            cout<< "Date of Last Payment " << record.dlp << endl;
            // Close the file.
            inventory.close();
        }
    
        void display_all(){
            cout<< "\n";
            cout << "Displaying all records."<<endl;;
    
            Account record;
            fstream inventory("cust.dat", ios::in | ios::binary);
            // Now read and display the records.
            inventory.read(reinterpret_cast<char *>(&record), sizeof(record));
            while(!inventory.eof()){
            cout<< "Name: " <<  record.name << endl;
            cout<< "Address: " <<  record.addr << endl;
            cout<< "City, State, Zip: " <<  record.csz << endl;
            cout<< "Phone Number: " <<  record.phone << endl;
            cout<< "Current Balance: $" <<  record.balance << endl;
            cout<< "Date of Last Payment " << record.dlp << endl;
            cout<< "\n";
            inventory.read(reinterpret_cast<char *>(&record), sizeof(record));
            }
            // Close the file.
            inventory.close();
        }
    
        void display_sp(int num){
            Account record;
            fstream inventory("cust.dat", ios::in | ios::out | ios::binary);
            // Now read and display the records.
            inventory.seekg(num* sizeof(record), ios::beg);
            inventory.read(reinterpret_cast<char *>(&record), sizeof(record));
    
            double tempBalance;
            cout << "Name: ";
            cin.ignore();
            cin.getline(record.name, NAME_SIZE);
            cout << "Address: ";
            cin.getline(record.addr, ADDR_SIZE);
            cout << "City, State, ZIP ";
            cin.getline(record.csz, CSZ_SIZE);
            cout << "PHONE # : ";
            cin.getline(record.phone, PHONE_SIZE);
            cout << "Balance: ";
                cin >> tempBalance;
                if (tempBalance > 0 ){
                    record.balance = tempBalance;
                }
            cout << "Date of Last Payment: ";
            cin.getline(record.dlp, DLP_SIZE);
    
    
            inventory.seekp(num * sizeof(record), ios::beg);
            inventory.write(reinterpret_cast<char *>(&record), sizeof(record));
    
            // Close the file.
            inventory.close();
        }
    
        void delete_account(int num){
            Account record;
            fstream inventory;
            fstream tempFile;
            inventory.open("cust.dat", ios::in | ios::out | ios::binary);
    
            if (inventory.fail())
            {
                cout << "\nError opening file.\n";
                inventory.close();
                exit(0);
            }
    
            // Now read and display the records.
            strcpy(record.name, "\0");
            inventory.seekp(num, ios::beg);
            inventory.write(reinterpret_cast<char *>(&record),sizeof(record));
            inventory.close();
    
            inventory.open("cust.dat", ios::in | ios::binary);
            tempFile.open("temp.dat", ios::out | ios::binary);
            inventory.read(reinterpret_cast<char *>(&record),sizeof(record));
    
            while (inventory)
            {
                tempFile.write(reinterpret_cast<char *>(&record),sizeof(record));
                inventory.read(reinterpret_cast<char *>(&record),sizeof(record));
            }
            inventory.close();
            tempFile.close();
            tempFile.open("temp.dat", ios::in | ios::binary);
            inventory.open("cust.dat", ios::out | ios::binary);
            tempFile.read(reinterpret_cast<char *>(&record),sizeof(record));
            while (!tempFile.eof())
            {
                if(record.name[0] != '\0')
                {
                    inventory.write(reinterpret_cast<char *>(&record),sizeof(record));
                }
                tempFile.read(reinterpret_cast<char *>(&record),sizeof(record));
            }
            tempFile.close();
            inventory.close();
            cout << "\nDeletion successful.\n";
        }
    Last edited by 2kaud; May 16th, 2017 at 01:48 AM. Reason: Added code tags

  2. #2
    Join Date
    May 2017
    Posts
    2

    Re: c++ database.it should let you enter an account but doesnt save what i put in i t

    i dont think it is saving what i put into the file. displays jargin.

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: c++ database.it should let you enter an account but doesnt save what i put in i t

    When posting code, please use code tags so that the code is readable. Go Advanced, select the formatted code and click '#'.

    Have you looked at the content of the file to see what has been written?

    Code:
    cout << "Balance: ";
    	cin >> tempBalance;
    	if (tempBalance < 0) {
    		ac.balance = tempBalance;
    	}
    	cout << "Date of Last Payment: ";
    	cin.getline(ac.dlp, DLP_SIZE);
    Part of the problem is here. You are mixing stream extraction (tempBalance) with getline. The issue is that stream extraction extracts up to the terminating white space character (line feed here) but doesn't extract it from the stream. getline() expects input to be terminated with the delim character (linefeed) but doesn't ignore delim characters at the start (like stream extraction does). So to follow a stream extraction with getline() you need to remove the linefeed from the stream (eg by using .ignore()).

    You are also not checking that the file(s) have been opened successfully. You can use .is_open() to check. See http://www.cplusplus.com/reference/f...tream/is_open/
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: c++ database.it should let you enter an account but doesnt save what i put in i t

    A couple of further points

    Code:
    inventory.seekg(num * sizeof(record), ios::beg);
    There is no checking that the num'th record actually exists and is not beyond the end of the file. What happens if there are 3 records on the file and num is entered as 10?

    Code:
    inventory.read(reinterpret_cast<char *>(&record), sizeof(record));
    while(!inventory.eof()) {
        //....
        inventory.read(reinterpret_cast<char *>(&record), sizeof(record));
    }
    This would more usual be coded as
    Code:
    while (inventory.read(reinterpret_cast<char *>(&record), sizeof(record))) {
        //...
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured