invalid lvalue in assignment... Shut the Box Program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PhysKid
    New Member
    • May 2013
    • 1

    invalid lvalue in assignment... Shut the Box Program

    Hi, am trying to program Shut the Box (http://en.wikipedia.or g/wiki/Shut_the_Box). when I decide which tile to flip, I get the error " invalid lvalue in assignment". I've looked this up and it's usually because you need == instead of =, but I am assigning the value 1 to the array element... So I can't figure it out.... I will take some of it out so you can read it easier.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include "random.hpp"
    
    int main(int argc, char* argv[]){
      if (argc!=3) printf("please enter: shut <#tiles> <#games to run>\n");
     int numTiles= atoi(argv[1]);
     int numGames= atoi(argv[2]);
    int tiles[numTiles];
    int die1, die2, dietotal;
    int score=0, highscore=0, lowscore=0, medianScoreTot=0;
    
     printf("no tiles: %d, games: %d \n", numTiles, numGames);
    
    
    for (int j=0;j<numTiles;j++){ //init the tiles: 0 is up, 1 is down
    tiles[j]=0;
    }
    
    for (int i=0; i<numGames; i++){ //begin a new game
    die1= randui(1, 6);
    die2= randui(1, 6);
    dietotal=die1+die2;
    
     printf("die1: %d, die2: %d, total: %d \n", die1, die2, dietotal);
    
    //*******************************************
    //lowst firstfor (int k=1; k<dietotal+1;k++){
     if (dietotal==2){
    tiles[2]=1;
     }else if (dietotal==3){
    int rand= randui(1, 2);
      if (rand==1){ 
        tiles[3]=1;
      }else tiles[1]=1 && tiles[2]=1;  //**the ERROR comes here
     }
    
     else if (dietotal==4){ //************* 
    int rand= randui(1, 3);
      if (rand==1){ 
        tiles[4]=1;
      }else if (rand==2){
     tiles[3]=1 && tiles[1]=1;  //**the ERROR comes here
      }else tiles [2]=1;  
    }    
    
     else if (dietotal==5){ //************* 
    int rand= randui(1, 3);
      if (rand==1)tiles[5]=1;
     else if (rand==2) tiles[4]=1 && tiles[1]=1; 
     else tiles[3]=1 && tiles[2]=1;   //**the ERROR comes here
     }
     else printf("there was an error. your sum was not 2-12\n");
    
    }//end loooop for no games.
    
    // print the tile  array to see which were turned.
    for (int l=1;l<numTiles+1 ;l++){ //init the tiles: 0 is up, 1 is down
      printf("tile[%d]: %d /n", l, tiles[l]);
    }
    //****************
     return 0;
    }
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Code:
    tiles[3]=1 && tiles[2]=1
    This doesn't make any sense. Why are you boolean ANDing 1 with tiles[2]?

    I think you meant to do the assignment separately.
    Code:
    else {
       tiles[3] = 1;
       tiles[2] = 1;
    }

    Comment

    Working...