943,986 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1063
  • Java RSS
Oct 26th, 2007
0

java---->c++

Expand Post »
Good day. How do i convert the following code to C++?
java Syntax (Toggle Plain Text)
  1. // Lets import our file functions.
  2.  
  3. import java.io.*;
  4.  
  5.  
  6. public class Grades {
  7. // Setup an array of test students
  8. private static Grades.test student[] = new Grades.test[50];
  9.  
  10. // Just for spunk we will create an inner class to keep this in one file.
  11. private static class test {
  12. public String id = "";
  13. public String answer = "";
  14. public double score = 0.0;
  15. public double grade = 0.0;
  16. }
  17.  
  18.  
  19. public static void main(String args[])
  20. {
  21.  
  22. int total=0;
  23.  
  24. // initialize(); This was removed because we initialize when we create students.
  25. getdata();
  26.  
  27. int i = 1;
  28.  
  29. // Loop through students until we reach our 50 limit or there were not that many in our class to begin with.
  30. while ((i < student.length - 1) && (student[i] != null))
  31. {
  32. total = 0;
  33.  
  34. for(int j = 0; j < 22; j++)
  35. {
  36. // Here we are going to check student answer against "master key" student (at index 0)
  37. if(student[0].answer.substring(j,j+1).equals(student[i].answer.substring(j,j+1)))
  38. {
  39. total = total + 5;
  40. }
  41. else if(student[i].answer.substring(j,j+1).equals(" "))
  42. {
  43. total = total - 4;
  44. }
  45. else
  46. {
  47. total = total - 2;
  48. }
  49. }
  50.  
  51. // Obviously if they end up leaving all blank or go negative, they get a zero for the grade.
  52. if (total < 0) { total = 0; }
  53.  
  54. // Here we print out the student id, their answers and total points (not grade point average!)
  55. System.out.println(student[i].id + " " + student[i].answer + " " + total);
  56. i++;
  57. }
  58. }
  59.  
  60.  
  61. // This fills our students answers from file.
  62. private static void getdata()
  63. {
  64.  
  65. try {
  66. // We setup a reader to read in text file students and answers.
  67.  
  68. BufferedReader infile = new BufferedReader(new FileReader("tftest.txt"));
  69.  
  70. String id = "";
  71. String answers = "";
  72.  
  73. // We loop until we get all 50 students
  74. for (int i = 0; i < student.length - 1; i++)
  75. {
  76. // Or until we hit the end of the line on our file.
  77. if (((id = infile.readLine()) != null) && ((answers = infile.readLine()) != null)) {
  78. student[i] = new Grades.test();
  79. student[i].id = id;
  80. student[i].answer = answers;
  81. }
  82. else { break; }
  83. }
  84.  
  85. infile.close();
  86.  
  87. }
  88. catch (IOException e) {
  89. System.out.println(e.toString());
  90. }
  91. }
  92. }
Similar Threads
Featured Poster
Reputation Points: 129
Solved Threads: 26
Nearly a Posting Maven
zandiago is offline Offline
2,463 posts
since Jun 2007
Oct 27th, 2007
0

Re: java---->c++

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

With ease if you are me.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Oct 27th, 2007
0

Re: java---->c++

..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.
Featured Poster
Reputation Points: 129
Solved Threads: 26
Nearly a Posting Maven
zandiago is offline Offline
2,463 posts
since Jun 2007
Oct 27th, 2007
0

Re: java---->c++

Click to Expand / Collapse  Quote originally posted by zandiago ...
..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++
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Help with ending a program
Next Thread in Java Forum Timeline: SOAP-XML-Exceptions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC