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; }
Dvole source share