Issues with a struct array. Any assistance would be appreciated

im having some trouble with assigning values to a structure. it seems to set every element of the array the same value, even though it should set it to a specified index number. ive looked over my code time and time agian, but i dont seem to see any problems. maybe an objective 3rd party might see what i am not.
thanks in advance


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
153
154
155
156
157
158
159
160
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <windows.h>

#define MAX         2

using namespace std;

typedef struct
{
    char *cmd;
    char *params[6];
    //int xpos,ypos;
    //char *text;
    int num;
} script_t;

void reverse(char s[]);
char *convert(int n);

class {
    public:

        void cparse_line()
        {

            char* pch;
            char  output[80];
            int num=0;

            // printf("%s",scr_line[read_index]);

            sprintf(output,"%s",scr_line[read_index]);
            printf("%s\n",output);
            pch = strtok (output,";");
            //printf("%s\n",pch);

            scr[set_index].cmd=pch;
         //   printf("%d %s\n",set_index,scr[set_index].cmd);

            while (pch != NULL)
            {

                pch = strtok (NULL, ";");
                if (pch!=NULL) printf ("%s|%d %d\n",pch,num,set_index);
                if (pch!=NULL) scr[set_index].params[num]=pch;
                num++;

            }

            scr[set_index].num=num;

        }

        void cprint_line()
        {
            printf("%d %s %s %s\n",read_index,scr[read_index].cmd,scr[read_index].params[0],scr[read_index].params[1]);
        }

        void cset_index(int index)
        {
            set_index=index;
        }

        void cread_index(int index)
        {
            read_index=index;
        }

        void cset_line(char *line)
        {
            scr_line[set_index]=line;
        }

        int get_rindex()
        {
            return read_index;
        }

        int get_sindex()
        {
            return set_index;
        }

    private:
        char *scr_line[100];
        script_t scr[MAX];
        int set_index,read_index;
}script;




void gotoxy( short x, short y ) {

HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
COORD position = { x, y };

SetConsoleCursorPosition( hStdout, position );
}

/* reverse:  reverse string s in place */
void reverse(char s[])
{
    int i, j;
    char c;

    for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
        c = s[i];
        s[i] = s[j];
        s[j] = c;
    }
}

/* itoa:  convert n to characters in s */
char *convert(int n)
{
    int i, sign;
    char s[32];

    if ((sign = n) < 0)  /* record sign */
        n = -n;          /* make n positive */
    i = 0;
    do {       /* generate digits in reverse order */
        s[i++] = n % 10 + '0';   /* get next digit */
    } while ((n /= 10) > 0);     /* delete it */
    if (sign < 0)
        s[i++] = '-';
    s[i] = '\0';
    reverse(s);
    return s;
}


int main()
{

int ind=0;

script.cset_index(0);
script.cread_index(0);
script.cset_line("printf;5;4;a sample string 1");
script.cparse_line();

script.cset_index(1);
script.cread_index(1);
script.cset_line("printf;5;8;a sample string 1");
script.cparse_line();

for(ind=0;ind<MAX;ind++)
{
script.cread_index(ind);
script.cprint_line();
}

return 0;

}
Sounds like you're (incorrectly) throwing around pointers somewhere.
You should rewrite the code so that no char arrays are used (use std::string instead). And instead of arrays, use vectors.
This will most likely take care of the problem as a side-effect.
thanks for the info. i did change the structure command and parameters to std::string's and it works great now. had to keep a few char arrays and pointers though as it not all the functions i am using seem to work with the string type. thanks again for the advice. oh and one last thing, how exactly do i use vectors. its been a looooong time since ive done much programming, and i never actually used vectors when i programmed for DOS. anyways thanks again,you help alot.
Topic archived. No new replies allowed.