# Increment by one

• hello i made a calculator white table that archive all the calculation the code bellow is for the table and it work only for row = 0 namely the first row i tried to increment y with (y++) but it does not work :( .

@
int y=0;

if (operationBool){

``````   if (row == y && col == 0  )
{ return int  (fNum);  }  //  fNum = number

{if( row == y && col==1) return  QString("+");}

else if (divideBool)
{if( row == y && col==1) return  QString("/");}

else if (subtractbool)
{if  ( row == y && col==1) return  QString("-");}

else if (multiplyBool)
{if  ( row == y && col==1) return  QString("x");}

if (row == y && col == 2  ) { return int(SNum);}

if(row == y && col ==4)return int (tot);
``````

y++;
}
@

• Where are you incrementing y?

• i edited the code sorry for the problem .

• Hi,
What should the program do??
In what kind of function do you place your code snippet?
For indenting I would use this:
@
int y=0;

if (operationBool){

``````   if ( (row == y) &&
(col == 0)  )
{
return int  (fNum);   //  fNum = number
}
``````

// etc etc......
// Only 1 comparison per line, use () around every single one.
y++; // You are incrementing the y, but You only do the if ones??
}
@
Why do you increment y? There is only a single if (operationbool), so y will be destroyed when you exit your function. The if statement is only executed ones.

• hi
i want to increment " y " to pass to the next row the first row is row y ,where y =0,
i want to increment y to become y=1 ; and keep it like that until the next operation . so when (operationBool is true again ) y is equal to 1 not zero , so you were right when you sad that y will be destroyed after i exit the function ,
what should i do to keep y = 1 after i exit the function ?
thank you

• Hi,
You should keep y in memory ;-)
If you want to do it dirty, you make y a global scope variable, if you want it a nice, you make it a member variable on your class.
IMHO you should first read a good programming book, understand variable scope etc and building classes and so on.

• hello :)
i defined y as a : static int y ;// in header file
then in cpp file i defined int MyModel :: y = 0 ;
and the code became :

@QVariant MyModel::data(const QModelIndex &index, int role)const
{ int row = index.row();
int col = index.column();

qDebug() << QString("row %1, col%2, role %3")
.arg(row).arg(col).arg(role);

switch(role){
case Qt::DisplayRole:

if(row && col==3) return QString(" = "); // will type "=" in all the row and the column 3 ;

if (operationBool){ // when equal button is released operationBool == true

if (row==y && col == 0 )
return int(fNum); // on the first row and first column write the value of the variable fNum

if(addBool ) // add buttomn is released write + in the first row and first col
if( row==y && col==1) return QString("+");

else if (divideBool)
if( row==y && col==1) return QString("/");

else if (subtractbool)
if( row==y && col==1) return QString("-");

else if (multiplyBool)
if( row==y&& col==1) return QString("x");

if (row==y && col == 2 )
return int(SNum);// write the second value of my operation

if(row==y && col ==4)return int (tot);} // write the total

``````            break;
``````

}
return QVariant();
}
@
now were i should add my y++ ?
what i want to do is going to the next row every time i release the equal button .
i tried to make y as global int but it didn't work :(
i'd like to read a good book about c++ but i need to represent this project as a final exam the next week .
thank you :) :)

• it work finally and this is the code but i still have a problem that every time i raise the row the previous row disappear
@
QVariant MyModel::data(const QModelIndex &index, int role)const
{ int row = index.row();
int col = index.column();
for (int i = 0 ; i<9 ; i++ ){
if(row==i && col==3) return QString(" = ");
}

if (equalesbool){

if (row==y && col == 0 ) return int(fNum);

if(divideBool){
if( row==y && col==1) return QString ("div");
divideBool=false ;}

if( row==y && col==1) return QString("add");

}

else if(subtractbool==1){

if( row==y && col==1) return QString("sub");
subtractbool=false;
}

else if (multiplyBool) {

if( row==y&& col==1) return QString("mult");

multiplyBool=false;}

if (row==y && col == 2 ) return int(SNum);

if(row==y && col ==4)return int (tot);

if (opBool){
y ++;
opBool = false;}

}

return QVariant();

``````    };
``````

@