Converting from Java to C++?

I have an assignment where I'm supposed to convert Java to C++. I've barely begun converting, but I've already run into several things I don't know how to convert. Here's my code:

#include <iostream>

#include <string>

#include <algorithm>

#include <iomanip>

using std::cout;

using std::cin;

using std::endl;

using std::string;

using std::transform;

using std::ios;

using std::setiosflags;

using std::setprecision;

int main()


int score, sumOfScoresByStudent = 0, sumOfAllScores = 0;

double classAverage;

string studentName, scoreStr;

string studentNameMessage = "Enter the name of the student: \n";

string numOfStudentsStr = "Enter the number of students: ";

string numOfScoresStr = "Enter the number of scores to be entered per student: ";

string outputTable = "Names, scores, average scores, and letter grades:\n\n";

numFormatter = getNumberInstance();



int numOfStudents = Integer.parseInt(JOptionPane


int numOfScores = Integer.parseInt(JOptionPane


int scores[][]= [numOfStudents][numOfScores];

string students[] = [numOfStudents];

double avgScores []= [numOfStudents];

char letterGrades []= [numOfStudents];

for (int i = 0; i < numOfStudents; i++) {

studentName = JOptionPane.showInputDialog(studentNameMessage);

students[i] = studentName;

outputTable += students[i] + " Scores: ";

for (int j = 0; j < numOfScores; j++) {

scoreStr = JOptionPane.showInputDialog("Enter score " + (j + 1)

+ " for " + students[i] + "; ");

score = Integer.parseInt(scoreStr);

scores[i][j] = score;

outputTable += scores[i][j] + " ";

sumOfScoresByStudent += score;

sumOfAllScores += score;


avgScores[i] = (double) sumOfScoresByStudent / numOfScores;

letterGrades[i] = findGrade(avgScores[i]);

outputTable += "Avg: " + numFormatter.format(avgScores[i]) + " "

+ "Grade: " + letterGrades[i] + "\n";

sumOfScoresByStudent = 0;


classAverage = (double) sumOfAllScores / (numOfStudents * numOfScores);

outputTable += "\nOverall class average = "

+ numFormatter.format(classAverage);

JOptionPane.showMessageDialog(null, outputTable);



static char findGrade(double averageScore) {

char theGrade;

if (averageScore >= 90)

theGrade = 'A';

else if (averageScore >= 80)

theGrade = 'B';

else if (averageScore >= 70)

theGrade = 'C';

else if (averageScore >= 60)

theGrade = 'D';


theGrade = 'F';

return theGrade;



PS. I know how to convert the number formatting, but here are the things I don't know how to do:

Convert a string to int

Get the arrays in the correct format

Any pointers you could give me would be tremendously appreciated!!


2 Answers

  • 6 years ago
    Favorite Answer

    In C++11. the <string> header defines several conversion functions:

    string s = "123";

    // signed integers:

    int i = stoi(s);

    long l = stol(s);

    long long ll = stoll(s);

    // unsigned integers:

    unsigned long ul = stoul(s);

    unsigned long long long ull = stoull(s);

    // floating point:

    float f = stof(s);

    double d = stod(d);

    long double ld = stold(d);


    Also, see the C++11 to_string function (in the <string> header) that converts the same types the other way. C++ does not automatically convert numbers to string type when concatenating using the + operator

    - - - - - - - -

    C++ does not allow variable-sized arrays. The closest substitute for Java-style arrays is vector<T>, very much like ArrayList<T> in Java, but supports indexing with [] brackets. See:

    See the constructor for an example. 2-D arrays need to be initialized in a for loop. You can declare and initialize your students[] and scores[][] arrays with:

    vector<string> students(numOfStudents); // start with (numOfStudents) entries, all empty strings.

    vector<vector<int>> scores; // start with empty outer vector

    for (int i=0; i<numOfStudents; ++i)

    { scores.push_back( vector<int>(numOfScores, 0) ); }

    That loop creates (numOfStudents) entries, each a vector of (numOfScores) zeroes, and adds them to the back (end) of the scores array.

    Note that C++ vectors and arrays contain objects, not object references. You never get the situation where two different positions in an array or vector refer to the same object, the way you do in Java. The same applies to object variables. This can change the way you design the object model for an application.

    • Commenter avatarLogin to reply the answers
  • Anonymous
    6 years ago

    String to int:

    As for your 2nd issue, can you be a bit more specific on array output?

    • Commenter avatarLogin to reply the answers
Still have questions? Get your answers by asking now.