Code Output Not What I Expected

This is a homework problem, and I know we aren't supposed to post about these kind of questions, but I have all the code written out except for I am not getting the output file as expected.

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
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
using namespace std;

struct division
{
    char divisionName[7];
    int quarter;
    double quarterlySales;
};

int main()
{
    fstream file("corp.txt", ios::out | ios::binary);
    division north, east, west, south;

    //Store names of divisions
    strcpy (north.divisionName, "North");
    strcpy (east.divisionName, "East");
    strcpy (south.divisionName, "South");
    strcpy (west.divisionName, "West");

    cout << "Enter the quarterly sales for the North division.\n";
    for (int k = 1; k <= 4; k++)
    {
        north.quarter = k;
        cout << "Enter quarter " << k << " sales: ";
        cin >> north.quarter;
        while (north.quarter < 0)
        {
            cout << "The sales figures cannot be less than 0. Re-enter the sales figures for quarter " << k << ": ";
                    cin >> north.quarter;
        }
        file.write(reinterpret_cast<char *> (&north.quarter), sizeof(north));
    }
    cout << endl;

    cout << "Enter the quarterly sales for the East division.\n";
    for (int k = 1; k <= 4; k++)
    {
        east.quarter = k;
        cout << "Enter quarter " << k << " sales: ";
        cin >> east.quarter;
        while (east.quarter < 0)
        {
            cout << "The sales figures cannot be less than 0. Re-enter the sales figures for quarter " << k << ": ";
                    cin >> east.quarter;
        }
        file.write(reinterpret_cast<char *> (&east.quarter), sizeof(east));
    }
    cout << endl;

    cout << "Enter the quarterly sales for the South division.\n";
    for (int k = 1; k <= 4; k++)
    {
        south.quarter = k;
        cout << "Enter quarter " << k << " sales: ";
        cin >> south.quarter;
        while (south.quarter < 0)
        {
            cout << "The sales figures cannot be less than 0. Re-enter the sales figures for quarter " << k << ": ";
                    cin >> south.quarter;
        }
        file.write(reinterpret_cast<char *> (&south.quarter), sizeof(south));
    }
    cout << endl;

    cout << "Enter the quarterly sales for the West division.\n";
    for (int k = 1; k <= 4; k++)
    {
        west.quarter = k;
        cout << "Enter quarter " << k << " sales: ";
        cin >> west.quarter;
        while (west.quarter < 0)
        {
            cout << "The sales figures cannot be less than 0. Re-enter the sales figures for quarter " << k << ": ";
                    cin >> west.quarter;
        }
        file.write(reinterpret_cast<char *> (&west.quarter), sizeof(west));
    }
    cout << endl;

    return 0;
}


This code works and everything but the txt file I am getting is just a jumbled mess that I can't make sense of.

I expect this code to write to a file and output each division name followed by the 4 numbers I enter that correspond to the division.

Instead the output file I get reads:

  8þa Õ¢îo°Ýðo   8þa Õ¢îo°Ýðo   8þa Õ¢îo°Ýðo   8þa Õ¢îo°Ýðo  þa North  þa North  þa North  þa North  hðo West  hðo West  hðo West  hðo West  ðo ðoz'ïoEast ðo ðo ðoz'ïoEast ðo ðo ðoz'ïoEast ðo ðo ðoz'ïoEast ðo
If you really want a file you can read, then you need to do this instead.

file << north.divisionName << "," << north.quarter << "," << north.quarterlySales << endl;

And remove the | ios::binary from the stream initialisation.
Instead of using file.write?

Edit: I figured it out. Thanks for the help!!
Last edited on
Topic archived. No new replies allowed.