Arrays with outputFilles.

So I have the function for choice 2 at the bottom of the program and I'm trying to implement it into the code so its functioning correctly. Also I'm trying to implement
void writeFile(string itemNames[], double itemCost[], int itemNoShip[][])
as well. When writing that function do I use a vector for the 2d array? My textbook is only showing for 1 kind of array being written to the .txt file not 3 seperate in different columns in the .txt file if that makes sense

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
163
164
165
166
167
168
169
170
171
172
173
174
  // The program will read an inventory file into 3 parallel arrays.
// One array will contain strings for Item Description
// One array will contain doubles for Cost Per Item
// One array will contain doubles for No. of Items and item Shippable status.
// The program will present a menu presents a user with 4 choices:
// 1. Read the inventory file, 2. Display the Inventory, 
// 3. Write the inventory to a file, 4. Exit the program.
// The program should validate user input for a valid menu option.
// The program should not continue if the file is not read.
// 06APR2022 Group 2

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

using namespace std;

// Function prototypes.
void logo();                            // This function outputs the company logo.

void menu();                            // This function outputs the menu.

bool read_and_display_file();           // test read of external file by calling a function

void displayInventory(string names[], double cost[], int ship[][2])

int main()
{

    //  Declare constants and variables.

    int userChoice;         // hold user's menu selection
    string itemNames;       // represents item description content from input file
    ifstream inputFile;     // inventory file - is located in function read_file


    do
    {
        // Step 2.  Call the logo function to output the company logo.
        logo();

        // Call the menu function to display the menu.
        menu();

        // Input user selection.
        cin >> userChoice;
        cin.ignore(2, '\n'); // To prevent program from storing beyond the first digit entered

        // exit(0);  // just for testing

        switch (userChoice)
        {
        case 1:
        {
            // Testing - only the Item Descriptions are included in the test file
            // Open the file and output all lines.
            // If successful, then break it down into the 3 arrays per the Mod 6 assignment
            read_and_display_file();
            break;
        }
        case 2:
            cout << "You chose " << userChoice << endl;
            void displayInventory(string names[], double cost[], int ship[][2])
            break;

        case 3:
            cout << "You chose " << userChoice << endl;
            break;
        case 4:
            cout << "\nGood bye!\n" << endl;
            break;
        default:
            cout << "Invalid Menu Item entered...must enter 1-4...\n";
        }

    } while (userChoice != 4);


    return 0;

}

//*****************************************************************
// This function outputs the company logo.                        *
//*****************************************************************

void logo()
{
    cout << "\n     ***   FUN FOODS FAST   ***\n";
    cout << "***************************************\n";
    cout << "*     *******   *******   *******     *\n";
    cout << "*     *******   *******   *******     *\n";
    cout << "*     **        **        **          *\n";
    cout << "*     ******    ******    ******      *\n";
    cout << "*     ******    ******    ******      *\n";
    cout << "*     **        **        **          *\n";
    cout << "*     **        **        **          *\n";
    cout << "*     **        **        **          *\n";
    cout << "***************************************\n";
}

//*****************************************************************
// This function should generate the menu.         *
//*****************************************************************

void menu()
{
    // Step 2. Display the menu.
    cout << "\n1. Read in Inventory\n"
        << "2. Display Inventory\n"
        << "3. Write to File\n"
        << "4. Exit\n";
}


//******************************************************************
// This function should read the external inventory file           *
// and display it's contents                                       *
//******************************************************************

bool read_and_display_file()  // This is for testing to see if the file contents are read and displayed
{
    ifstream inputFile;     // inventory file
    string itemNames;

    inputFile.open("c:\\temp\\inputInventory.txt");  // Open the file.

    if (inputFile)  //  Return true if file is found or opened
    {
        while (getline(inputFile, itemNames))      // Read all rows in the file
        {
            cout << endl << itemNames;      // Display each row's content
        }

        cout << endl;
        inputFile.close();          // Close the file.
        return true;
    }
    else  // Return false if file cannot be found or opened
    {
        cout << "\nFile could not be read.\n";
        return false;
    }


//******************************************************************
//  This function should display the inventory file
//   and display its contents
//******************************************************************

void displayInventory(string names[], double cost[], int ship[][2])
{
    int count = 0;          // counter value for loop

    // Display column headings.
    cout << "\nItem Name" << "\t\t"
        << "Cost" << "\t"
        << "No. Stock" << "\t"
        << "Shipping (1-Yes 0-No)\n";

    // Formatting for consistent output of data type doubles.
    cout << setprecision(2) << fixed << showpoint;

    // Loop to dispaly values from the input file.
    for (count = 0; count < 10; count++)
        cout << names[count] << "\t"
        << setw(12) << cost[count]
        << setw(13) << ship[count][0]
        << setw(25) << ship[count][1] << "\n";

}
Last edited on
crunch_bar	1.50	10	1
chocolate_bar	1.75	15	1
carrot_bar	1.50	7	0
caramel_bar	1.80	12	1
nutty_bar	1.90	20	1
taffy_bar	2.50	15	1
mint_bar	2.75	12	0
cherry_bar	3.00	8	1
cranapple_crisp	2.75	15	1
peach_crisp	2.50	16	1



This is what the .txt file looks like. the $x.xx is price. The double digit is the quantity and the 1 and 0 is shipped or not.
Last edited on
Starting to write what I think would be the function for the first array column. Am I heading in the right direction? Looking at my textbook this looks like what it should look like but not 100% sure

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void writeFile(string itemNames[], double itemCost[], int itemNoShip[10][2])
{
    ofstream outputFile;
    const int SIZE = 10;
    string itemNames[SIZE] = { "crunch_bar", "chocolate_bar", "carrot_bar", "caramel_bar",
                                "nutty_bar", "taffy_bar", "mint_bar", "cherry_bar",
                                "cranapple_crisp", "peach_crisp" };
    cout << "Item Name" \n;
    for (count = 0; count < SIZE; count++)
        itemNames[count] = count;

    outputFile.open("inputInventory.txt");

        for (count = 0; count < SIZE; count++)
            outputFile << itemNames[count] << endl;

        outputFile.close();

        cout << "The items were saved to the file!\n ";
   
}
This has several issues. itemNames is multiple defined, L10 does not make any sense. count is not defined before usage...

Perhaps something like (not tried):

1
2
3
4
5
6
7
8
9
10
void writeFile(string itemNames[], double itemCost[], int itemNoShip[10][2]) {
    const int SIZE = 10;

    ofstream outputFile("inputInventory.txt");

    for (int count = 0; count < SIZE; count++)
        outputFile << setw(20) << left << itemNames[count] << right << setw(5) << itemCost[count] << '\n';

    cout << "The items were saved to the file!\n ";
}

1
2
3
        // Input user selection.
        cin >> userChoice;
        cin.ignore(2, '\n'); // To prevent program from storing beyond the first digit entered 


Your comment is incorrect, the input has already happened before the cin.ignore. Since userChoice is an int type the value accepted by the program could be any valid integer, 1, 1000, 100,000, etc.

All the cin.ignore is going to do is try to remove up to two characters from the input buffer after the integer was extracted.

// The program should not continue if the file is not read.

You're not really doing this, the program should probably stop if the file can't be read.

Also you should realize that it is possible to bypass reading the file altogether.
Topic archived. No new replies allowed.