How can I get this declaration to work?
EDIT: I also got an answer to make the sector a vector of vectors:
vector<vector<char>>sector;
and it will save you from the rest of my mistakes.
EDIT: I made the sector an array of pointers as someone suggested and was still getting three errors:
EDIT: I edited the program, but didn't fix all the errors:
I have this section of the 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 am getting the following errors:
line 5> error C2540: non-constant array expression
line 5> error C2440: "initialization": cannot convert from 'char (*) [1]' to 'char **'
line 14> error C3861: 'seekg': id not found
per seekg: yes I know I need to enable fstream, I included this in main.cpp, this is a separate .h file also included in main.cpp.
How to fix errors? Specifically, how do 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
Well,
The load_data (int, int) function returns char. You are passing char to atoi function, which takes char *. In addition to that, you probably don't include the stdlib.h header file!
#include <cstdlib>
int atoi(const char*);
If you don't want to include stdlib.h then you can declare atoi as extern, but remember when you compile this module.
extern int atoi(const char*)
Note that the argument to the atoi function must be a null terminated string.
For your code to work, you must return the data to the load a char *, not char.
char* load_data(int,int);
So now you can 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 ++ the load_data function can return std :: string.
std::string load_data(int,int)
and then use the 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 this, you must not
(about
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 don't forget to free this memory
delete[](sector);
source to share
You cannot return a pointer to a stack variable. And arrays must 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;
source to share
I'm not entirely sure what the purpose of your exercise is. But if you want to read the "stuff" from a file and get it in the format you expect (like int, string ...), you can simply use the → 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 the result like this:
10 12
00O
99!
1 55X 19 What is a question?
Is this what you were? NOTE. I am using C ++ because I noticed that you mentioned "main.cpp"
source to share