Good day. How do i convert the following code to C++?
// Lets import our file functions.
import java.io.*;
public class Grades {
// Setup an array of test students
private static Grades.test student[] = new Grades.test[50];
// Just for spunk we will create an inner class to keep this in one file.
private static class test {
public String id = "";
public String answer = "";
public double score = 0.0;
public double grade = 0.0;
}
public static void main(String args[])
{
int total=0;
// initialize(); This was removed because we initialize when we create students.
getdata();
int i = 1;
// Loop through students until we reach our 50 limit or there were not that many in our class to begin with.
while ((i < student.length - 1) && (student[i] != null))
{
total = 0;
for(int j = 0; j < 22; j++)
{
// Here we are going to check student answer against "master key" student (at index 0)
if(student[0].answer.substring(j,j+1).equals(student[i].answer.substring(j,j+1)))
{
total = total + 5;
}
else if(student[i].answer.substring(j,j+1).equals(" "))
{
total = total - 4;
}
else
{
total = total - 2;
}
}
// Obviously if they end up leaving all blank or go negative, they get a zero for the grade.
if (total < 0) { total = 0; }
// Here we print out the student id, their answers and total points (not grade point average!)
System.out.println(student[i].id + " " + student[i].answer + " " + total);
i++;
}
}
// This fills our students answers from file.
private static void getdata()
{
try {
// We setup a reader to read in text file students and answers.
BufferedReader infile = new BufferedReader(new FileReader("tftest.txt"));
String id = "";
String answers = "";
// We loop until we get all 50 students
for (int i = 0; i < student.length - 1; i++)
{
// Or until we hit the end of the line on our file.
if (((id = infile.readLine()) != null) && ((answers = infile.readLine()) != null)) {
student[i] = new Grades.test();
student[i].id = id;
student[i].answer = answers;
}
else { break; }
}
infile.close();
}
catch (IOException e) {
System.out.println(e.toString());
}
}
}