Need help with converting a string array to an int array.

My program reads data from a text file and displays its content, which I was able to do. The file looks like this,

3450, Guido VanRossum, 10, 15, 14, 18, 20
6120, Yukihiro Matsumoto, 10, 9, 13, 12, 14
9230, James Gosling, 10, 16, 13, 12, 10

I declared 3 string arrays to hold student IDs, Names, and Scores. The problem is that now I have to calculate the sum of the 5 scores for each student but I can't do that when the array holding scores is string. My code looks like 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
const int STUDENT_INFO = 10;
const int ROWS = 10;
const int COLS = 5;
int main(){
string studentIds[STUDENT_INFO];
string studentNames[STUDENT_INFO];
string studentScores[ROWS][COLS]; //<---String array holding scores.
ifstream inputFile;
inputFile.open("data121.txt");
if(inputFile){
for(int i=0; i<STUDENT_INFO; i++){
getline(inputFile, studentIds[i], ',');
cout << studentIds[i];
getline(inputFile, studentNames[i], ',');
cout << studentNames[i];
for(int j=0; j<COLS; j++){
getline(inputFile, studentScores[i][j], ','); //<---Reading and Displaying scores when the delimiter is a comma.
cout << studentScores[i][j];
}
}
for(int k=0; k<ROWS; k++){
int grade = 0;
for(int l=0; l<COLS; l++){
grade += studentScores[k][l]; //<---Can't take the sum of string array content. 


How would you convert the string array to an int array? I was told to change the studentScores array to int, but that won't work with getline. Any help is appreciated.
Last edited on
closed account (SECMoG1T)
well there are different ways you could use to parse the data, you can go for std::stringstreams, std::stoi or any other.

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <algorithm>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>

struct StudentData ///if you have learnt of structs/classes ... they good for keeping related data together.
{
    static const int NUMSCORES=5;
    std::string studentId;
    std::string firstName,secondName;
    int         scores[NUMSCORES]{};
    int         scoreSum{};

    StudentData(std::string _data);
    StudentData(){}
    void print();
};

StudentData::StudentData(std::string _data)///comma sep string
{
    std::replace_if(_data.begin(),_data.end(),[](char c){return c==',';},' ');
    std::stringstream _TempStrm(_data);///i'll parse the data with a std::stringstream object

    _TempStrm>>studentId>>firstName>>secondName;

    for(int i=0; i<NUMSCORES; i++)
    {
        _TempStrm>>scores[i];
        scoreSum += scores[i];
    }
}

void StudentData::print()
{
    std::cout<<"Name: "<<firstName<<" "<<secondName<<" ID: "<<studentId<<" scores{";

    for(int i=0; i<NUMSCORES; i++)
        std::cout<<scores[i]<<",";

    std::cout<<"}  Total: "<<scoreSum<<"\n";
}

int main()
{
    std::ifstream inputFile("data121.txt");

    if(!inputFile)
    {std::cout<<"failed to open your file\n"; return 1;}

    std::string _tempData{};
    const int STUDENT_INFO = 10;
    int index =0;
    StudentData record[STUDENT_INFO];///you can also use a vector or your favorite container type

    while(std::getline(inputFile,_tempData,'\n') && index < STUDENT_INFO)
    {
        record[index] = StudentData(_tempData);
        ++index;
    }

    for(int i =0; i<STUDENT_INFO; i++)
        record[i].print();
}
Do you have to use arrays ?
I would be much easier to create a class and store them in a vector.
string str = "45";
int myint = stoi(str);
I tried doing that,

1
2
3
4
5
6
7
string studentScores[ROWS][COLS];
int arrayOfScores[ROWS][COLS];
for(int i=0; i<ROWS; i++){
for(int j=0; j<COLS; j++){
arrayOfScores[i][j] = stoi(studentScores[i][j]);
}
}


Once I run the program this error message appears,
"terminate called after throwing an instance of 'std::invalid_arguement'
what(): stoi
Aborted (core dumped)".
Last edited on
use string inside stoi, not array.
@Awsom3Alan3, if you want help you need to show your whole code.

You will find it considerably easier to do as @Yolanda and @Thomas1965 have suggested and create a Student class or struct. You can define stream extraction (>>) and insertion (<<) operators for them.

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;

//=====================================

struct Student
{
   string id;
   string name;
   vector<int> scores;
};

istream &operator >> ( istream &strm, Student &student )
{
   string line;
   getline( strm, line );
   stringstream ss( line );

   getline( ss >> ws, student.id, ',' );  
   getline( ss >> ws, student.name, ',' );

   student.scores.clear();
   int mark;
   char comma;
   ss >> mark;   student.scores.push_back( mark );
   while ( ss >> comma >> mark ) student.scores.push_back( mark );
   return strm;
}

ostream &operator << ( ostream &strm, const Student &student )
{
   strm << "id: " << student.id << "    name: " << student.name << "    scores: ";
   for ( int mark : student.scores ) strm << mark << " ";
   return strm;
}

//=====================================

int main()
{
   vector<Student> students;
// fstream in( "data.txt" );
   stringstream in( "3450, Guido VanRossum, 10, 15, 14, 18, 20   \n"
                    "6120, Yukihiro Matsumoto, 10, 9, 13, 12, 14 \n"
                    "9230, James Gosling, 10, 16, 13, 12, 10     \n" );

   Student stud;
   while ( in >> stud ) students.push_back( stud );

   cout << "The following students were read:\n";
   for ( Student s : students ) cout << s << '\n';
}

The following students were read:
id: 3450    name: Guido VanRossum    scores: 10 15 14 18 20 
id: 6120    name: Yukihiro Matsumoto    scores: 10 9 13 12 14 
id: 9230    name: James Gosling    scores: 10 16 13 12 10
Last edited on
I got it to work,
Thanks!
Last edited on
Topic archived. No new replies allowed.