Creating multiple output files using ofstream.

I've been working on this sales problem that has me write a program that takes in a list of salespersons ID number(1-10), item number(7-14), and quantity, example (2,7,4) and prints out sales value to an output file. My problem is that after the first line of data has been processed from the text file, my program no longer reads in values it seems, nor creates new files.

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
#include <iostream>
#include <fstream>
#include <iomanip>          //for data output manipulators
#include <string>
using namespace std;

int main()
{
    int idNumber;           //stores each sale persons ID number
    int itemNumber;         //stores product ID
    int quantity;           //stores number of specific items sold
    double itemCost;        //stores unit cost
    string inFile;          //file contains all sale persons information
    string outFile;
    ifstream inData;
    ofstream outData;

    cout << "Please enter the name of the data file "
         << "followed by the [Enter] key." << endl;
    cin >> inFile;
    inData.open(inFile.c_str());
    if(!inData)                                     //tests if file opened correctly
    {
        cout << "Could not open file." << endl;
        return 1;
    }
    inData >> idNumber >> itemNumber >> quantity;
    do
    {
        if(idNumber <= 0 || idNumber > 10)          //checks for valid idNumbers
        {
            cout << "Invalid ID Number." << endl;
            return 2;
        }
        switch(itemNumber)                          //assigns value to variable itemCost
        {                                           //depending on itemNumber
            case 7  : itemCost = 345.00;
                    break;
            case 8  : itemCost = 853.00;
                    break;
            case 9  : itemCost = 471.00;
                    break;
            case 10 : itemCost = 933.00;
                    break;
            case 11 : itemCost = 721.00;
                    break;
            case 12 : itemCost = 663.00;
                    break;
            case 13 : itemCost = 507.00;
                    break;
            case 14 : itemCost = 259.00;
                    break;
            default : cout << "Invalid item number." << endl;
                    break;
        }
         cout << "Enter the name of the sale persons file." << endl;
         cin >> outFile;
         outData.open(outFile.c_str());
         outData << "Sales Person: " << idNumber << endl
              << "Total Sales: $" << double(quantity) * itemCost << endl << endl;

    }
    while(inData);
    return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
//input text file.
//Name is "sales.txt
7 7 7 //only line read 
1 9 2
2 8 2
3 10 1
4 14 3
5 7 2
6 9 3
8 12 5
9 9 11
10 10 4


1
2
3
4
//only file created. corresponds to the first line of data
//file name is "salesOut.txt"
Sales Person: 7
Total Sales: $2415


Not sure if i'm not inputting in the rest of the values correctly after the first line. Also no more text files are created after the first prompt for an output text file.
Last edited on
You don't read any data in in the do-while loop... so it shouldn't be any surprise that no data after the first line is read in.

After the first iteration of the loop, line 58 will fail since outData is already associated with an open file from the first iteration.
wooo! awesome! thank you so much! your comment on my line 58 saved me so much more time too! now everything prints out fine.
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
#include <iostream>
#include <fstream>
#include <iomanip>          //for data output manipulators
#include <string>
using namespace std;

int main()
{
    int idNumber;           //stores each sale persons ID number
    int itemNumber;         //stores product ID
    int quantity;           //stores number of specific items sold
    double itemCost;        //stores unit cost
    string inFile;          //file contains all sale persons information
    string outFile;
    ifstream inData;
    ofstream outData;

    cout << "Please enter the name of the data file "
         << "followed by the [Enter] key." << endl;
    cin >> inFile;
    inData.open(inFile.c_str());
    if(!inData)                                     //tests if file opened correctly
    {
        cout << "Could not open file." << endl;
        return 1;
    }
    inData >> idNumber >> itemNumber >> quantity;
    do
    {
        if(idNumber <= 0 || idNumber > 10)          //checks for valid idNumbers
        {
            cout << "Invalid ID Number." << endl;
            return 2;
        }
        switch(itemNumber)                          //assigns value to variable itemCost
        {                                           //depending on itemNumber
            case 7  : itemCost = 345.00;
                    break;
            case 8  : itemCost = 853.00;
                    break;
            case 9  : itemCost = 471.00;
                    break;
            case 10 : itemCost = 933.00;
                    break;
            case 11 : itemCost = 721.00;
                    break;
            case 12 : itemCost = 663.00;
                    break;
            case 13 : itemCost = 507.00;
                    break;
            case 14 : itemCost = 259.00;
                    break;
            default : cout << "Invalid item number." << endl;
                    break;
        }
        cout << "Enter the name of the output file." << endl;   //prompt user for first output file
        cin >> outFile;
        outData.open(outFile.c_str());                          
        outData << "Sales Person: " << idNumber << endl      //calculations printed on output file
                << "Total Sales: $" << double(quantity) * itemCost << endl;
        outData.close();                                //close current outputfile before opening next file 
        inData >> idNumber >> itemNumber >> quantity;

    }
    while(inData);
    return 0;
}


1
2
3
4
5
6
7
8
9
10
11
//same input file
7 7 7
1 9 2
2 8 2
3 10 1
4 14 3
5 7 2
6 9 3
8 12 5
9 9 11
10 10 4


1
2
3
4
//output
//10 different text files have been created. each corresponding to a different line
//containing each lines calculations


Topic archived. No new replies allowed.