I dont know where im going wrong. can anyone help please?

This is supposed to be my input file
John Smith A
Sue Jones C
Ray Adams B

This should be my output file
Smith, John, Zone A, $75.00
Jones, Sue, Zone C, $30.00
Adams, Ray, Zone B, $55.00
Total Charges: $160.00


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
  #include<iostream>
#include<cstring>
#include<cmath>
#include <iomanip>
#include <fstream>
#include <cstdlib> 
#include<ctime> 
#include<string>

using namespace std;
const double ZONE_A = 75.00;
const double ZONE_B = 55.00;
const double ZONE_C = 30.00;
const int SEATS = 100;


struct TicketHolder
{
	string holderFName;
	string holderLName;
	char zone;
	double charge;
};
int loadData(TicketHolder audience[]);
void addCharge(TicketHolder audience[], int numPeople);
double totalCharge(TicketHolder audience[], int numPeople);
void outputData(TicketHolder audience[], int numPeople, double someTotal);

int main()
{
	int numPeople = 0;
	double someCharge = 0;
	TicketHolder audience[SEATS];
	numPeople = loadData(audience);
	addCharge(audience, numPeople);

	someCharge = totalCharge(audience, numPeople);

	outputData(audience, numPeople, someCharge);


	system("pause");
	return 0;
}

int loadData(TicketHolder audience[])
{
	TicketHolder inputStruct;
	string inputFile;
	
	ifstream inFile;
	int i = 0;
	cout << "Please enter the file name of the audience names and their zones " << endl;
	cin >> inputFile;
	inFile.open(inputFile);
	while (inFile.peek() != EOF)
	{
   		inFile >> inputStruct.holderFName;
		cin.ignore();
		inFile >> inputStruct.holderLName;
		cin.ignore();
		inFile >> inputStruct.zone;
		cin.ignore();
		audience[i] = inputStruct;
		i++;
		if (i == 99)
		{
			cout << " Out of Seats " << endl;
			break;
		}
	}

	return i;
	inFile.close();
}

void addCharge(TicketHolder audience[], int numPeople)
{	
	//TicketHolder audience;
	for (int i = 0; i < numPeople; i++)
	{
		if (audience[i].zone == 'A')
			audience[i].charge = ZONE_A;
		else if (audience[i].zone == 'B')
			audience[i].charge = ZONE_B;
		else if (audience[i].zone == 'C')
			audience[i].charge = ZONE_C;
	}
}
double totalCharge(TicketHolder audience[], int numPeople)
{
	double total = 0;
	for (int i = 0; i < numPeople; i++)
	{
		total += audience[i].charge;
	}
	return total;
}
void outputData(TicketHolder audience[], int numPeople, double someTotal)
{
	TicketHolder output;
	ofstream outFile;
	string outputFile;
	cout << " What do you want the output file to be called? " << endl;
	cin >> outputFile;
	outFile.open(outputFile);
	for (int i = 0; i < numPeople; i++)
	{
		outFile << output.holderLName;
		outFile << output.holderFName;
		outFile << output.zone;
		outFile << someTotal;
	}
	outFile.close();
}
What is the error/problem?
The output file is coming up blank and I dont know if its because of my code or not.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void outputData (TicketHolder audience[], int numPeople, double someTotal)
{
  TicketHolder output;
  ofstream outFile;
  string outputFile;
  cout << " What do you want the output file to be called? " << endl;
  cin >> outputFile;
  outFile.open (outputFile);
  for (int i = 0; i < numPeople; i++)
  {
    outFile << output.holderLName;
    outFile << output.holderFName;
    outFile << output.zone;
    outFile << someTotal;
  }
  outFile.close ();
}


The problem is that the data is in the array audience, but you write the content from output to the file.
Last edited on
See if this helps?

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
#include<iostream>
#include <iomanip>
#include <fstream>


// you are not using any of the below. so take them out.
//#include <cstdlib>
//#include<ctime>
//#include<string>
//#include<cstring>
//#include<cmath>

using namespace std;
const double ZONE_A = 75.00;
const double ZONE_B = 55.00;
const double ZONE_C = 30.00;
const int SEATS = 100;
struct TicketHolder {
    string holderFName;
    string holderLName;
    char zone;
    double charge;
};
int loadData(TicketHolder audience[]);
void addCharge(TicketHolder audience[], int numPeople);
double totalCharge(TicketHolder audience[], int numPeople);
void outputData(TicketHolder audience[], int numPeople, double someTotal);

int main() {
    int numPeople = 0;
    double someCharge = 0;
    TicketHolder audience[SEATS];
    numPeople = loadData (audience);
    addCharge (audience, numPeople);
    someCharge = totalCharge (audience, numPeople);
    outputData (audience, numPeople, someCharge);

    // do not do this it is a windows thing and it does not work on all comptuers.
    // see the top sticky thread of this forum for more info.
    //system("pause");
    return 0;
}

int loadData(TicketHolder audience[]) {
    TicketHolder inputStruct;
    string inputFile;
    ifstream inFile;
    int i = 0;
    cout << "Please enter the file name of the audience names and their zones " << endl;
    cin >> inputFile;
    inFile.open (inputFile);
    if (inFile) {

        // we have a file

        while (inFile.peek () != EOF) {
            inFile >> inputStruct.holderFName;
            cin.ignore ();
            inFile >> inputStruct.holderLName;
            cin.ignore ();
            inFile >> inputStruct.zone;
            cin.ignore ();
            audience[i] = inputStruct;
            i++;
            if (i == 99) {
                cout << " Out of Seats " << endl;
                break;
            }
        }
// you are returning before you close your file.
//    return i;
//    inFile.close();

        // do this instead
        inFile.close ();
        return i;

    } else {
        // we do not have a file
        cout << "No file found, check your inputs!" << endl;
    }

}

void addCharge(TicketHolder audience[], int numPeople) {
    //TicketHolder audience;
    for (int i = 0; i < numPeople; i++) {
        if (audience[i].zone == 'A')
            audience[i].charge = ZONE_A;
        else if (audience[i].zone == 'B')
            audience[i].charge = ZONE_B;
        else if (audience[i].zone == 'C')
            audience[i].charge = ZONE_C;
    }
}

double totalCharge(TicketHolder audience[], int numPeople) {
    double total = 0;
    for (int i = 0; i < numPeople; i++) {
        total += audience[i].charge;
    }
    return total;
}

void outputData(TicketHolder audience[], int numPeople, double someTotal) {
    TicketHolder output;
    ofstream outFile;
    string outputFile;
    cout << " What do you want the output file to be called? " << endl;
    cin >> outputFile;
    outFile.open (outputFile);
    for (int i = 0; i < numPeople; i++) {
        outFile << output.holderLName;
        outFile << output.holderFName;
        outFile << output.zone;
        outFile << someTotal;
    }
    outFile.close ();
}
Last edited on
It didnt work
closed account (48T7M4Gy)
Try it with these revised functions.

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
int loadData(TicketHolder audience[])
{
    string fName, lName;
    char zone;
    int i = 0; // Number of people registered
    
    string inputFile;
    
    cout << "Please enter the file name of the audience names and their zones " << endl;
    cin >> inputFile;

    ifstream inFile(inputFile);
    if (inFile.is_open())
    {
        while (inFile >> fName >> lName >> zone)
        {
            audience[i].holderFName = fName;
            audience[i].holderLName = lName;
            audience[i].zone = zone;
            i++;
        }
    }
    else
        cout << "Unable to open file.\n";
    
    inFile.close();
    return i;
}


and this one also
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void outputData(TicketHolder audience[], int numPeople, double someTotal)
{
    ofstream outFile;
    string outputFile;
    cout << " What do you want the output file to be called? " << endl;
    cin >> outputFile;
    
    outFile.open(outputFile);
    for (int i = 0; i < numPeople; i++)
    {
        outFile << audience[i].holderLName << " * ";
        outFile << audience[i].holderFName << " * ";
        outFile << audience[i].zone << endl;
        outFile << audience[i].charge << endl;
    }
   // Include total charge here - calculate it here instead of separate function??
    outFile.close();
}


Smith * John * A
75
Jones * Sue * C
30
Adams * Ray * B
55
Last edited on
It worked when I replaced outFile << output.holderLName; with outFile << audience[i].holderLName; though you need to improve the formatting
Also you should get rid of cin.ignore(); in the while loop of your loadData function.
Topic archived. No new replies allowed.