OOPs. Idk where to put my fstream and make it into an array

I am completely stumped.

I have 4 text files that have either Strings(Names) or Ints.
There is 1 input per line such as:
1
2
3
4
1
2
3
4


or
1
2
3
4
Jack
Bean
Kim
Bill


So where do I put these files in my code?? I get back weird exponential numbers and my name file returns nothing.

I don't get no/many errors when I modify, but I don't get the correct output(which should be a double).

here are some extra info:
fstream MEfile;
MEfile("data.txt")
output Array name: ME[] (MAX 4)



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

using namespace std;
int i;

	string names[]={"Bob","ERic","Bill","Joe"};
	int ME[4];
	int FE[4];
	int QA[4];
	
	
class myStudent
{
public:
	void getNames();
	void calcFinalGrade();
	void getData();

	
private:

	double FinalGrade;
	int newME;
	int newFE;
	int newQA;

};

void myStudent::getNames()
{
		cout<<setw(15)<<names[i];
	
}

void myStudent::calcFinalGrade() //Calculate into Letter Grade
{
	//int ME[4]={5,6,7,8};
	//int FE[4]={9,10,11,12};
	//int AQ[4]={13,14,15,16};

	newME=ME[i];
	newFE=FE[i];
	newQA=QA[i];







	FinalGrade=0.35*newME+.3*newFE+.35*newQA;

	//Print Letter Grade
	if (FinalGrade>=90 && FinalGrade<100)
		cout<<"  A"<<endl;
	if(FinalGrade>=80 && FinalGrade<89.9)
		cout<<"  B"<<endl;
	if (FinalGrade>=70 && FinalGrade<79.9)
		cout<<"  C"<<endl;
	if (FinalGrade<69.9)
		cout<<"  FAIL"<<endl;
	else
		cout<<"Final Grade is out of Range (0-100) or incorrect"<<endl;		
}


int main()
{

	int ME[4];
	ifstream MEfile("MidtermExam.txt");
	if (MEfile.is_open())
	{
		for(int i = 0; i < 4; i++)
		{
			MEfile>>ME[i];
		}
		MEfile.close();
	}

		ifstream FEfile("MidtermExam.txt");
	if (FEfile.is_open())
	{
		for(int i = 0; i < 5; i++)
		{
			FEfile>>FE[i];
		}
		FEfile.close();
	}

		ifstream QAfile("MidtermExam.txt");
	if (QAfile.is_open())
	{
		for(int i = 0; i < 4; i++)
		{
			QAfile>>ME[i];
		}
		QAfile.close();
	}

		


	for(i=0;i<4;i++)
	{
	myStudent student [4];

	student[i].getNames();	
	student[i].calcFinalGrade();
	}
	 
	system("pause");

}

thanks for the help.
Last edited on
hi BvsK,

are you sure that i is initialised here?

FinalGrade=0.35*ME[i]+.3*FE[i]+.35*AQ[i];

I've updating it.
Now giving incorrect FinalGrade.
Ok, you have to be careful comparing floats, they don't store data exactly. Google, and read up about them.

0.1 can't be stored exactly so this fails every time:

1
2
3
float MyFloat = 0.1;
if (10 * MyFloat == 1.0)   //always false


Also try some debigging with a debugger or lots of couts to track the values of your variables.
Topic archived. No new replies allowed.