Won't output to file

Input to be read from file. Main to call functions. Program compiles and runs, but not correctly. Output file opens but nothing written to it.


Sample of input file:
Duckey Donald 85
Goof Goofy 89
Brave Balto 93
Snow Smitn 93
Alice Wonderful 89
Samina Akthar 85
Simba Green 95
Donald Egger 90
Brown Deer 86
Johny Jackson 95
Greg Gupta 75

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
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;



struct studentType
{
string studentFName, studentLName;
char grade;
int testScore;
};


int main ()
{

int n;
ifstream inputFile;
ofstream outputFile;
inputFile.open( "Ch9_Ex2Data.txt");
if (!inputFile.eof())
{
outputFile.open( "Ch9_Ex2Out.txt");  
    
studentType students[20];
int readData(ifstream& inputFile, studentType students[]);
void getGrade(studentType students[], int n);
void highestScore(ofstream& outputFile, studentType students[], int n);
void displayData(ofstream& outputFile, studentType students[], int n);



}   
system("pause");

return 0;
}

int readData(ifstream& inputFile, studentType students[])
{
	int n = 0;
	inputFile >> students[n].studentFName >> students[n].studentLName >> students[n].testScore;
		while (inputFile)
			{
				n++;
				inputFile >> students[n].studentFName >> students[n].studentLName >> students[n].testScore;
			}
return n;
}
		
void getGrade(studentType students[],  int listSize)
{
	int score;
	char grade;
	 if (score >= 90) 
	 	grade = 'A';
	 else if (score >= 80)
	 	grade = 'B';
	 else if (score >= 70)
	 	grade = 'C';
	 else if (score >= 60)
	 	grade = 'D';
	 else
	 	grade = 'F';
}
void highestScore(ofstream& out, studentType students[], int n)	
{
	int max = 0, i;
		for (i = 1; i < n; i++)
			{
			if (students[i].testScore > students[max].testScore)
				out << "Higest test score: " << students[max].testScore << endl;
				out << "Student names having the highest test score: " << endl;
			}		
				for (i = 0; i < n; i++)
					{
					if (students[i].testScore == students[max].testScore)
					out << students[i].studentLName << "," << students[i].studentFName << endl;
					}


}
void displayData(ofstream& out, studentType students[], int n) 
{
	for (int i = 0; i < n; i++)
	{
		out << left << students[i].studentLName << "," << students[i].studentFName << " ";
		out << students[i].testScore << " " << students[i].grade << endl;
	}
}

  
Last edited on
Line 21: If the open fails, it will set the bad bit, not the eof bit.
You can test a unsuccessful open by:
1
2
  if (! inputfile)
    return 1;


Line 29-32: These are function prototypes, not function calls.



updated code. the functions are the same as originally posted. when I run it it returns 1, the input file is located in the same file as my project. Please advise. Thanks
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
struct studentType
{
string studentFName, studentLName;
char grade;
int testScore;
};

int readData(ifstream& inputFile, studentType students[]);
void getGrade(studentType students[], int n);
void highestScore(ofstream& outputFile, studentType students[], int n);
void displayData(ofstream& outputFile, studentType students[], int n);

int main ()
{

int n;
ifstream inputFile;
ofstream outputFile;
inputFile.open( "Ch9_Ex2Data.txt");
 if (! inputFile)
    return 1;


    
studentType students[20];
readData (inputFile, students);
getGrade (students, n);
highestScore (outputFile, students, n);
displayData (outputFile, students, n);

outputFile.open( "Ch9_Ex2Out.txt");  
  
system("pause");

return 0;
}
Last edited on
The link below shows about input/output with files:
http://www.cplusplus.com/doc/tutorial/files/
https://en.wikipedia.org/wiki/Indentation_style
Your code would be a lot easier to follow if you applied a consistent indentation style.
Extraction operator stops at whitespace and is left there. Try this to eat up the newline char.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
     inputFile >> students[n].studentFName;
     inputFile.get();
     inputFile >> students[n].studentLName;
     inputFile.get();
     inputFile >> students[n].testScore;
     inputFile.get();
          
          while (inputFile)
		{
			n++;
			 inputFile >> students[n].studentFName;
                         inputFile.get();
                         inputFile >> students[n].studentLName;
                         inputFile.get();
                         inputFile >> students[n].testScore;
                         inputFile.get();
		}
Last edited on
Thanks for your time in helping with this assignment. I have changed the extraction operator, but still not writing to the file?? any thoughts? I am still confused as to why it returns 1 ?
I misread your post. I thought you were reading in from file. You can disregard what I said completely.
Last edited on
Several issues. You haven't checked to see if your inputFile is actually open or not. You didn't call any function that would actually do anything. Inside of int main(), you declare variables, open files, and make function prototypes, then you literally exit the program.

To call the functions you made, put them ABOVE int main(), otherwise they can't be called from within int main().

Good luck!
zapshe, please see my second post, as this is what I was trying to do there. If that is not correct can you please advise as to how I can correct this? Your time is greatly appreciated
There's a number of issues in your code.

Please compare it to the one above, which is more or less yours + comments. It could help you improving its first part.

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


struct StudentType
{
    std::string first_name;
    std::string last_name;
    char grade;
    int test_score;
    // If you don't initialize 'grade' and 'test_score', their initial values
    // are unknown (= garbage).
};


int readData(std::ifstream& input_file, StudentType* students, std::size_t size);
void getGrade(StudentType students[], int list_size);
void highestScore(std::ofstream& output_file, StudentType students[], int n);
void displayData(std::ofstream& output_file, StudentType students[], int n);


int main ()
{
    std::ifstream input_file( "Ch9_Ex2Data.txt");
    if (! input_file) {
        std::cout << "Cannot open Ch9_Ex2Data.txt.\nExiting now.\n";
        return 1;
    }

    constexpr int Students_Max { 20 };      // just to avid magic numbers

    StudentType students[Students_Max];     // use std::vector instead!!
    // students[] is uninitialized here!
    // It means *all* the 'grades' and the 'test_score' might contain garbage
    // values.
    
    // You are neglecting the function return value!
    // This will cause several errors, since you don't need to analyze the
    // entire C-style array "students[]", but only the part you have assigned
    // values to.
    // Remember: the part you have neither initialized nor assigned might
    // contain garbage values.
    readData (input_file, students, Students_Max);
    
    // Neither inilialized nor assigned any good value; see previous comment:
    int n;      // bad name! Do use a meaningful name
    
    // getGrade will take into consideration an unknown number of elements,
    // since we cannot now what's there in 'n':
    getGrade (students, n);

    std::ofstream output_file( "Ch9_Ex2Out.txt");
    // You're NOT checking if 'output_file' has been properly opened!!


    // Let's check this part later...
    // But read the comments inside the functions body.
    highestScore (output_file, students, n);
    displayData (output_file, students, n);

    system("pause");

    return 0;
}


// Data example:
// Duckey Donald 85
// Goof Goofy 89
// Brave Balto 93
// Snow Smitn 93
// Alice Wonderful 89
// Samina Akthar 85
// Simba Green 95
// Donald Egger 90
// Brown Deer 86
// Johny Jackson 95
// Greg Gupta 75
int readData(std::ifstream& input_file, StudentType* students, std::size_t size)
{
    for (int n = 0; n < size && input_file; ++n)
    {
        input_file >> students[n].first_name
                   >> students[n].last_name
                   >> students[n].test_score;
    }
    return n;
}


// What should this funtion do?
// Shouldn't it loop through the C-style array and assign values to ... ?
// Re-read it: as it is now, it does pretty nothing:
void getGrade(StudentType students[],  int list_size)
{
    int score;          // uninitialised local variable
    char grade;         // uninitialized local variable
    if (score >= 90) {
        grade = 'A';
    } else if (score >= 80) {
        grade = 'B';
    } else if (score >= 70) {
        grade = 'C';
    } else if (score >= 60) {
        grade = 'D';
    } else {
        grade = 'F';
    }
}


//             Bad parameter name: give it a meaningful name-------------\/
void highestScore(std::ofstream& output_file, StudentType students[], int n)
{
    int max = 0;
    for (int i = 1; i < n; ++i)
    {
        if (students[i].test_score > students[max].test_score) {
            output_file << "Higest test score: "
                        << students[max].test_score << '\n';
        }
        
        // Are your sure this part should be inside the for loop?
        output_file << "Student names having the highest test score: " << '\n';
        // Aren't you missing something here? ------------------------^
        
        // Aren't you going to do anything with the 'max' value?
    }
    
    for (i = 0; i < n; ++i)
    {
        if (students[i].test_score == students[max].test_score) {
            output_file << students[i].last_name << ", "
                        << students[i].first_name << '\n';
        }
    }
}


//             Bad parameter name: give it a meaningful name-------------\/
void displayData(std::ofstream& output_file, StudentType students[], int n)
{
    for (int i = 0; i < n; i++)
    {
        output_file << left << students[i].last_name << ","
                    << students[i].first_name << ' '
                    << students[i].test_score << ' '
                    << students[i].grade << '\n';
    }
}

Thanks for the input! This gets me going in the right direction
Topic archived. No new replies allowed.