Hello, would like to ask about this problem im facing.

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <ios>
#include <string>
#include <string.h>
#include <vector>
#include <sstream>
#include <algorithm>

#define MAX_MSG 30

using namespace std;
vector<UserInit> ReadUsersInitial() {
    fstream file ("Users_initial.txt");
    FILE *file1 = fopen("Users_initial.txt", "r");
    string tempFile;
    if (file.good()) {
//blah blah
 }
        file.close();
        fclose(file1);

        int numOfElements =0;
        char delim = ':';
        numOfElements = count (tempFile, delim);
        char* userArray [[B]numOfElements*2[/B]];
        UserInit x;
        vector<UserInit> UsersArray;
        int counter = 0;
        char* str = new char[tempFile.size() + 1];
        copy(tempFile.begin(),tempFile.end(),str);
        str[tempFile.size()] = '\0';
        strip(str);
        str = strtok (str, ":");
        while (counter < numOfElements) {
            userArray[counter] = str;
            str = strtok (NULL, ":");
            counter++;
        }
}
        delete[]str;
        counter = 0;
        int counter2 = 0;
        while (counter < numOfEntry) {
            x.SetType(atoi(userArray[counter2]));
            x.SetUserID(atoi(userArray[counter2+1]));
            x.SetUserName(userArray[counter2+2]);
            x.SetUserClearance(atoi(userArray[counter2+3]));
            counter2 += 4;
            UsersArray.push_back(x);
            counter++;
        }
        counter = 0;
        counter2 = 0;
        return UsersArray;
    }
    else
       cout << "Error opening file, please try again.";
}

Input file

U:0:Alice:1:
U:1:Bob:2:
U:2:Carol:3:
U:3:Derek:4:
U:4:Eve:5:

the bolded part is where it lies. Basically my program is trying to read a set of values from a file, and allocating it to a vector. But somehow when i transferred my code to a WIndows environment from Linux, it doesnt work anymore. Any help is much appreicated

Recommended Answers

All 2 Replies

Hello, would like to ask about this problem im facing.

When you create an array of fixed size you need to provide a constant value for the size. You are creating memory that is determined at compile time and not runtime so the size needs to be fixed and not change when the program is run. You have two alternatives:

  • Use a constant expression (such as a #define BUF_SIZE )
  • Create dynamic memory as necessary at runtime

In your situation I'd suggest the second approach. Something like the following should work

char * userArray = new char[numOfElements * 2]

But dont forget to delete[] that memory when you are finished ;)

You can't have a dynamic array. If you want to do that you need to use vector or use new/delete dynamic allocation. Space for arrays is reserved at compiletime so that's why you need a constant expression there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.