0

Hi Am making a simple class program about storing Customer records in a binary file using structs.
Problem is with "Display All" function So far i can add records to binary file but when i try to list all file items using my DisplayAll function, it just spits out junk at me.

the simple read function for binary files works fine so i can read one file at a time. Am wondering if the problem is with my logic in my code.

I debugged but ended up in fstream inbuilt code.

    */ Program #2
    This program will use structs to store data about
    accounts  */


#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

const int SIZE = 16;

**struct Cust
{
    char name[SIZE];
    char phone[SIZE];
    float accountBal;
};**

// Function Prototypes

// Function to enter new records into file. 

// Function to display menu
void menu ();
void addCust(fstream&);
void displayAll(fstream&);

int main ()
{
    char choice [4];
    fstream custBinFile;  // Filestream for I/O to Binary File

    // Opening Binary File for Both input and output. 
    custBinFile.open("custBinFile.dat", ios::out|ios::in|ios::binary);

    cout<< "Enter any choice from the above Menu List:"<<endl;
    do
    {
        menu();
        cin >> choice;
        // If to handle adding a record to file
        **if((strcmp(choice, "A") == 0) || (strcmp(choice, "a") == 0))
        {
            addCust(custBinFile);
        }**
        // Displaying one record from File
        else if((strcmp(choice, "F") == 0)|| (strcmp(choice, "f") == 0))
        {
            cout << "Find and Display record \n" << endl;
        }
        // Handles deleting customer record
        else if((strcmp(choice, "D") == 0)|| (strcmp(choice, "d") == 0))
        {
            cout << "Delete Customer Record \n" << endl;
        }
        // Find and Change record
        else if((strcmp(choice, "C") == 0)|| (strcmp(choice, "c") == 0))
        {
            cout << "Find and Change Record \n" << endl;
        }
        // Displays all contents sorted in order
        **else if((strcmp(choice, "L") == 0)|| (strcmp(choice, "l") == 0))
        {
            displayAll(custBinFile);
        }**
        // handles Bad input: Input Validation
        else if((strcmp(choice, "E") != 0)|| (strcmp(choice, "e") != 0))
            cout << "Bad input, you twit!! Change that" << endl;

    } while((strcmp(choice, "E") != 0) && (strcmp(choice, "e") != 0));

    cout << "Exiting now ";


    custBinFile.close();
    return 0;
}

void menu()
{
    cout<< "Main Menu"<<endl;
    cout<< " \n";
    cout<< "A - Add Customer Record \n";
    cout<< "F - Find and Display Record\n";
    cout<< "D - Delete Customer Record\n";
    cout<< "C - Change Customer Record\n";
    cout<< "L - List All Records\n";
    cout<< "E - Exit\n";
    cout<< " \n";
    cout<< "Enter letter corresponding to your choice:"<<endl;
}

**void addCust(fstream& custBinFile)
{
    // Initialise Variables
    Cust custType;
    // User inputs record Info.
    cout << "Enter the customer's name: "; 
    cin >> custType.name;
    cout << "\n Phone Number: ";
    cin >> custType.phone;
    cout << "\n Account Balance: ";
    cin >> custType.accountBal;

    // If empty record found in File, search it and overwrite it with new record
    // Otherwise, add record to Binary File. 
    // Open record to Binary File.
    custBinFile.write(reinterpret_cast<char *> (&custType), sizeof(custType));
}**

**void displayAll(fstream& custBinFile)
{
    // Displaying all Structs in File. 
    Cust custType;
    while(!custBinFile.eof())
    {
        custBinFile.read(reinterpret_cast<char *> (&custType), sizeof(custType));
        cout << "Customer Name: " << custType.name << endl;
        cout << "Customer Phone Number: " << custType.phone << endl;
        cout << "Customer Account Balance: " << custType.accountBal << endl;
    }
}**

1 Answers1

0

It looks like if you foget to shift position in a file up to the end before appending a new record (or you try to read from the end of a file). upd: by the way, it is not a good idea to write in such a way due to bite-order and words-aligning issues.

Valerij
  • 71
  • 1
  • 3