Anyone with java knowledge that can help me convert code to C++

Hello,

Could someone help me convert this?

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

import java.util.Scanner;
 
public class AssignGrades {
 
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the number of students:");
        int numOfStudents = in.nextInt();
        double[] scores = new double[numOfStudents];
        System.out.print(" Enter " + numOfStudents + " scores:");
        double best = 0;
        for (int i = 0; i < numOfStudents; i++) {
            scores[i] = in.nextDouble();
            if (best < scores[i]) {
                best = scores[i];
            }
        }
		System.out.print(" ");
        char grade;
        for (int i = 0; i < scores.length; i++) {
            if (scores[i] >= best - 10) {
                grade = 'A';
            } else if (scores[i] >= best - 20) {
                grade = 'B';
            } else if (scores[i] >= best - 30) {
                grade = 'C';
            } else if (scores[i] >= best - 40) {
                grade = 'D';
            } else {
                grade = 'F';
            }
            System.out.println("Student " + i + " score is " + (int)scores[i] + " and grade is " + grade);
        }
    }
}


Thank you
http://www.mathcs.emory.edu/~lxiong/cs170/share/code/chapter6/AssignGrade.java.html
Well since you seem to be in lazy mode, just searching for the easy way out.

Hell, at least you've made an effort to search the web. Some people just copy/paste their assignment and demand code.

Anyway
> Scanner in = new Scanner(System.in);
Replace all further mention of in with std::cin

> System.out.print("Enter the number of students:");
Replace all System.out.print with std::cout

A couple of other tweaks, and you should be nearly there.
of all the code in the world to convert, you picked this?!
If you have a similar assignment, you should just sit down and rewrite it. The guts are there, sure, but its harder to convert than rewrite if you don't understand anything. Or re-google and find a c++ version. You won't learn much converting this with someone else doing it, and you will fall more behind than you already are.

on top of what was said you have to strip out the unnecessary class, write a proper main, get the correct includes (lose the import), fix your memory leak (java does for you) or use vector (more to convert and learn) and just for the head on desk of it fix 22-32 (its like one line of code, you don't need all those conditions). By the time you did all that, a clean page was the better way.
Last edited on
Topic archived. No new replies allowed.