Code won't write to file

Jul 9, 2015 at 10:58pm
Can someone give me an idea of why my code won't write to the file SORTED.TXT? It successfully creates the file, but doesn't write the data from the arrays.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void writeData(string array1[MAX_NUM], int array2[MAX_NUM], int count1)
{
	ofstream sortData("SORTED.TXT");
	
	sortData.open(FILE_SORT.c_str());
	
	while(sortData)
	{
		for(int i = 0; i < count1; i++)
		{
			sortData << array1[i] << array2[i];
		}
	}
	
	sortData.close();
}
Jul 9, 2015 at 11:06pm
What does the line 5 do?
Jul 9, 2015 at 11:22pm
Thank you, that fixed part of it haha. This is what I have now. When I run it, the txt file contains... I have a major problem somewhere else in the program.

DA837498
0
0
0
0
0

The DA837498 is correct, but beyond that it is wrong.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void writeData(string array1[MAX_NUM], int array2[MAX_NUM], int count1)
{
	ofstream sortData("SORTED.TXT");
	
	if (sortData.is_open())
	{
		for(int i = 0; i < count1; i++)
		{
			sortData << array1[i] << " " << array2[i] << endl;
		}
	}
	else
	{
		cout << "Could not open file." << endl;
	}
	
	sortData.close();
}
Last edited on Jul 9, 2015 at 11:22pm
Jul 10, 2015 at 12:00am
What happens if you output the array values? It almost seems like the arrays are smaller than the count value.
Jul 10, 2015 at 12:12am
array1 outputs DA837498 and thats all. array2 outputs
0
0
0
0
0
0

This is the code I have so far, can you tell where I'm going wrong?

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

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cctype>
using namespace std;

enum level {FRESHMAN, SOPHOMORE, JUNIOR, SENIOR};
const string STUD_REC = "CREDITS.TXT";
const string FILE_SORT = "SORTED.TXT"; 
const int MAX_NUM = 999;

void ifUnder(int hrs, string idNum, string idArray1[MAX_NUM], int classLevel1[MAX_NUM]); //if undergraduate, this will fill the arrays with the data
// and apply the enumerated value to it.

void sortArrays(string array1[MAX_NUM], int array2[MAX_NUM], int count1);
																						

int sortData(string idArray[MAX_NUM], int classLevel[MAX_NUM]);//goes through data and calls the ifUnder function if necessary

void writeData(string array1[MAX_NUM], int array2[MAX_NUM], int count1);

int main()
{	
	string id[MAX_NUM];
	int claLevel[MAX_NUM];
	int counter;
    
	cout << "This program will convert hours taken to class level for all "
	"Undergraduate students.  You will be able to search for each student by "
	"the student ID number." << endl << endl;
	
	counter = sortData(id, claLevel);
	
	sortArrays(id, claLevel, counter);
	writeData(id, claLevel, counter);	
	
	
	cout << endl;
    return 0;
}

int sortData(string idArray[MAX_NUM], int classLevel[MAX_NUM])
{
	string id;
	char stuType;
	double hours;
	ifstream studData;
	int count = 0;
	
    studData.open (STUD_REC.c_str());      // open the file for reading
    
    if(studData) //If the file opens successfully
	{
		studData >> id >> stuType >> hours; //priming read
		
		while(studData) // while an id was successfully read
		{	
			if((stuType == 'U') || (stuType == 'u'))
			{
				ifUnder(hours, id, idArray, classLevel);
				count++;			
			}
			studData >> id >> stuType >> hours; //attempt to read next line
			
		}
	}
	return count;
}

void ifUnder(int hrs, string idNum, string idArray1[MAX_NUM], int classLevel1[MAX_NUM])
{
	int array1Count = 0;
	int array2Count = 0;
	level idLevel;
	
	if(0 <= hrs < 32)
	{
		idLevel = FRESHMAN;
	}
	else if(32 <= hrs <= 63)
	{
		idLevel = SOPHOMORE;
	}
	else if(64 <= hrs <= 95)
	{
		idLevel = JUNIOR;
	}
	else if(96 <= hrs)
	{
		idLevel = SENIOR;
	}
			
	idArray1[array1Count] = idNum;
	array1Count++;
	
	classLevel1[array2Count] = idLevel;
	array2Count++;
	
}

void sortArrays(string array1[MAX_NUM], int array2[MAX_NUM], int count1)
{
		
	    int i, j, first;
		string temp;
		
      	for (i = count1 - 1; i > 0; i--)
     	{
           first = 0;                 // initialize to subscript of first element
           for (j = 1; j <= i; j++)   // locate smallest between positions 1 and i.
          {
                 if (array1[j] < array1[first])
                 first = j;
          }
         temp = array1[first];   // Swap smallest found with element in position i.
         array1[first] = array1[i];
         array2[first] = array2[i];
         array1[i] = temp;
		}
}

void writeData(string array1[MAX_NUM], int array2[MAX_NUM], int count1)
{
	ofstream sortData("SORTED.TXT");
	
	if (sortData.is_open())
	{
		for(int i = 0; i < count1; i++)
		{
			sortData << array1[i] << " " << array2[i] << endl;
		}
	}
	else
	{
		cout << "Could not open file." << endl;
	}
	
	sortData.close();
}
Jul 10, 2015 at 12:28am
A sample of say 10 items in "CREDITS.TXT" might be useful.
Jul 10, 2015 at 12:36am
Ok

DA849503 U 6
DE938493 U 96
AB059875 G 36
CD940594 A 124
BC493827 U 48
BB584958 G 18
BB382948 U 24
AD668599 U 60
DA837498 U 44
AA736748 G 16

Also, here is an update of what I have. I moved where the arrays were being populated to the same function that reads the 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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cctype>
using namespace std;

enum level {FRESHMAN, SOPHOMORE, JUNIOR, SENIOR};
const string STUD_REC = "CREDITS.TXT";
const string FILE_SORT = "SORTED.TXT"; 
const int MAX_NUM = 999;

void ifUnder(int classLevel1[MAX_NUM], int count1); //if undergraduate, this will fill the arrays with the data
// and apply the enumerated value to it.

void sortArrays(string array1[MAX_NUM], int array2[MAX_NUM], int count1);
																						

int sortData(string idArray[MAX_NUM], int classLevel[MAX_NUM]);//goes through data and calls the ifUnder function if necessary

void writeData(string array1[MAX_NUM], int array2[MAX_NUM], int count1);

int main()
{	
	string id[MAX_NUM];
	int claLevel[MAX_NUM];
	int counter;
    
	cout << "This program will convert hours taken to class level for all "
	"Undergraduate students.  You will be able to search for each student by "
	"the student ID number." << endl << endl;
	
	counter = sortData(id, claLevel);
	
	sortArrays(id, claLevel, counter);
	ifUnder(claLevel, counter);
	writeData(id, claLevel, counter);	
	
	
	cout << endl;
    return 0;
}

int sortData(string idArray[MAX_NUM], int classLevel[MAX_NUM])
{
	string id;
	char stuType;
	double hours;
	ifstream studData;
	int count = 0;
	
    studData.open (STUD_REC.c_str());      // open the file for reading
    
    if(studData) //If the file opens successfully
	{
		studData >> id >> stuType >> hours; //priming read
		
		while(studData) // while an id was successfully read
		{	
			if((stuType == 'U') || (stuType == 'u'))
			{
				idArray[count] = id;
				classLevel[count] = hours;
				count++;	
			}
			studData >> id >> stuType >> hours; //attempt to read next line
			
		}
	}
	return count;
}

void ifUnder(int classLevel1[MAX_NUM], int count1)
{
	level idLevel;
	
	for(int i = 0; i < count1; i++)
	{
		if(0 <= classLevel1[i] < 32)
		{
			idLevel = FRESHMAN;
			classLevel1[i] = idLevel;
		}
		else if(32 <= classLevel1[i] <= 63)
		{
			idLevel = SOPHOMORE;
			classLevel1[i] = idLevel;
		}
		else if(64 <= classLevel1[i] <= 95)
		{
			idLevel = JUNIOR;
			classLevel1[i] = idLevel;
		}
		else if(96 <= classLevel1[i])
		{
			idLevel = SENIOR;
			classLevel1[i] = idLevel;
		}
	}
}

void sortArrays(string array1[MAX_NUM], int array2[MAX_NUM], int count1)
{
		
	    int i, j, first;
		string temp;
		
      	for (i = count1 - 1; i > 0; i--)
     	{
           first = 0;                 // initialize to subscript of first element
           for (j = 1; j <= i; j++)   // locate smallest between positions 1 and i.
          {
                 if (array1[j] < array1[first])
                 first = j;
          }
         temp = array1[first];   // Swap smallest found with element in position i.
         array1[first] = array1[i];
         array2[first] = array2[i];
         array1[i] = temp;
		}
}

void writeData(string array1[MAX_NUM], int array2[MAX_NUM], int count1)
{
	ofstream sortData("SORTED.TXT");
	
	if (sortData.is_open())
	{
		for(int i = 0; i < count1; i++)
		{
			sortData << array1[i] << " " << array2[i] << endl;
		}
	}
	else
	{
		cout << "Could not open file." << endl;
	}
	
	sortData.close();
}
Last edited on Jul 10, 2015 at 12:41am
Jul 10, 2015 at 1:34am
Why does your sort data read in the data?
Jul 10, 2015 at 2:39am
Where is the correct place to read it in?
Jul 10, 2015 at 3:56am
Ok that looks like the data a previous poster was using for working out a id validation routine.
When I run your code I get a warning for these expressions,
if(0 <= classLevel1[i] < 32)
warning: comparisons like 'X<=Y<=X' do not have their mathematical meaning.

I used this sample data in CREDITS.txt,
BC223344 U 10
AB112233 A 134
EE555555 G 18
DE445566 U 122
DA849503 U 6
DE938493 U 96
AB059875 G 36
CD940594 A 124
BC493827 U 48
BB584958 G 18
BB382948 U 24
AD668599 U 60
DA837498 U 44
AA736748 G 16

When I look at the SORTED.txt after running your code I get,
DE938493 0
DE445566 0
DA849503 0
DA837498 0
BC493827 0
BC223344 0
BB382948 0
AD668599 0

So I assume you are sorting the arrays according to the first two characters in the id number?

I would go about it this way printing stuff to make sure it was working.

1. Load CREDITS.txt into arrays
2. Print arrays to see if it is how you expect
3. Sort the arrays.
4. Print sorted arrays to see if it is how you expect
5. Save arrays to SORTED.txt
Last edited on Jul 10, 2015 at 6:41am
Topic archived. No new replies allowed.