Input file is not opening

When I run this code it stops at the if statement and says my input file is not opening on the console. My output file gets created but nothing is being stored there. My input file is in the same directory so I don't know why its not opening.


#include <iostream>
#include <fstream> // library for file input and output
#include <string>
using namespace std;

int main() {

// Variables
double test1, test2, test3, test4, finalexam, percentage;
string name, social, address, telephone;
int age, numberofyears;
char letterGrade;

// declare input file stream varaible and open file
ifstream fin;
fin.open("project2_input.txt");

if (!fin)
{
cout << "Could not open file. Terminating program." << endl;
return -1;
}

// declare output file stream varaible and open file
ofstream fout;
fout.open("project2_output.txt");

if (!fin)
{
cout << "Could not open file. Terminating program." << endl;
return -1;
}

//read data from file
fin >> name;
fin >> age;
fin >> address;
fin >> numberofyears;
fin >> telephone;
fin >> social;
fin >> test1;
fin >> test2;
fin >> test3;
fin >> test4;
fin >> finalexam;

// calculate final semester grade
percentage = (.10 * test1) + (.15 * test2) + (.15 * test3) + (.20 * test4) + (.40 * finalexam);

if (percentage >= 90){
letterGrade = 'A';
cout<<"A"<<endl;}
else if(percentage >= 80){
letterGrade = 'B';
cout<<"B"<<endl;}
else if(percentage >= 70){
letterGrade = 'C';
cout<<"C"<<endl;}
else if(percentage >= 65){
letterGrade = 'D';
cout<<"D"<<endl;}
else{
cout<<"F"<<endl;}


cout << name;
cout << age;
cout << address;
cout << numberofyears;
cout << telephone;
cout << social;
cout << test1;
cout << test1;
cout << test2;
cout << test3;
cout << test4;
cout << finalexam;
cout << percentage;

fout << name;
fout << age;
fout << address;
fout << numberofyears;
fout << telephone;
fout << social;
fout << test1;
fout << test1;
fout << test2;
fout << test3;
fout << test4;
fout << finalexam;
fout << percentage;

// closing files
fin.close();
fout.close();

return 0 ;

}
You can try this. Not sure if it will do your calculations as I don't have the file populated with data that you are using. But I got rid of all the compiler errors. Also when using files, don't open the input and output at the same time. If you are reading from file first, open the file for reading then when you are done, close it. Then you are free to open the file for writing.
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
#include <iostream>
#include <fstream> // library for file input and output
#include <string>
using namespace std;

int main() {

// Variables
double test1, test2, test3, test4, finalexam, percentage;
string name, social, address, telephone;
int age, numberofyears;
char letterGrade;

// declare input file stream varaible and open file
ifstream fin;
fin.open("project2_input.txt");

if (fin.is_open())
{
cout << "File is open." << endl;

}

// declare output file stream varaible and open file


//read data from file
fin >> name;
fin >> age;
fin >> address;
fin >> numberofyears;
fin >> telephone;
fin >> social;
fin >> test1;
fin >> test2;
fin >> test3;
fin >> test4;
fin >> finalexam;

// calculate final semester grade
percentage = (.10 * test1) + (.15 * test2) + (.15 * test3) + (.20 * test4) + (.40 * finalexam);

if (percentage >= 90){
letterGrade = 'A';
cout<<"A"<<endl;}
else if(percentage >= 80){
letterGrade = 'B';
cout<<"B"<<endl;}
else if(percentage >= 70){
letterGrade = 'C';
cout<<"C"<<endl;}
else if(percentage >= 65){
letterGrade = 'D';
cout<<"D"<<endl;}
else{
cout<<"F"<<endl;}


cout << name;
cout << age;
cout << address;
cout << numberofyears;
cout << telephone;
cout << social;
cout << test1;
cout << test1;
cout << test2;
cout << test3;
cout << test4;
cout << finalexam;
cout << percentage;
fin.close();

ofstream fout;
fout.open("project2_output.txt");

if (fout.is_open())
{
cout << "File Open" << endl;
;
}
fout << name;
fout << age;
fout << address;
fout << numberofyears;
fout << telephone;
fout << social;
fout << test1;
fout << test1;
fout << test2;
fout << test3;
fout << test4;
fout << finalexam;
fout << percentage;

// closing files
fout.close();

return 0 ;

}
Last edited on
This is my input file data

Nancy Peterson
22
510 Tumble Dr., San Gabriel, TX 57981
3
(666) 759 - 2249
492-35-5984
80.9
90.2
98.5
89.8
97.7

I ran the corrected code, but 00000000 prints out on the output file. The calculations I tried to perform were the numbers average on a weighted scale were the first 4 numbers represent test1,test2,test3,test4 and the 5th number is the final exam score.
Time to learn about getline().

Input using >> will separate at whitespace, and the name, address and telephone numbers contain several whitespaces.
@lastchance

Do I change fin >> name; to getline(fin,name);
@ OP,
To give you an idea.

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


// Struct to fold the information of the student.
struct Student
{
	std::string name;
	std::string SSN;
	std::string address;
	std::string telephone;
	unsigned int age;
	unsigned int numOfYears;
};

void readFile(std::ifstream&, Student&, std::string, double&);
char determineGrade(double);
void writeFile(std::ofstream&, Student, std::string, char);

int main()
{
	Student student{};
	double percentage{};
	char letterGrade{};

	std::string inFileName{ "project2_input.txt" };
	std::string outFileName{ "project2_output.txt" };
	std::ifstream inFile;
	std::ofstream outFile;
	readFile(inFile, student, inFileName, percentage);
	letterGrade = determineGrade(percentage);
	writeFile(outFile, student, outFileName, letterGrade);

}
void readFile(std::ifstream& info, Student& student, std::string fileName, double& percentage)
{
	// An array to hold the weighted score, based on the type of exam.
	double weightedScore[]{ .10, .15, .15, .20, .40 };

	double testScores{};
	// A counter to be used to determine the weighted score
	// in the while loop.
	int countTestScores{ 0 };

	info.open(fileName);

	if (!info)
	{
		std::cout << "File [" << fileName << "] could not open.\n";
		return;
	}
	else
	{
		std::cout << "File [" << fileName << "] open.\n";
		std::getline(info, student.name);
		info >> student.age;
		info.ignore();
		std::getline(info, student.address);
		info >> student.numOfYears;
		info.ignore();
		std::getline(info, student.telephone);
		std::getline(info, student.SSN);

		// While is reading the scores, compute the percentage
		// by multiplying the test score by the proper weighted score.
		// Example: Score 98.5 * .10[aray index 0] = percentage.
		// Example: Score 87.5 * .15[array index 1] = percentage.
		// The percentage is to be used as a running total to
		// determine later the letter grade.
		while (info >> testScores)
		{
			percentage += (testScores * weightedScore[countTestScores]);
			countTestScores++;
		}
	}
	info.close();
	std::cout << "File [" << fileName << "] closed.\n";
}

// This function obtains the percentage
// obtain by reading the file to obtain
// the letter grade, which would be 
// returned as a character data type.

char determineGrade(double percentage)
{
	char letterGrade{};

	if (percentage >= 90)
		letterGrade = 'A';

	else if (percentage >= 80)
		letterGrade = 'B';

	else if (percentage >= 70)
		letterGrade = 'C';

	else if (percentage >= 65)
		letterGrade = 'D';

	else
		letterGrade = 'F';

	return letterGrade;
}

// This function writes a file with the name of the student
// and the final grade, based on the test scores from the input file.
void writeFile(std::ofstream& outFile, Student student, std::string fileName, char letterGrade)
{
	outFile.open(fileName);
	std::cout << "File [" <<fileName << "] open.\n";

	outFile << "Student Name\t\tFinalGrade\n";
	outFile << student.name << "\t\t" << letterGrade;

	outFile.close();
	std::cout << "File [" << fileName << "] closed.\n";
}


<project2_input.txt.>
Nancy Peterson
22
510 Tumble Dr., San Gabriel, TX 57981
3
(666) 759 - 2249
492-35-5984
80.9
90.2
98.5
89.8
97.7


Console output:
File [project2_input.txt] open.
File [project2_input.txt] closed.
File [project2_output.txt] open.
File [project2_output.txt] closed.


<project2_output.txt.>
Student Name		FinalGrade
Nancy Peterson		A

What am I doing wrong in this code? My input file still won't open.

#include <iostream>
#include <fstream> // library for file input and output
#include <string>
using namespace std;

int main() {

// Variables
double test1, test2, test3, test4, finalexam, percentage;
string name, social, address, telephone;
int age, years;
char letterGrade;


// declare input file stream varaible and open file

ifstream fin;

fin.open("project2_input.txt");



if (!fin) {

cout << "Could not open file. Terminating program." << endl;

return -1;

}



// declare output file stream varaible and open file

ofstream fout;

fout.open("project2_output.txt");


//read data from file

getline(fin,name);
fin >> age;
getline(fin,address);
fin >> years;
getline(fin,telephone);
getline(fin,social);
fin >> test1;
fin >> test2;
fin >> test3;
fin >> test4;
fin >> finalexam;

// calculate final semester grade
percentage = (.10 * test1) + (.15 * test2) + (.15 * test3) + (.20 * test4) + (.40 * finalexam);



if (percentage >= 90){

letterGrade = 'A';

cout<<"A"<<endl;}

else if(percentage >= 80){

letterGrade = 'B';

cout<<"B"<<endl;}

else if(percentage >= 70){

letterGrade = 'C';

cout<<"C"<<endl;}

else if(percentage >= 65){

letterGrade = 'D';

cout<<"D"<<endl;}

else{

cout<<"F"<<endl;}




cout << name;

cout << age;

cout << address;

cout << years;

cout << telephone;

cout << social;

cout << test1;

cout << test1;

cout << test2;

cout << test3;

cout << test4;

cout << finalexam;

cout << percentage;

fout << name;

fout << age;

fout << address;

fout << years;

fout << telephone;

fout << social;

fout << test1;

fout << test1;

fout << test2;

fout << test3;

fout << test4;

fout << finalexam;

fout << percentage;



// closing files

fin.close();

fout.close();

return 0 ;

}

Is your file "project2_input.txt" in the same location as your main.cpp file?

Disclaimer: I am using Visual Studio. That is where it would be placed as a default.
I am not sure about other IDEs, though.
Last edited on
Hello dbarclay100,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


Usually the input file is in the same directory as the ".cpp" file that contains "main".

Something you could try is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// declare input file stream varaible and open file
//ifstream fin;
//fin.open("project2_input.txt");
//
//if (!fin)
//{
//	cout << "Could not open file. Terminating program." << endl;
//	return -1;
//}

// declare output file stream varaible and open file
ofstream fout;
fout.open("FindMe.txt");

if (!fout)  // <--- Needs changed.
{
	cout << "Could not open file. Terminating program." << endl;
	return -1;
}

Like you see comment out all the input file code and leave the output code. I changed the file name to something you are not likely to use.

Since I do not know what operating system you have I will use Windows as an example.

In Windows File Explorer, or something like it, start at "C:\" and use find to find the output file name (FindMe.txt). This is where your input file should be. The thing about opening a file for output is that if the file name is not found it will create it.

Next you will have to deal with mixing "std::cin >> aVariable" and "std::getline(...)". Each works well, but not well together.

Hope that helps,

Andy
Topic archived. No new replies allowed.