String array how to use it? Implementation

So, I am writing a console game as my first C ++ project, and I want to implement a search function. Here is what he does:

get current coordinate read description from 2d string array cout description

But I can't get this array of 2d strings to work.

string zoneid[100][100]; zoneid[1][1] = "text"; cout << "You see " << zoneid[1][1] << endl; 

It gives me an error - the expected constructor, destructor, or type conversion before the '=' token in the first line. I tried with braces, braces, still not helping. Googling didn't help either.

Update: here is the full code, but the error is indicated only in the line zoneid [1] [1] = "text";

  #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <stdlib.h> #include "genlib.h" #include "strutils.h" #include <time.h> #include <string> int inventory_array[49]; int coordsX; int coordsY; std::string zoneid[100][100]; zoneid[1][1] = "Text"; void init_inv() { for (int i=0; i < 50; i++) { inventory_array[i] = 0; } } void introduce() { cout << "Welcome to Diablo 2! " << endl; } void inventory() { cout << endl << "Your inventory:" << endl; for (int i = 0; i < 50; i++) { if (inventory_array[i] != 0) { cout << i << ". " << "something" << endl; } } } int itemRoll() { int item_id = 0; item_id = (rand() % 1000); return item_id; } void look(int x, int y) { cout << "You see " << zoneid[1][1] << endl; } void inputController() { while (true) { cout << "Please enter command!" << endl; string command; getline(cin, command); if (command == "inv") { inventory(); } if (command == "look") { look(coordsX, coordsY); } if (command == "roll") { for (int i=0; i < 50; i++) { cout << itemRoll() << endl; } cout << itemRoll() << endl; } if (command == "kill") { cout << "KILL COMMAND ACTIVATED" << endl; } if (command == "quit") { cout << "FAILED TO INTERPRET" << endl; break; } } } void ending() { cout << "Thanks for playing Diablo 2"; } int main(int argc, char ** argv) { srand(time(NULL)); introduce(); init_inv(); coordsX = 1; coordsY = 1; inputController(); ending(); return 0; } 
+4
source share
6 answers

OK here is the problem:

 int inventory_array[49]; int coordsX; int coordsY; std::string zoneid[100][100]; zoneid[1][1] = "Text"; 

This code is in the file area. That is, this is not a function. But zoneid[1][1] = "Text" is executable code - it must be in a function.

You can put the initializer in main() :

 int main() { zoneid[1][1] = "Text"; // ... } 
+3
source

Your definition does not work, because you can assign a value to a global variable only inside the function body or on the same line as you declared it.

So:

 int a; a = 5; // Error int b = 5; // OK, definition in same line as declaration int c; int main() { c = 5; // OK, definition within a function body. } 
+5
source

You cannot initialize your array as outside of a function. You can do this though:

 string zoneid[][] = { {"text"} }; 

But doing this for an array of size 100 * 100 is impractical. Therefore, it is probably best to simply initialize to the beginning of main .

+1
source

EDIT: (according to your update)

You cannot assign a string value in the global scope. You can only declare global variables. By the way, declaring global variables is usually considered bad practice anyway .

0
source

You must include a header for the lines

 #include <string> 
0
source

In C ++, it is not allowed to have executable statements (for example, zoneid[1][1] = "Text"; ) outside functions.

If you move this assignment to a function (for example, to the beginning of main ), then it should work better (but I did not check the rest of the code for errors).

0
source

All Articles