Sum of array giving bad output

Hello, Im working on a program reading data from textfile, and then solving some tasks using a function library made as a class. In the "solve4" function I get a bad output no matter what (by bad I mean ="3582342347")

Maybe someone here can lead me on the right path...

Main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include "Resolver.h"

using namespace std;

int main()
{
    cout << "Resolver" << endl;
    Resolver taskToResolve("testdata.txt");
    taskToResolve.doIt();
    return 0;
}


Resolver.h:
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
#ifndef RESOLVER_H
#define RESOLVER_H

#include <iostream>
#include "bookStruct.h"

using namespace std;

const int MaxN=100;

class Resolver
{
    public:
        Resolver();
        Resolver(string filename);
        virtual ~Resolver();
        void setFileNames(string t);
        void doIt();
    protected:
    private:
        string book_Data;
        void parseTextLine(string tmp, int & bookCnt, bookList X[]);
        void readDataFromFiles(string bookFile, int & bookCnt, bookList X[]);
        //DataT X[MaxN];
        void solve1(int bookCnt, const bookList X[]);
        void solve2(int bookCnt, const bookList X[]);
        void solve3(int bookCnt, const bookList X[]);
        void solve4(int bookCnt, const bookList X[]);
        void solve5(int bookCnt, const bookList X[]);
};

#endif // RESOLVER_H 


Resolver.cpp:
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "Resolver.h"
#include <cstdlib>
#include <fstream>

using namespace std;

Resolver::Resolver()
{
    //ctor
}

Resolver::Resolver(string t)
{
    //ctor
    setFileNames(t);
}

Resolver::~Resolver()
{
    //dtor
}

void Resolver::setFileNames(string t){
    book_Data=t;
}


void Resolver::doIt(){
    int bookCnt;
    bookList X[MaxBooks];
    readDataFromFiles("testdata.txt", bookCnt, X);
    solve1(bookCnt, X);
    solve2(bookCnt, X);
    solve3(bookCnt, X);
    solve4(bookCnt, X);
    solve5(bookCnt, X);
}

void Resolver::parseTextLine(string tmp, int & bookCnt, bookList X[]){
    std::size_t found;

    found=tmp.find(";");
    if (found!=string::npos) {
        X[bookCnt].author=tmp.substr(0,found);
        tmp=tmp.substr(found+1);
    }

    found=tmp.find(";");
    if (found!=string::npos) {
        X[bookCnt].title=tmp.substr(0,found);
        tmp=tmp.substr(found+1);
    }

    found=tmp.find(";");
    if (found!=string::npos) {
        X[bookCnt].pageNumber=atoi(tmp.substr(0,found).c_str());
        tmp=tmp.substr(found+1);
    }

    bookCnt++;
}


void Resolver::readDataFromFiles(string bookFile, int & bookCnt, bookList X[]){

    bookCnt=0;

    ifstream finS(bookFile.c_str(),ios::in);

    bool first=true;
    while (!finS.eof()) {
        string tmp="";
        getline(finS,tmp);
        if (tmp!="") {
            if (first) {
                first=!first;
            } else {
                parseTextLine(tmp,bookCnt,X);
            }
        }
    }

    finS.close();
}

void Resolver::solve1(int bookCnt, const bookList X[]){
    //maximum pick
    int j=0;
    for (int i=1;i<bookCnt;i++) {
        if (X[i].pageNumber>X[j].pageNumber) {
            j=i;
        }
    }
    cout << X[j].title << endl;
}

void Resolver::solve2(int bookCnt, const bookList X[]){
    bool pNGT = false;
    int j;
    for (int i=1;i<bookCnt;i++) {
        if (X[i].pageNumber<15) {
            pNGT = true;
            j=i;
        }
    }
    if(pNGT == true){
        cout << X[j].title << " Has less than 15 pages" << endl;
    }
    else{
        cout << "No books have less than 15 pages" << endl;
    }
}

void Resolver::solve3(int bookCnt, const bookList X[]){
    bool exist = false;
    int j;
    for (int i=1;i<bookCnt;i++) {
        if (X[i].title=="The Old Man and the Sea") {
            exist = true;
            j=i;
        }
    }
    if(exist == true){
        cout << X[j].author << endl;
    }
    else{
        cout << "Does not exist" << endl;
    }
}

void Resolver::solve4(int bookCnt, const bookList X[]){
    int sum = 0;
    for (int i=1;i<bookCnt;i++) {
        sum+=X[i].pageNumber;
        }
    cout << sum << endl;
}

void Resolver::solve5(int bookCnt, const bookList X[]){
    int jules[MaxN];
    int j = 0;
    for (int i=1;i<bookCnt;i++) {
        if (X[i].author == "Jules Verne") {
            jules[j] = i;
            j++;
        }
    }
    for (int p=0;p<j;p++) {
        cout << jules[p] << endl;
    }
}


bookStruct.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef BOOKSTRUCT_H_INCLUDED
#define BOOKSTRUCT_H_INCLUDED

#include <iostream>


using namespace std;

const int MaxBooks=100;

struct bookList{
    string author;
    string title;
    int pageNumber;

    //int bookCnt;
    //bookList X[MaxBooks];
};

#endif // BOOKSTRUCT_H_INCLUDED 


testdata.txt:
1
2
3
4
5
6
7
8
9
10
11
10
Author01;Title01;25
Author02;The Old Man and the Sea;345
Author03;Title03;30
Jules Verne;Title04;70
Author05;Title05;30
Author06;Title06;260
Author07;Title07;250
Jules Verne;Title08;150
Author09;Title09;200
Author10;Title10;240

I really dont understand why all the 4 other tasks work excellent using the same array..
Here is an example of the output:
1
2
3
4
5
6
7
8
9
10
11
Resolver
Title09
Title04 Has less than 15 pages
Author02
-736582296
3
7

Process returned 0 (0x0)   execution time : 0.009 s
Press any key to continue.
Last edited on
The problem is in void Resolver::parseTextLine(); you say in Resolver.cpp:54
1
2
3
4
5
    found=tmp.find(";");
    if (found!=string::npos) {
        X[bookCnt].pageNumber=atoi(tmp.substr(0,found).c_str());
        tmp=tmp.substr(found+1);
    }
but there is no semicolon (';') after the number of pages, so it never initializes `pageNumber'.


By the way, great post: good description of the problem, with all the code needed, and an input file to test.
But it's a little bothersome to handle the multiple files. Consider upload them to github or simiilar, next time.

Regards.
By the way, index array may go from 0 to size-1, ¿why do you start your loops at 1? (missing the first book)
for (int i=1;i<bookCnt;i++)
Topic archived. No new replies allowed.