3 Replies - 849 Views - Last Post: 02 May 2013 - 03:38 PM Rate Topic: -----

#1 physfun  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-March 13

invalid lvalue in assignment... assigning values to an array

Posted 02 May 2013 - 10:41 AM

Hi, am trying to program Shut the Box (http://en.wikipedia.org/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. =[ If you have any ideas, I'd really appreciate it!


 #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;
}




Is This A Good Question/Topic? 0
  • +

Replies To: invalid lvalue in assignment... assigning values to an array

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4142
  • View blog
  • Posts: 12,892
  • Joined: 25-December 09

Re: invalid lvalue in assignment... assigning values to an array

Posted 02 May 2013 - 11:19 AM

Please post the complete error messages exactly as they appear in your development environment.

By the way you shouldn't use "rand" for a variable name, there is a function with that name in stdlib.h.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7883
  • View blog
  • Posts: 13,396
  • Joined: 19-March 11

Re: invalid lvalue in assignment... assigning values to an array

Posted 02 May 2013 - 11:23 AM

else tiles[1]=1 && tiles[2]=1; 



You can't compound statements this way.
This would have to be

else { 
  tiles[1]=1; 
  tiles[2]=1; 
}


Was This Post Helpful? 1
  • +
  • -

#4 physfun  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-March 13

Re: invalid lvalue in assignment... assigning values to an array

Posted 02 May 2013 - 03:38 PM

View Postjon.kiparsky, on 02 May 2013 - 11:23 AM, said:

else tiles[1]=1 && tiles[2]=1; 



You can't compound statements this way.
This would have to be

else { 
  tiles[1]=1; 
  tiles[2]=1; 
}





Wow, I can't believe I didn't see that. Thank you.


jimblumberg, that is the entire error message- "invalid lvalue in assignment". But thanks, I'll change the name of rand.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1