Can't get program to fout with ofstream!

I need this program to read the output into a .txt but for what ever reason I cannot get it to do so. Can anyone take a look a this and help me figure out the solution?

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
  // Michael Matteis


#include <string>
#include <fstream> // fin and fout
#include <sstream>
#include <cmath> // Using mathematics
#include <iomanip> // Setprecision
using namespace std;

//////////////////////////////////////////////////////////////  //Here I indenitfied the different variables I will be using in specificity of what it is I want each of them to do or be interpreted as by the compiler

int appNum = 0; // Applicant numbers named as intiger
int totalApplicants, totalMusicAccept, totalLibaccept; // Total scores of each named intiger
int LibaccptNum = 0;
int MusicaccptNum = 0;
string Accept, txt, scores;
char EOL = '\n';
string School, Music, la, alumnus, reason;
ifstream inputfile;
ofstream mp2output;
float GPA, math, verb;
bool almn, BoolAccpt;
string strAccpt, printSchool;



/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main() {
	
	// Here I use a while statement followed by multiple if statements to ensure that each response is returned and either read as true or false in regards to this assignments specific required catagories for each school and applicant

	ifstream inputfile("source.txt"); // This allows the compiler to use the inputs in my TXT file.
	ofstream fout ("mp2output.txt");

	fout << "Acceptance to College by Michael Matteis" << '\n' << '\n';

	while (!inputfile.eof()) //using a while statement 
	{

		appNum++;
		inputfile >> School >> GPA >> math >> verb >> alumnus;
		//getAppData(School, GPA, math, verb, alumnus); 


		if ((School == "L") && (LibaccptNum >= 5)) // School name and max applicants
		{
			BoolAccpt = false; reason = " Admissions full, Maximum applicants admitted";
		}
		else if ((School == "L") && (alumnus == "Y") && (math + verb < 1000))
		{
			BoolAccpt = false; reason = " Rejected- SAT scores are too low";
		}
		else if ((School == "L") && (alumnus == "N") && (math + verb < 1200))
		{
			BoolAccpt = false; reason = "Rejected - SAT scores are too low";
		}
		else if ((School == "L") && (alumnus == "N") && (GPA < 3.5))
		{
			BoolAccpt = false;
			reason = " Rejected - GPA is too low";
		}
		else if ((School == "L") && (alumnus == "Y") && (GPA < 3.0))
		{
			BoolAccpt = false;
			reason = " Rejected - GPA is too low";
		}
		else if ((School == "L") && (BoolAccpt != true))
		{
			strAccpt = " Rejected ";
		}

		if ((School == "L") && (alumnus == "Y") && (GPA >= 3.0) && ((math + verb) >= 1000) && (LibaccptNum < 5))
		{
			BoolAccpt = true;
			strAccpt = "Congratulations you have been accepted into Liberal Arts! ";
			LibaccptNum++;
		}
		if ((School == "L") && (alumnus == "N") && (GPA >= 3.5) && ((math + verb) >= 1200) && (LibaccptNum < 5))
		{
			BoolAccpt = true; strAccpt = " Congratulations you have been accepted into Liberal Arts! ";
			LibaccptNum++;
		}


		if ((School == "M") && (MusicaccptNum < 3) && (math >= 500 && verb >= 500)) // School name, max applicants, and math and verb scores.
		{
			BoolAccpt = true; strAccpt = " Congratulations you have been accepted into Music!";
			MusicaccptNum++;
		}


		if ((School == "M") && ((math < 500) || (verb < 500) || (MusicaccptNum >= 3)))
		{
			BoolAccpt = false;
			if ((School == "M") && (BoolAccpt != true) && (math < 500) || (verb < 500))
			{
				strAccpt = "Rejected - SAT scores are too low ";
			}
			if ((School == "M") && (BoolAccpt != true) && MusicaccptNum >= 3)
				BoolAccpt = false;
			{
				strAccpt = " Unfortunately admissions are full at this time";
			}
		}

		if (School == "L") printSchool = "***Applying to Liberal Arts";
		if (School == "M") printSchool = "***Applying to School of Music";

		////////////////////////////////////////////////////////////////////////////////////////////////// 
		// the following statements will read/output the data that was inputed by I or the user after it has ran through the if statements to provide the correct format expected of this assignment

		fout << "Applicant #: " << appNum << '\n';
		fout << "School = " << School << ' ';
		fout << fixed << setprecision(1);
		fout << "GPA = " << GPA << setprecision(0);
		fout << " Math = " << math << " Verbal = " << verb << " Alumnus = " << alumnus << '\n';
		fout << printSchool << '\n' << strAccpt << reason << '\n' << "*******************************" << '\n' << endl;

		reason.clear();
		strAccpt.clear();
	}

	///////////////////////////////////////////////////////////////////////////////////////
	//Below will read/output the final totals including; applicants, those accepted into Music or Lberal Arts.

	fout << '\n' << '\n' << "Total Applicants: " << appNum << '\n';
	fout << "Total accepted into music: " << MusicaccptNum << '\n';
	fout << "Total accepted into Liberal Arts: " << LibaccptNum << '\n' << '\n';

	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////////




Why all the global variables?

You only have one function so make all of those variables local to that function.

Are you sure your input file is opening correctly?

You should really test that the files open correctly, this is one of the biggest sources of errors when dealing with files.

I need this program to read the output into a .txt but for what ever reason I cannot get it to do so.
You need to be a bit more specific. What happens - compiler error , crash, wrong input, no input....?
Before you actually try to use a stream you should check that it is in a valid state.
1
2
3
4
5
6
ifstream inputfile("source.txt");
if (!inputfile)
{
  // handle error
  return -1;
}

Same for the output stream.
Some issues:

Line 33-34: You don't test if the open of your input or output files was successful. Let's say the open of you input file failed. You continue with your program as if the file were there.

Line 38: Do not loop on (! stream.eof()) or (stream.good()). This does not work the way you expect. The eof bit is set true only after you make a read attempt on the file. This means after you read the last record of the file, eof is still false. Your attempt to read past the last record sets eof, but you're not checking it there. You proceed as if you had read a good record. This will result in reading an extra (bad) record. The correct way to deal with this is to put the >> (or getline) operation as the condition in the while statement.
1
2
3
  while (stream >> var) // or while (getline(stream,var))
  {  //  Good operation
  }


Line 42: You don't show the format of your input file, so it's impossible to determine if this read is correct.

Line 42: You don't test if your read was successful. If it wasn''t successful, you continue as if nothing had happened.

I did not check your if/else logic to see if every permutation was covered.
Last edited on
source.txt:
L 4.0 600 650 N
M 3.9 610 520 N
L 3.8 590 600 N
L 3.0 600 600 Y
L 3.4 600 600 N
L 3.0 500 490 Y
L 2.9 500 500 Y
M 3.5 500 490 Y
M 3.9 490 600 Y
L 3.5 700 500 N
L 3.1 600 400 Y
L 3.0 490 510 Y
L 4.0 800 800 Y
M 3.2 500 500 N


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
#include <cmath> // Using mathematics
#include <fstream> // fin and fout
#include <iostream> // <-- missing header
#include <iomanip> // Setprecision
#include <sstream>
#include <string>

bool manageLiberalArt(double gpa, int math, int verb, char alumn, 
                        int already_in, std::string& reason);
bool manageMusic(int math, int verb, int already_in, std::string& reason);

int main() {
    
    // Here I use a while statement followed by multiple if statements to 
    // ensure that each response is returned and either read as true or false 
    // in regards to this assignments specific required categories for each 
    // school and applicant

    std::ifstream inputfile("source.txt");
    if(!inputfile) {
        std::cout << "Can't open file 'source.txt'.\n Exiting now.\n";
        std::exit(1);
    }
    std::ofstream fout("mp2output.txt");
    if(!fout) {
        std::cout << "Can't create file 'mp2output.txt'.\n Exiting now.\n";
        std::exit(1);
    }

    fout << "Acceptance to College by Michael Matteis\n\n";
    // Loop: a) read a line of text inside a std::string; get rid of '\n'
    //          (to perform such a task, std::getline() is recommended);
    //       b) create a std::istringstream from that std::string;
    //       c) use that std::istringstream to initialize my variables.
    int app_num {1},
        lib_art_accept {},
        music_accept {};
    for(std::string line; std::getline(inputfile, line); ++app_num) { // will loop until EOF
        std::istringstream ss(line);
        // Let's assume source.txt is done this way:
        //  school       gpa       math    verb    alumn
        //    L          4.0       600     650       N
        // (1 char)    (double)   (int)   (int)   (char)
        char school {},
             alumn {};
        double gpa {};
        int math {},
            verb {};
        ss >> school >> gpa >> math >> verb >> alumn;
        std::string reason;
        switch(school) { // switch is usually easier to read
        case 'L': {
            if(manageLiberalArt(gpa, math, verb, alumn, lib_art_accept, reason)) {
                lib_art_accept++;
            }
            break;
        }
        case 'M': {
            if(manageMusic(math, verb, music_accept, reason)) {
                music_accept++;
            }
            break;
        }
        default:
            std::cout << "Error while reading file.\n Exiting now.\n";
            exit(1);
            break;
        }

        std::string print_school;
        if (school == 'L') print_school = "***Applying to Liberal Arts";
        if (school == 'M') print_school = "***Applying to School of Music";

        // ===================================================================
        // the following statements will read/output the data that was inputed 
        // by I or the user after it has ran through the if statements to 
        // provide the correct format expected of this assignment
        fout << "Applicant #: " << app_num
             << std::fixed << std::setprecision(1) 
             << " GPA = " << gpa 
             << " Math = " << math 
             << " Verbal = " << verb 
             << " Alumnus = " << alumn
             << '\n' << print_school 
             << '\n' << reason << '\n';
    }

    // =======================================================================
    // Below will read/output the final totals including; applicants, those 
    // accepted into Music or Lberal Arts.
    fout << "\n\nTotal Applicants: " << app_num
         << "\nTotal accepted into music: " << lib_art_accept
         << "\nTotal accepted into Liberal Arts: " << music_accept << "\n\n";

    return 0;
}

bool manageLiberalArt(double gpa, int math, int verb, char alumn, 
                        int already_in, std::string& reason)
{
    if(already_in > 4) {
        reason = "Sorry, no more applicants accepted.\n";
        return false;
    }
    
    if(alumn == 'Y') {
        if(math + verb < 1000) {
            reason = "Rejected - SAT scores are too low.\n";
            return false;
        }

        if(gpa < 3.0) {
            reason = "Rejected - GPA is too low.\n";
            return false;
        }
    }

    if(alumn == 'N') {
        if(math + verb < 1200) {
            reason = "Rejected - SAT scores are too low.\n";
            return false;
        }

        if(gpa < 3.5) {
            reason = "Rejected - GPA is too low.\n";
            return false;
        }
    }

    reason = "Congratulations you have been accepted into Liberal Arts!\n";
    return true;
}

bool manageMusic(int math, int verb, int already_in, std::string& reason)
{
    if(already_in > 2) {
        reason = "Unfortunately admissions are full at this time";
        return false;
    }
    
    if(math < 500 || verb < 500) {
        reason = "Rejected - SAT scores are too low.\n";
        return false;
    }
    
    reason = "Congratulations you have been accepted into Music!\n";
    return true;
}


mp2output.txt:
Acceptance to College by Michael Matteis

Applicant #: 1 GPA = 4.0 Math = 600 Verbal = 650 Alumnus = N
***Applying to Liberal Arts
Congratulations you have been accepted into Liberal Arts!

Applicant #: 2 GPA = 3.9 Math = 610 Verbal = 520 Alumnus = N
***Applying to School of Music
Congratulations you have been accepted into Music!

Applicant #: 3 GPA = 3.8 Math = 590 Verbal = 600 Alumnus = N
***Applying to Liberal Arts
Rejected - SAT scores are too low.

Applicant #: 4 GPA = 3.0 Math = 600 Verbal = 600 Alumnus = Y
***Applying to Liberal Arts
Congratulations you have been accepted into Liberal Arts!

Applicant #: 5 GPA = 3.4 Math = 600 Verbal = 600 Alumnus = N
***Applying to Liberal Arts
Rejected - GPA is too low.

Applicant #: 6 GPA = 3.0 Math = 500 Verbal = 490 Alumnus = Y
***Applying to Liberal Arts
Rejected - SAT scores are too low.

Applicant #: 7 GPA = 2.9 Math = 500 Verbal = 500 Alumnus = Y
***Applying to Liberal Arts
Rejected - GPA is too low.

Applicant #: 8 GPA = 3.5 Math = 500 Verbal = 490 Alumnus = Y
***Applying to School of Music
Rejected - SAT scores are too low.

Applicant #: 9 GPA = 3.9 Math = 490 Verbal = 600 Alumnus = Y
***Applying to School of Music
Rejected - SAT scores are too low.

Applicant #: 10 GPA = 3.5 Math = 700 Verbal = 500 Alumnus = N
***Applying to Liberal Arts
Congratulations you have been accepted into Liberal Arts!

Applicant #: 11 GPA = 3.1 Math = 600 Verbal = 400 Alumnus = Y
***Applying to Liberal Arts
Congratulations you have been accepted into Liberal Arts!

Applicant #: 12 GPA = 3.0 Math = 490 Verbal = 510 Alumnus = Y
***Applying to Liberal Arts
Congratulations you have been accepted into Liberal Arts!

Applicant #: 13 GPA = 4.0 Math = 800 Verbal = 800 Alumnus = Y
***Applying to Liberal Arts
Sorry, no more applicants accepted.

Applicant #: 14 GPA = 3.2 Math = 500 Verbal = 500 Alumnus = N
***Applying to School of Music
Congratulations you have been accepted into Music!



Total Applicants: 15
Total accepted into music: 5
Total accepted into Liberal Arts: 2


Topic archived. No new replies allowed.