0

I would like to use strings in c++, but I couldn't. When I create strings, then I get always an error message. I would like to absolve the problem so if it is possible I don't want to use . (I don't know, how should I use with matrix strings)

Here is a small part of my code:

#include <iostream>
#include <fstream>

using namespace std;

int main ()
{
    string fajlnev="tancrend.txt";
    ifstream fajl(fajlnev.c_str());

    int i=0;
    int db=0;
    string atmeneti;

    while (!fajl.eof())
    {
        getline(fajl,atmeneti);
        getline(fajl,atmeneti);
        getline(fajl,atmeneti);
        db++;
    }
    db--;

    string tanc[db][3];

    fajl.close();
    ifstream fajl2(fajlnev.c_str());

    for(i=0;i<db;i++)
    {
        fajl2>>tanc [i][0];
        fajl2>>tanc [i][1];
        fajl2>>tanc [i][2];
    }
Thomas Ayoub
  • 27,208
  • 15
  • 85
  • 130
Balint
  • 9
  • 1
  • `#include `. And, although it isn't the problem here, get rid of `using namespace std;`. – Pete Becker Jul 07 '16 at 12:20
  • Terrible code. VLAs are not supported, `db` can be `-1`. – LogicStuff Jul 07 '16 at 12:20
  • 3
    Mandatory read: [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – molbdnilo Jul 07 '16 at 12:20

1 Answers1

0

string tanc[db][3]; is illegal C++. All values used as array sizes should be compile-time constant (so you should be able to know exact value before program is executed).

Some compilers allows variable-length arrays as language extentions, but those are usually limited (Usually they allow what C allows and no more than that).

In your case it is better to use C++ variable length container: std::vector

std::vector<std::array<std::string, 3>> tanc(db);
Revolver_Ocelot
  • 7,747
  • 3
  • 26
  • 45