George George - 1 month ago 22
C++ Question

Class method not working

class automat
{
int stari[50], sf[50], n, nsf, si;

char m[50][50];

public:
automat(int a, int b) { setn(a); setstari(); setnsf(b); intitsf(); initm(); }

void setn(int a) { n = a; } //init. nr de stari

void setstari() { for (int i = 0; i < n; i++) stari[i] = i; } // init. starile

void setnsf(int a) { nsf = a; } // init. nr de stari finale

void intitsf() { for (int i = 0; i < nsf; i++) sf[i] = 0; } //init stari finale cu 0 la inceput

void initm() { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) m[i][j] = NULL; } // init muchii cu 0 la inceput

void setsf(int a) { sf[a] = 1; } // seteaza o stare finala

void addm(int a, int b, char c) { m[a][b] = c; } // adauga muchie

void setsi(int a) { si = a; } // seteaza starea initiala

void afismuchii()
{
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (m[i][j] != 0)
cout << i << " se duce in " << j << " cu " << m[i][j] << endl;
}
};




int main()
{
ifstream f("file.txt");

int n, m, F;
f >> n; f >> m; f >> F;

automat a(n, F);

int x, y; char c;

f >> x;
a.setsi(x); //init. stare initiala

while (F) // init. stari finale
{
f >> x;
a.setsf(x);
F--;
}

while (m) // init. muchii
{
f >> x; f >> c; f >> y;
a.addm(x, y, c);
m--;
}

a.afismuchii();
}


Please excuse my commentaries and the name of the functions.

At the end of the program, when i run " a.afismuchii(); " i should get:

0 goes to 1 with a
0 goes to 1 with b

but instead, the matrix is NULL and i only get:

0 goes to 1 with
0 goes to 1 with

so the addm() method may not be working properly, but i dont get why.

Answer

You have to cast (m[i][j]) to static_cast (m[i][j])

Comments