java---->c++

Thread Solved

Join Date: Jun 2007
Posts: 2,462
Reputation: zandiago is on a distinguished road 
Solved Threads: 25
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

java---->c++

 
0
  #1
Oct 26th, 2007
Good day. How do i convert the following code to C++?
  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,265
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: java---->c++

 
0
  #2
Oct 27th, 2007
>How do i convert the following code to C++?

With ease if you are me.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 2,462
Reputation: zandiago is on a distinguished road 
Solved Threads: 25
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

Re: java---->c++

 
0
  #3
Oct 27th, 2007
..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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,442
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: java---->c++

 
0
  #4
Oct 27th, 2007
Originally Posted by zandiago View Post
..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++
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC