So, I'm writing console game as my first project on C++ and what I want to do is to implement look function. Here is what it does:
get current coordinate read description from 2d array of strings cout description
But I can't make that 2d string array to work.
string zoneid[100][100];
zoneid[1][1] = "text";
cout << "You see " << zoneid[1][1] << endl;
It gives me error - expected constructor, destructor, or type conversion before '=' token on the first line. I tried with braces, curly braces, still doesn't help. Googling didn't help much either.
Update: here is complete code, but the error is only on 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;
}
Your definition doesn't work because you can assign a value to a global variable only within a function body or in the same line as you declare 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.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With