开发者

String array, how do I use it? Implementation

开发者 https://www.devze.com 2023-01-25 18:25 出处:网络
So, I\'m writing console game as my first project on C++ and 开发者_StackOverflowwhat I want to do is to implement look function. Here is what it does:

So, I'm writing console game as my first project on C++ and 开发者_StackOverflowwhat 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.
}


OK here's the problem:

int inventory_array[49];

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

This code is at file scope. That is, it's not in a function. But zoneid[1][1] = "Text" is executable code -- it needs to be in a function.

You could put the initializer in main():

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


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

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

But doing this for an array of size 100*100 is impractical. So it is probably better to just move the initialization to the beginning of main.


You must include the header for strings

#include <string>


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

If you move that assignment in a function (for example, to the very beginning of main), then it should work better (but I have not checked the rest of the code for errors).


EDIT: (wrt your update)

You cant assign a string value in the global scope. You can only declare global variables. Btw, declaring global variables is generally considered bad practice anyway.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号