Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
D:\CodeBlocks\TheRPG\Level.cpp|45|error: invalid types 'int[int]' for array subscript|
D:\CodeBlocks\TheRPG\Level.cpp|51|error: invalid types 'int[int]' for array subscript| 


in Level.cpp

C++
<pre>#include "Level.h"
#include <iostream>
#include "Enemy.h"
#include "Data.h"
#include <windows.h>
#include <fstream>
#include <string>


using namespace std;

void Level::getID()
{

    fstream fluxi("ID.txt",ios::in);
    for(int i=0; i<20; i++)
    {
        fluxi>>ID[i]>>IDP[i]>>IDN[i];
    }
    fluxi.close();
    system("PAUSE");

}


Level::Level()
{


}
Level::Level(float g,int e,int l ,int s)
    : gold(g),exp(e),lvl(e),stage(s)
{



}

void Level::showInventory()
{
    fstream fluxo("INV.txt",ios::in);
    k=0;
    while(!fluxo.eof())
    {
        fluxo>>IDO[k]>>IDPO[k]>>IDNO[k];
        k++;
    }

    for(int i=0; i<k; i++)
    {
        cout<<"Item LVL:"<<IDO[i]<<" ATK:"<<IDPO[i]<<" "<<IDNO[i]<<" Sword";
    }

}

void Level::addInventory()
{
    fstream fluxoa("inv.txt",ios::out | ios::app);

}

void Level::editInventory()
{
    fstream fluxoa("inv.txt",ios::out | ios::app);
}



void Level::stage1()
{
    getID();

    while(gameOver==false)
    {
        system("CLS");
        cout<<"\n\t\tLvl:"<<lvl<<"   Exp:"<<exp<<"   Gold:"<<gold<<"   Stage:"<<stage<<endl;




        system("PAUSE");
    }


}

void Level::stage2()
{

}
void Level::stage3()
{

}




in Level.h


C++
#ifndef LEVEL_H
#define LEVEL_H
#include "Data.h"
#include "Enemy.h"

class Level
{
public:
    Level();
    Level(float g,int e,int l ,int s);
    void stage1();
    void stage2();
    void stage3();
private:
    void getInventory();
    void editInventory();
    void showInventory();
    void addInventory();
    void getID();
    float gold;
    int lvl,stage,exp;
    int ID[21];
    int IDP[21];
    int IDO[21];
    int IDPO[21];
    int k;
    char IDN[21][10];
    char IDNO[21][10];

    bool gameOver;

};

#endif // LEVEL_H



What is wrong with the code?

What I have tried:

D:\CodeBlocks\TheRPG\Level.cpp|45|error: invalid types 'int[int]' for array subscript|
D:\CodeBlocks\TheRPG\Level.cpp|51|error: invalid types 'int[int]' for array subscript| 
Posted
Updated 12-Mar-17 21:29pm
Comments
nv3 12-Mar-17 19:02pm    
Take a look at:
fluxi>> .... >>IDN[i];
IDN is two-dimensional array. So IDN[i] is in itself an array! And the same problem somewhat further down in your code with IDNO.
SoLix 13-Mar-17 2:18am    
Can u show me how to do it? I've tried with string and still didn't work

1 solution

I guess that IDN is used by you as char array the operators of the fluxi object arent working.

C++
    for(int i=0; i<20; i++)
{
  fluxi >>IDN[i]; // calls char* operator
  fluxi >> i>ID[i]>>IDP[i];// calls int operator
}

Tip: Use longer variables names for better reading (of other people) and a full path your the files.
 
Share this answer
 
Comments
Member 13049206 13-Mar-17 10:28am    
I changed the variable names to something more visible and the code just worked maybe i had a typo , thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900