c++ math problem in my code

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{
ifstream StudentAnswers;
ifstream CorrectedAnswers;
const int size = 20;
char SA[size];
char CA[size];
int counter = 1;
int wrong = 1;
int correct = size - wrong;
float tp = correct / size;

cout << "Test #1 Grades \n";
cout << "--------------\n";
cout << "Question# Student's Answers Correct Answers \n";

StudentAnswers.open("StudentAnswers.txt", ios::in);
CorrectedAnswers.open("CorrectedAnswers.txt", ios::in);

for(counter; counter < 21; counter++)
{
StudentAnswers >> SA[counter];
CorrectedAnswers >> CA[counter];
if(SA[counter] != CA[counter])
{
cout << "Q" << counter << setw(11) << SA[counter - 1] << setw(20) << CA[counter - 1] << endl;
wrong++;
}
}
cout << "This student got " << wrong << " wrong \n";
cout << "The Student got a " << tp << " on the test";

StudentAnswers.close();
CorrectedAnswers.close();

return 0;
}
// sorry about the format. I don't know how to do it the other way.
// The problem with my code is tp(total percentage) and it won't calculate the math correctly.
In math you can give an equation before you provide values, but the equation cannot be used until you have values. For example, I can say:

y = mx + b

then say:

m = 1/2, b = -2, x = 2

At that point, you can go back and plug in the values to get y = -1.

The computer never goes back. You have to tell it what to do, and when. Which means that you have to get m, b, and x before you can tell it what to do to get y.

Hope this helps.
thanks for the help. I also found another reason was that i was using int trying to put them into floats
That's not an issue. An int will happily convert to a float.
I agree the issues here is the placement of the code
1
2
int correct = size - wrong;
 float tp = correct / size;

in this case it become a case of Garbage In Garbage Out.
I thin the following code sample might give you the answer you are looking for.
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
#include <iostream>
 #include <iomanip>
 #include <fstream>

 using namespace std;

 int main()
 {
 ifstream StudentAnswers;
 ifstream CorrectedAnswers;
 const int size = 20;
 char SA[size];
 char CA[size];
 int counter = 1;
 int wrong = 1;
 int correct;
 float tp;

 cout << "Test #1 Grades \n";
 cout << "--------------\n";
 cout << "Question# Student's Answers Correct Answers \n";

 StudentAnswers.open("StudentAnswers.txt", ios::in);
 CorrectedAnswers.open("CorrectedAnswers.txt", ios::in);

 for(counter; counter < 21; counter++)
 {
 StudentAnswers >> SA[counter];
 CorrectedAnswers >> CA[counter];
 if(SA[counter] != CA[counter])
 {
 cout << "Q" << counter << setw(11) << SA[counter - 1] << setw(20) << CA[counter - 1] << endl;
 wrong++;
 }
 }
 correct = size - wrong;
 tp = ((float)correct) / size
 cout << "This student got " << wrong << " wrong \n";
 cout << "The Student got a " << tp << " on the test";

 StudentAnswers.close();
 CorrectedAnswers.close();

 return 0;
 }
There are two problems. The first is this code:
1
2
3
4
int counter = 1;
int wrong = 1;
int correct = size - wrong;
float tp = correct / size;

The last statement will do integer division of correct/size to generate an integer result. The integer is then converted to float and stored. You want the conversion to happen earlier than that. So try:
 
float tp = (float)correct / size;

This will convert "correct" to a float. That sets up a sort of chain reaction: to do the division, the compiler converts "size" to a float also, does division and then assigns the result to tp.

The second problem is more serious. When you say "tp = (float)correct / size;" it simply computes the value of correct/size and assigns it to the variable tp. If "correct" or "size" change later on, it does NOT update tp automatically. So you have to set tp later on in the code.
I agree with this analyst but I think both correct and tp set up later on in the code.
Topic archived. No new replies allowed.