Greetings & Peculiar input data problem.

Hello, I'm new to the forum my name is _Lfred.
I am here to ask for an opinion on a problem from a more advanced coder then I am.

I will tell you what the code is intended to do and what is the problem that I am having. I will post the code.

this code is to read input from two separate data files which are constructed as follows.

data1.txt
1
2
3
4
5
6
7
8
9

Doe, John	1000001	500	270.89
Brady, Laura	1000002	650	598.84
Sidney, Jacob	1000003	450	200.30
Fishburn, Lary	1000004	900	340.40
Jackson, Samuel	1000005	500	490.00
Sanders, Bob	1000006	600	455.22
Grey, Anthony	1000007	500	459.99
McIrish, Steve	1000008	1000	630.12


and data2.txt
1
2
3
4
5
6
7
8
Doe, John	1000031	1000	270.89
Brady, Laura	1056002	1000	598.84
Sidney, Jacob	1000003	1000	200.30
Fishburn, Lou	1005504	1000	340.40
Jackson, Samuel	1034005	1000	490.00
George, Max	1043455	1000	980.55
Stan, 	Lee	1055334	1000	33.90


The program is to read these files into arrays of data structures and sort them according to the percent of resources used. Then it is to output the sorted list of structured data to an output file. Append data from the two input files to one output file.

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
175
176
177
178
179
180
181
182
/*
Alfred J. Denigris
CSC 211 Lab 4
March 19, 2009
*/
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
#include <cmath>
#include <string>

using namespace std;
const int DATAMAX = 20;

// Data Structyre Types
//---------------------
struct nameType
{
	string first;
	string last;
};//end nameType

struct resType
{
	int limit;
	double used;
	double percentUsed;
};//end resType

struct userType
{ 
	nameType name;
	int userID;
	resType resource;
	char flag;
};//end userType

//Prototype Functions
//-------------------
void chooseInputFile(ifstream& inFile, char fileName[]);
void chooseOutputFile(ofstream& outFile);
void getTheDataIntoTheStruct(userType user[],ifstream& inFile, int& arraysize);
double calcPercentUsed(int limit,double used);
void decendingOrderSort(userType list[], int arraysize);
void output(ofstream& outFile, userType user[], int arraysize, char filename[]);

// Main Program
//-------------
void main()
{
	//Local variables
	//---------------
	userType user[DATAMAX];// declare structured data array
	ifstream inFile;
	ofstream outFile;
	int arraysize=0;
	char fileName[50];
	bool again=true;
	char ch1;

	while (again)
	{	char fileName[50];
		
		//Start
		chooseInputFile(inFile, fileName);// choose input file
		chooseOutputFile(outFile);// choose output file
		
		getTheDataIntoTheStruct(user,inFile,arraysize);//Get Data


		decendingOrderSort(user, arraysize);	// Sort data
	
		output(outFile,user,arraysize,fileName);	// Output data
	
		inFile.close();
		outFile.close();
		
		cout <<"Do you want to add anther file? y or n ";
		cin >> ch1;
		if (ch1 == 'y' || ch1 =='Y')
			again = true;
		else
			again= false;
	}
}// End of Main Program
//---------------------


// Functions
//--------------
void chooseInputFile(ifstream& inFile, char fileName[]) 	//Function to choose a file to open
{
	cout <<"Which file would you like to read data from? \n:";
	cin >> fileName;
	
	inFile.open(fileName);
	cout <<fileName<< " has been opened for intput\n";

}//end chooseInputFile

void chooseOutputFile(ofstream& outFile)	//Function to choose a file to open
{
	char fileName[50];
	cout <<"Which file would you like to write output data to?? \n:";
	cin >> fileName;
	
	outFile.open(fileName, ios::app);
	cout <<fileName<< " has been opened for output.\n";
}//end chooseInputFile

void getTheDataIntoTheStruct(userType user[],ifstream& inFile, int& arraysize) // Fills Up a Struct
{	
	for (int i=0; !inFile.eof(); i++)
	{
		inFile >> user[i].name.last;		// get name.last
		inFile >> user[i].name.first;		// get name.first
		inFile >> user[i].userID;			// get userID
		inFile >> user[i].resource.limit;	// get resource.limit
		inFile >> user[i].resource.used;	// get resourece.used
		
		user[i].resource.percentUsed = calcPercentUsed(user[i].resource.limit,	user[i].resource.used);
		// calculate resource.percentUsed
		
		(user[i].resource.percentUsed >= 90) ?user[i].flag='*' :user[i].flag=' ';
		// If %used is > 90, put * in flag 
		
		arraysize++;// Keep track of how many users have been input	
	}
}//end getTheDataIntoTheStruct

double calcPercentUsed(int limit,double used) //calculate Percent Used, Return Double percentUsed
{
	return static_cast<double>(used/limit)*100;
}

// selection sort function
void decendingOrderSort(userType list[], int arraysize)
{
	int length = arraysize;
	int i, icopy, i2;
	userType temp;

	//cout << "length = "<<length<<endl;

	for (i = 0; i <length -1; i++)
	{
		icopy = i;
		
		for (i2= i+1; i2 <length; i2++)
		{
			if (list[icopy].resource.percentUsed < list[i2].resource.percentUsed)
				icopy = i2; // make sure that icopy index points to the largest value
		}	
		temp = list[icopy];
		list[icopy] = list[i];
		list[i] = temp;// switch index with largest	
	}
}

void output(ofstream& outFile, userType user[], int arraysize, char fileName[])
{
	outFile <<"\nHere is the Data Report for the file\n"<<fileName;
	outFile<<endl;

	outFile <<setw(25)<<left<<"Name (First, Last)"<<setw(15)<<left<<"Student ID#"<<setw(15)<<left<<"Res. Limit"<<setw(10)<<left<<"Res. Used"<<setw(10)<<left<<"% Used" <<setw(10)<<left<<"Over 90%?"<<endl;
	for (int i=0; i<85; i++)
		outFile <<'_';
	outFile<<endl;
	for ( i=0; i<arraysize; i++)
	{
		outFile <<setw(13)<<left<< user[i].name.last;		// get name.last
		outFile <<setw(12)<<left<< user[i].name.first;		// get name.first
		outFile <<setw(15)<<left<< user[i].userID;			// get userID
		outFile <<setw(15)<<left<< user[i].resource.limit;	// get resource.limit
		outFile <<setw(10)<<left<< user[i].resource.used;	// get resourece.used
		outFile <<setw(10)<<left<<setprecision(2)<<fixed<<showpoint<< user[i].resource.percentUsed;
		outFile <<setw(10)<<left<< user[i].flag;
		outFile <<endl;	
	}//for(int i=0, i<DATAMAX; i++)
	cout <<endl;
}



When I compile my source code, and choose the appropriate input files through the prompt. Everything seems to run smoothe.

The problem is that the output file repeats the data from the first structure array. It seems to me as if the data is never input into the array the second time.

If someone can compile this code and help me to understand why I'm having this problem, I would very much appreciate the assist. thank you =]
-_Lfred
Last edited on
im not able to understand the problem actually.. if you can give an example from your two input files..
what output you get when you run the two files above, and what is expected??
where are you making your arraysize to zero. in the first run it is zero before the while loop, but next time onwards it never made zero and hence the second output has (2 * arraysize) number of lines. check it!!
Thank you, I noticed that too while looking over this program and Changed it to :
1
2
3
4
5
...
	while (again)
	{
		int arraysize=0;
...


a sample of the output now looks this way. I am still having an issue with this but it is a diffrent one. I will still keep trying to understand the issue.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Here is the Data Report for the file
data1.txt
Name (First, Last)       Student ID#    Res. Limit     Res. Used % Used    Over 90%? 
_____________________________________________________________________________________
Jackson,     Samuel      1000005        500            490       98.00     *         
Brady,       Laura       1000002        650            598.84    92.13     *         
Grey,        Anthony     1000007        500            459.99    92.00     *         
Sanders,     Bob         1000006        600            455.22    75.87               
McIrish,     Steve       1000008        1000           630.12    63.01               
Doe,         John        1000001        500            270.89    54.18               
Sidney,      Jacob       1000003        450            200.30    44.51               
Fishburn,    Lary        1000004        900            340.40    37.82               

Here is the Data Report for the file
data2.txt
Name (First, Last)       Student ID#    Res. Limit     Res. Used % Used    Over 90%? 
_____________________________________________________________________________________
Topic archived. No new replies allowed.