LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   help with c++ code/pointers/arrays (https://www.linuxquestions.org/questions/programming-9/help-with-c-code-pointers-arrays-283272/)

drisay 01-28-2005 08:11 AM

help with c++ code/pointers/arrays
 
hi all,

i'm attempting a first c++ app here. it's a music db thing. anyways what it is is doesn't really matter. i'm guessing that it is a pointer issue. i guess i just don't quite understand them yet. here's my code so far.

Code:

#include <string>
#include <fstream>
#include <iostream>
using namespace std;

#define SCALES_FILE "../etc/scales.m"
#define CHORDS_FILE "../etc/chords.m"

#define MAIN_DELIM "="
#define SEC_DELIM ","
#define COMMENT_CHAR 0x23

#define SCALE_LENGTH 3
#define CHORD_LENGTH 2

#define BUFFER_MAX 80

// error message values
#define SCALE_FILE_UNDEFINED 1
#define CHORD_FILE_UNDEFINED 2

typedef struct {
        char *name;
        int increments[1];
        int increment_length;
} scale;

typedef struct {
        char *name;
        char **indicators;
        int increments[1];
        int increment_length;
} chord;

scale *scales;
int scale_length;
chord *chords;
int chord_length;

int setGlobalVariables();
scale setNextScale(char *buffer);

int setGlobalVariables() {
        char buffer[BUFFER_MAX];

        // set the scales from the config files
        ifstream scalefile(SCALES_FILE);
        if (!scalefile.is_open()) {
                return SCALE_FILE_UNDEFINED;
        }

        int index = 1;
        scale *tmp1;
        while (!scalefile.eof()) {
                scalefile.getline(buffer, BUFFER_MAX);

                // skip parsing the line if it is a comment (#) or a zero length line
                if (buffer[0] != (char)COMMENT_CHAR && strlen(buffer) > 0) {
                        tmp1 = new scale[index];

                        if (index > 1) {
                                for (int i = 0; i < (index - 1); i++) {
                                        tmp1[i] = scales[i];
                                        cout << tmp1[i].name << "tmp1" << endl;
                                }
                        }

                        tmp1[(index - 1)] = setNextScale(buffer);

                        delete scales;
                        scales = new scale[index];

                        for (int i = 0; i < index; i++) {
                                scales[i] = tmp1[i];
                                cout << scales[i].name << "scales" << endl;
                        }
                        index++;

                        delete tmp1;
                }
        }
        scale_length = index -1;

        scalefile.close();

        cout << scales[0].name << endl; // result= Minor (*wanted result= Major)
        cout << scales[1].name << endl; // result= Minor (*wanted result= Minor)

        return 0;
}

scale setNextScale(char *buffer) {
        scale tmp;
        char *tmpscale;

        tmp.name = strtok(buffer, MAIN_DELIM);
        tmpscale = strtok(NULL, MAIN_DELIM);

        // parsing for tmpscale required here

        return tmp;
}

here's my question... look at the section where i put comment // result=. i'm getting the wrong result and i'm assuming that it's a pointer/memory address issue that i don't quite get. every array entry takes the value of the last entry inserted.

also, is there an easier way to add extra memory to an array without completely reinitialising the entire thing?

any other comments on the code... please feel free to tear it apart... looking for any critics as i want to improve my coding.

thanks,
drisay.

drisay 01-28-2005 08:24 AM

also, is there a way to get the length of an array? in java there array_name.length. anything similar in c/c++?


All times are GMT -5. The time now is 06:00 PM.