Can't make my code work :S

Hi. I'm a newbie in coding, trying to learn some things, but for some reasons can't make my code work.
I have this type of textfile:
Rocca al Mare kontor Paldiski mnt 102, 13522 Tallinn Tel 665 5100 E 10-20, T 10-20, K 10-20, N 10-20, R 10-20, L 10-18, P 10-15
Kristiine kontor Endla 45, 10615 Tallinn Tel 665 5100 E-R 10-20, L 10-17, P 10-15

Basically, I need to store this content in a more simple structure which would consist of the name of "kontor", its address, telephone and a work schedule.

What I've done is:
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
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;
class kontorid{
    private:
    void decideDay(string tooaegu[]);
    protected:
    vector<string> name;
    vector<string> address;
    vector<string> telephone;
    vector<string> worktime;
    public:
    kontorid(){}
    void add(string nimiu, string aadressu, string telefonu, string tooaegu[]){
        if(nimiu!=""){
        name.push_back(nimiu);cout<<1<<endl;
        address.push_back(aadressu);
        telephone.push_back(telefonu);
        //decideDay(tooaegu);}
    }
    void print();
};

kontorid *ProcessLine(string textLine, char filename[20], kontorid *kontorid){
    int i=0;//char pos in string
    int pos=0;//array pos
    string textType[4];//deviding my string by \t
    string tooaeg[7];//days of the week
    while(i<textLine.length()){
        textType[pos]=textType[pos] + textLine[i];//adding by char
        switch(textLine[i])
          {
               case '\t':
               pos++;
               break;
          }
        i++;
    }
    i=0;
    pos=0;
    textLine=textType[3];
    while(i<textLine.length()){
        switch(textLine[i]){
               case ',':
               pos++;
               break;
               default:
               if(tooaeg[pos]==" "){//erasing spaces in the begining
                tooaeg[pos]=textLine[i];
               }
               else{//adding by char
                tooaeg[pos].append(textLine,i, 1);
                }
                break;
        }
        i++;
    }
    kontorid->add(textType[0],textType[1],textType[2],tooaeg);
    return kontorid;
}
kontorid *ReadFile(char filename[20], kontorid *kontorid){
    string textLine;
    ifstream inFile;
    inFile.open(filename);
    if(!inFile){
        cout<<"Unable to read"<<endl;
    }
    while (!inFile.eof()) {
        getline(inFile, textLine);
        kontorid=ProcessLine(textLine, filename, kontorid);
    }
    inFile.close();
}

int main()
{
    kontorid *kontorid;
    char filename[20];
    cout<<"File name: ";
    cin>>filename;
    cout<<endl;
    kontorid=ReadFile(filename, kontorid);
    //kontorid->print();
    return 0;
}

But my programme crashes. Well I kinda know the reason. It's in "void add". But I don't get it, what's wrong with it, am I pointing wrong to it or something? Kinda new with pointers, don't know all the tricky stuff.

Also I have another problem.
I have this void for deciding whether the days in schedule are simple (eg. M, T, F) or complex (M-F etc). My idea was that programme get the schedule, for example Monday-Friday and looks for that "-" sign and if it finds it, then it fills the missing gaps. Code:
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
void kontorid::decideDay(string tooaegu[]){
    int i=0;
    int first=0;
    int second=0;
    char week[]="ETKNRLP";
    while(i<7){
        if(&tooaegu[i][1]=="-"){
            for(int j=0; j<7; j++){
                if(tooaegu[i][0]==week[j]){
                    first=j;
                }
                if(tooaegu[i][0]==week[j]){
                    second=j;
                }
                if(first!=0 && second!=0){
                    for(int k=0; k<second-first+1;k++){
                    worktime.push_back(tooaegu[k].substr(4,tooaegu[k].length()-4));}
                }
            }
        }
        else{
            if(tooaegu[6]!=""){
            worktime.push_back(tooaegu[i].substr(2,tooaegu[i].length()-2));}
        }
        i++;
    }
}
The problem is that my if part doesn't want to work, only the else part does.
Last edited on
Hi, I just read ur last sentence but tell me if this is right:
if(&tooaegu[i][1]=="-")

'&' and '[i][1]' dont seem to make much sense to me...

I think u'r mixing up how char arrays and strings work.

This is ur initialization: string tooaeg[7];
The '7' represents how big the string tooaeg is.

When u pass the string tooaeg into a function it is like this:
1
2
void myfunc(string tooaeg)
{ ; }//do stuff 

And not with the brackets []

Unfortunately, in C++ there isn't a super convenient way to access specific characters in a string like u r trying to do.

I'd suggest reading about how to pass strings into functions and what exactly is passed. Also, read about the string class. It has many member functions that will do what u want like '.find' and '.compare'.

Your other option (probably better) is to adopt your code to only have 'char arrays'.
Topic archived. No new replies allowed.