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());    
        }
    }
}

Recommended Answers

All 3 Replies

Member Avatar for iamthwee

>How do i convert the following code to C++?

With ease if you are me.

..in other words, i heard that the two languages are similar and are different in other respects(such as speed and efficiency). What i was reffering to was are the commands the same, headers, mathematical calculations...ect....I didn't need you to convert the entire program...i'm not studying java.

..in other words, i heard that the two languages are similar and are different in other respects(such as speed and efficiency). What i was reffering to was are the commands the same, headers, mathematical calculations...ect....I didn't need you to convert the entire program...i'm not studying java.

You heard correctly - similar. Similar != same.
Some key words are the same. Java doesn't have headers. Many math calculations are the same, though Java does not allow for operator overlading.

If you expect to convert the code from Java to C++, you are obviously going to need to study a little bit of Java. That would require some homework on your part... though it seems like it would be easier to just do your own C++ homework instead of trying to convert someone else's Java homework into C++ :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.