structured data writing file from function

I am modifying a previous assignment program to add 2 more menu items. 1 additional menu option to write structure data input by user into a file for insured items, and for uninsured items into a separate file; namely insured.dat and uninsured.dat.

The problem I am having is with what is being written to the files.

EXAMPLE:

description 1: abc
quantity 1: 4
price 1: 5.00
insured 1: Y

description 1: def
quantity 1: 4
price 1: 5.00
insured 1: Y

description 1: xxx
quantity 1: 4
price 1: 5.00
insured 1: N

When program runs, and (my option 5) menu option selected, my insured.dat file will store:

abc I assume one for each items insured.
abc

also uninsured.dat will store only one(because only one uninsured item input) but it is still the first entry

abc

I don't know if it is a problem with my loop, but I have tried while loops, do while loops. I tried reinterpret_cast operator, with out the reinterpret.... I don't know what the whole structure will not store in the file.

P.S. I know I could clean this code up with some extra time. i.e. switch/case etc.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
  
#include <iostream>
#include <cstring>
#include <iomanip>
#include <fstream>
using namespace std;

const int AMOUNT = 3;

//STRUCTURE PROTOTYPE ********************************************************************
//****************************************************************************************
struct HouseholdItems       //structure prototype
{
    string description;     //using various data types inside the structure
    int quantity;
    float price;
    char insured;
};

//FUNCTION PROTOTYPES ********************************************************************
//****************************************************************************************
void getData(HouseholdItems items[AMOUNT]);
void displayAll(HouseholdItems items[AMOUNT]);
void totalItems(HouseholdItems items[AMOUNT]);
void totalValue(HouseholdItems items[AMOUNT]);
void totalInsured(HouseholdItems items[AMOUNT]);
void write(HouseholdItems items[AMOUNT]);
//MAIN FUNCTION **************************************************************************
//****************************************************************************************

int main()
{
    char goAgain;                                                       // Reads user input to choose another option
    int choice = 0;                                                     // Reads users choice form the menu
    cout << "This program will display various data about your household items.\n";
    cout << "Please enter the information about your items...\n";
    
    HouseholdItems items[AMOUNT];                                       //Using HouseholdItems as new data type
    HouseholdItems *pointHouse;                                          
    pointHouse = &items[AMOUNT];                                        // Not sure if the array is acting as the pointer or not.
    getData(items);                                                     // Getting user information about the household items
    
    do
    {
        // Begin menu selection //
        
        cout << endl;
        cout << endl;
        cout << "Select an option: \n";
        cout << "1. Display all item information.\n";
        cout << "2. Display item amount total.\n";
        cout << "3. Display total value of items.\n";
        cout << "4. Display only insured items.\n";
        cout << "5. Write insured data to file; return file quantity.\n";
        cout << "6. Display insurance data from file.\n";
        cout << "7. To QUIT";
        cout << endl;
        cin >> choice;
        
        if (choice == 1)
        {
            displayAll(items); // Function call given true result
        }
        else if (choice == 2)
        {
            totalItems(items);  // Function call given true result
        }
        else if (choice == 3)
        {
            totalValue(items);  // Function call given true result
        }
        else if (choice == 4)
        {
            totalInsured(items); // Function call given true result
        }
        else if (choice == 5)
        {
            write(items);
        }
        else if (choice == 6)
        {
            //read(items);      // Haven't built this yet
        }

        else if (choice == 7)
        {
            return 0;  // End program if user selects option 7(QUIT)
        }
        cout << "\nWould you like to select another option? (Y/N): ";
        cin >> goAgain;
    }while (goAgain == 'y' || goAgain =='Y');  //If user wants to keep selecting from the menu, otherwise
    return 0;  // end program if selcetion not yes
}
//FUNCTION DEFINITIONS *******************************************************************
//****************************************************************************************
void getData(HouseholdItems items[AMOUNT])
{
    int yesInsured = 0;                                                 // Holds insured item number
    int noIsnsured = 0;                                                 // Holds uninsured number
    for (int i = 0; i < AMOUNT; i++)                                    //for loop gathering data for array structure
    {
        cout << endl;
        cout << "Enter item " << i + 1 << " description: \n";           // User input item
        //cin >> items[i].description;                                  // store item in items[i].description
        cin. ignore();
        
        getline(cin, items[i].description);
        
        cout << endl;
        cout << "Enter the quantity of item " << i + 1 << ": ";         // User input quantity
        cin >> items[i].quantity;                                       // store quantity in item[i].quantity
        cout << endl;
        cout << "Enter the price of item " << i + 1 << ": ";            // User enter price
        cin >> items[i].price;                                          // Store price
        if (items[i].price > 10000)                                     // Condidtional error check price < 10,000
        {
            cout << "Item cannot exceed $10,000. Try again. ";          // Return true
            cin >> items[i].price;
        }
        
        cout << endl;
        cout << "Is item " << i + 1 << " insured? (Y)es or (N)o: ";     // User input item insured
        cin  >> items[i].insured;                                       // Store choice
        if (items[i].insured == 'y' || items[i].insured == 'Y')         // check codition for yes
        {
            yesInsured += 1;                                            // add to yesInsured if condition met
        }
        else if (items[i].insured == 'n' || items[i].insured == 'N')    // check condition for no
        {
            noIsnsured += 1;                                            // add to noInsured if condition met
        }
        else
            {
            cout << "Invalid entry. Please enter Y or N";               // else stmt for incorrect entry
            cin >> items[i].insured;
            }
    }
    
}
// FUNCTION WRITING DATA TO FILE***********************************************************
void write(HouseholdItems items[AMOUNT])
{
    fstream myFile("insured.dat", ios::out | ios::binary);
    fstream yourFile("uninsured.dat", ios::out | ios::binary);

    for(int i = 0; i < AMOUNT; i++)
    {
        if(items[i].insured == 'y' || items[i].insured == 'Y')
            {
                myFile.write((char *)items,sizeof(items));
                cout << endl;
            }
        else if(items[i].insured == 'n' || items[i].insured == 'N')
        {
            yourFile.write(reinterpret_cast <char*>(items),sizeof(items));
            cout << endl;
        }
    }
    myFile.close();
    yourFile.close();
}
I see a couple of problems in your write() function.

First and probably most important, the sizeof() calls are probably not producing the value you expect.

Second how are you "viewing" the data that your file contains? Remember a binary file is not considered human readable, you'll need a "hex-dump" type of program to view the file contents. What's with the cout calls? And what happens if the file opening fails? You probably should write your "read" function to be able to verify the values were written correctly.

Third you don't actually need the else if(), a simple else should be all you need and you should stick with the C++ style casts instead of the C style casts.

Lastly since your file close() calls are at the end of the function, they are not really necessary. Just let the destructor close the files when the function returns.

Topic archived. No new replies allowed.