views:

280

answers:

3

EDIT: I also got an answer to make sector a vector of vectors:

vector<vector<char>>sector;

and that gets rid of the rest of my errors.

EDIT: I've made sector an array of pointers as someone suggested, and still get three errors:

EDIT: I have edited the program, but it has not fixed all of the errors:

I have this section of a program:

char* load_data(int begin_point,int num_characters);
ifstream mapdata("map_data.txt");
const int maxx=atoi(load_data(0,2));
const int maxy=atoi(load_data(2,2));
char** sector=new char[maxx][maxy];

char* load_data(int begin_point,int num_characters)
{
    seekg(begin_point);
    char* return_val=new char[num_characters+1];
    mapdata.getline(return_val,num_characters);
    return return_val;
}

And I get these errors:

line 5>error C2540: non-constant expression as array bound

line 5>error C2440: 'initializing' : cannot convert from 'char (*)[1]' to 'char **'

line 14>error C3861: 'seekg': identifier not found

per seekg: yes I know I have to include fstream, I included that in main.cpp, this is a separate .h file also included in main.cpp.

How do I fix the errors? Specifically, how to I fix the errors while keeping all my variables global?

Also, if it helps, this is map_data.txt:

10
10
00O
99!

1
55X
19
What is a question?
18
This is an answer
1
1
2
1
A: 

You can't return a pointer to a stack variable. And arrays need to be returned as pointer types.

Try:

char* load_data(int begin_point,int num_characters)
{
    seekg(begin_point);
    char* return_val = new char[num_characters+1];
    mapdata.getline(return_val, num_characters);
    return return_val;
}

char* foo = load_data(...);
...
delete [] foo;
jeffamaphone
doesn't work - I still get errors when I try to use atoi, etc.
Keand64
A: 

Well,

function load_data(int,int) returns a char. You are passing that char to the atoi function, that takes a char*. In addition to that, you are probably not including stdlib.h header file!!

#include <cstdlib>
int atoi(const char*);

If you dont wan't to include stdlib.h, then you could declare atoi as extern, but be aware when you compile this module.

extern int atoi(const char*)

Take into account that the argument of atoi function must be a null-terminated string.

In order for your code to work, you should make function load data return a char*, not a char.

char* load_data(int,int);

So, now you could do

//notice these aren't const, they rely on non-compile time available data.
int maxx = atoi (load_data(....));
int maxy = atoi (load_data(....));

If you are in C++, load_data function could return a std::string.

std::string load_data(int,int)

and then use c_str() method, which returns a C-String from a C++ string.

   const char* std::string:c_str()


    int maxx = atoi(load_data(....).c_str());
    int maxy = atoi(load_data(....).c_str());

In addition to that, you shouldn't

(regarding

line 5>error C2540: non-constant expression as array bound

line 5>error C2440: 'initializing' : cannot convert from 'char (*)[1]' to 'char **'

)

char sector[maxx][maxy];

You should

 char** sector = new char[maxx][maxy]();

and dont forget to free this memory

delete[](sector);
Tom
That's not how you initialize a 2-D array in C. It would be like char **sector; sector=new char*[maxx];for(int i=0;i<maxx;++i) sector[i]=new char[maxy];
Blindy
A: 

I'm not quite sure what is the goal of your exercise. But if you want to read 'stuff' from file and get it in format that you expect (like int, strings ...) you can just use operator>> and getline like this:

#include <fstream>
#include <string>

using namespace std;

int main()
{
    ifstream ifs("data.txt");
    if (!ifs.is_open()) return 0;

    int maxx;
    int maxy;

    ifs >> maxx >> maxy;
    cout << maxx << " " << maxy << endl;

    // ----

    char OO_0[4];       // can use char[] or string, see next
    ifs >> OO_0;
    OO_0[sizeof(OO_0)] = 0;

    cout << OO_0 << endl;

    // ----
    string _99;
    ifs >> _99;

    cout << _99 << endl;

    int one;
    string _55_X;
    int _19;
    string what_is;

    ifs >> one >> _55_X >> _19 >> ws;
    // ws gets rid of white space at the end of the line ...
    // this is because getline would only read that ws up to eol

    getline(ifs,what_is);

    cout << one << " " << _55_X << " " << _19 << " " << what_is << endl;

    ifs.close();
}

And you get output like this:

10 12
00O
99!
1 55X 19 What is a question?

Is that what you were after? NOTE: I'm using c++ because I noticed you mentioned "main.cpp"

stefanB