Thread: Lvalue required Turbo C++ 3.0

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    2

    Lvalue required Turbo C++ 3.0

    Hi all!

    I'm trying to make a c game, something like hangman.
    I have a problem trying to get a different word each time. I used a function I found on the Internet to get a random number each time the game is run. I checked and this function works.
    Next, I used "switch" to assign a different word for every number (from 1 to 20). I get a "Lvalue required" error for every case on ' word="WORDX" '. Why can't I assign a value to the word string inside the switch function? Please help me!

    Code:
    int number, lenght;
    char word[256];
    
    number="get_random_number_function()";
    
    switch(number)
    {
             case '0': word="WORD1";
                         lenght=strlen(word);
                         word[lenght]='\0';
                         break;
             case '1': word="WORD2";
                         lenght=strlen(word);
                         word[lenght]='\0';
                         break;
    ...
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Short answer is you need to use strcpy to copy the data into the array, you cannot assign a string literal like "WORDX" to an array like you are attempting.
    Code:
    char word[256];
    ...
    word = "WORD1";
    The code above creates an array called word that exists at a specific unchangeable address on the stack. The assignment line where the problem exists needs to be looked at from a certain context. The use of the identifier word here is an address, the address of the array on the stack. The string literal "WORD1" is also an address in this context, the address where the string literal exists in memory. You can't reassign the address of an array to point to something else. The array's address is not an l-value (something that can be assigned to). If word were a pointer, you could do what you are attempting because a pointer is an r-value and therefore assignable.

    Incidentally, the rest of the code bits that figure out the length of the word and then assigns the null terminating character is completely unnecessary and useless. For strlen to work at all, the string you are attempting to get the length of needs to be null terminated, strlen cannot work without it. The character at the location you are assigning null to will already have a null at that exact spot.

    Also, note that these situations are different:
    Code:
    char word1[256] = "Hello";  // This is initialization of an array and is allowed
    char word2[256];
    char * word3;
    word2 = "World";  // This is assignment to an array and is not allowed
    word3 = "Happy";  // This is assignment to a pointer and is allowed though technically, word3 should be const char*
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ady_ltp92 View Post
    Please help me!
    OK... a number of issues ...

    1) Turbo C is TOTALLY outdated. Unless you are running MS-DOS and some version of windows before Win 2000 (also totally outdated) you need to upgrade that compiler, like, yesterday. Google "Pelles C"... it's a nice modern compiler.

    2) C is a programming language with no knowledge of strings. In C strings are arrays of characters with nulls at the end. This means you cannot do things like string=string or string+string... it doesn't know how.

    3) and "get_random_number_function()" is not a function call... it's an array of characters. Plus there is no evidence of you even trying to implement it as a function call in your code. Random numbers are easy... use rand() from the standard C library.

    4) It appears your program is missing the int main (void) entry point and thus won't compile or run anyway.

    To sum up...
    A) Get rid of that nasty compiler and get onto something proper.
    B) You need to sit down with books and/or tutorials and actually learn C... page by page, exercise by exercise.
    Last edited by CommonTater; 05-19-2011 at 08:53 AM.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    2
    I know that Turbo C++ is very old but I'm using it since I need some functions from conio.h and graphics.h that are included only on a Borland compiler.
    I guess I should have copied the entire code:


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <conio.h>
    #include <graphics.h>
    #include <time.h>
    
    
    
    void initializare(int life,char *necunoscut)
    {
    //Print info
      clrscr();
      gotoxy(1,1);
      printf("Hangman v. 1.0   -   Joc creat in C\n\n");
      printf("Type the correct letter to complete the word:\n\n");
      printf("Lifes: %d\n\n",life);
      printf(necunoscut);printf("\n\n");
    
    }
    
    
    //Function that returns a random number
    int nr_random(void)
    {
     return 1+ rand()%6;
    }
    
    
    
    //Find the numbers of X charecters from the word; used to update life
    int charcnt(char *ptr, char c, int size)
    {
    int count = 0, i; /*contor*/
    
    for(i=0;i<size;i++){
    			  if (c==ptr[i]) count++;
    			  }
    return count;
    }
    
    
    
    
    
    int main()
    {
      char *word="R O M A N I A";
      char letter;
      char necunoscut[256];
      int lenght_word;
      int i,j,l,m; // variabile contor
      int x,y; // cursor's position
      unsigned int random_number;
      int no_lines=0, life=6,end=1;
    
    
    
      /*Choose the word*/
      srand((unsigned)time(NULL));
      random_number=nr_random();
    
    printf("%d", random_number); //I checked and it brings a different number between 0-6 each time.
    
      switch(random_number)
      {
      case '0':   word="A L B A N I A";
    	    break;
      case '1':   word="F R A N C E";
    	    break;
      case '2':   word="R O M A N I A";
    	    break;
      case '3':   word="C A N A D A";
    	    break;
      case '4':   word="H U N G A R Y";
    	    break;
      case '5':   word="S P A I N";
    	    break;
      case '6':   word="B U L G A R I A";
    	    break;
    
      }
    
      lenght_word=strlen(word);
      necunoscut[lenght_word+1]='\0';
    
      //Find the number of '_' that need to be printed, depending on the size of the word
      for(j=0;j<=lenght_word;j+=2){
    				  necunoscut[j]='_';
    				  necunoscut[j+1]=' ';
    				  }
      necunoscut[lenght_word]='\0';
    
      //Print initial info
      printf("Hangman v. 1.0   -   Joc creat in C\n\n");
      printf("Type the correct letter to complete the word:\n\n");
      printf("Lifes: %d\n\n",life, " ");
      printf(necunoscut);printf("\n\n");
    
    
    
      //Testing if the word has been found
      while (necunoscut!=word)
      {
      printf("Letter typed:");letter=getch();
      letter=toupper(letter);
      //Check if the character is a letter
      while ((letter!='A')&&(letter!='B')&&(letter!='C')&&(letter!='D')&&(letter!='E')&&(letter!='F')&&(letter!='G')&&(letter!='H')&&
    	 (letter!='I')&&(letter!='J')&&(letter!='K')&&(letter!='L')&&(letter!='M')&&(letter!='N')&&(letter!='O')&&(letter!='P')&&
    	 (letter!='Q')&&(letter!='R')&&(letter!='S')&&(letter!='T')&&(letter!='U')&&(letter!='V')&&(letter!='W')&&(letter!='X')&&
    	 (letter!='Y')&&(letter!='Z')) {
    				       //If user typed a character different from a letter:
    				       nr_lines=0;
    				       for(l=0;l<=lenght_word;l++)
    				       {
    				       if (necunoscut[l]=='_') nr_lines=nr_lines+1;
    				       }
    				       if(nr_lines==0) break;
    				       if (life==0) break;
    				       putch(7);
    				       delline();
    				       y=wherey();
    				       gotoxy(1,y);
    				       printf("Letter typed: "); letter=getch();
    				       letter=toupper(letter);
    				       }
    
    
    	  //If the character is a letter
    	  for(i=0;i<=lenght_word;i++){
    	                                            if (word[i]==letter) necunoscut[i]=letter;
    			            }
    	 if((charcnt(necunoscut,letter,lenght_word))==0) life=life-1;
    
    
           //Deletes the last two line; used to update the console
    	 initializare(life,necunoscut);
    	 nr_lines=0;
    	 for(l=0;l<=lenght_word;l++)
    	 {
    	 if (necunoscut[l]=='_') nr_lines=nr_lines+1;
    	 }
    	if (nr_lines==0) break;
    	if (life==0) {
    		       end=0;
    		       break;
    		       }
    
      }
    
      //End of game
      if (end==0) printf("\n\nGAME OVER!\n");
      if (end==1) printf("\n\nYOU WON!\n");
    
      system("PAUSE");
      return 0;
    }

    The problem is that each time the word is "R O M A N I A", nomatter the value of random_number. Could you give me some advice?
    Last edited by ady_ltp92; 05-20-2011 at 04:00 AM.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Add some diagnostic prints... take note of the values coming back from your random number generator... if they're outside the range of 0 to 6 your switch statement does nothing.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I guess you need to do some more reading (or asking) to realise that some crappy 20+ year old compiler is not the only way to read a key press without waiting, or to bash a few pixels onto the screen.

    Everyone else manages this, all you need to do is join the rest of us in this millenium.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Actually his range isn't 0 to 6, but 48 to 54 (i.e., '0' through '6').

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I would just use a table rather than switch.

    Code:
    static const char *tab[] = { "1st" ,"2nd", ... } ; 
    
    if( isdigit(rand_no) && range_ok(rand_no) ) {
       p = tab[ rand_no - '0' ];
       // or strcpy(foo,tabl[rand_no - '0' ]); depend on use

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lvalue required error
    By Dink87522 in forum C Programming
    Replies: 4
    Last Post: 10-04-2009, 08:34 PM
  2. Lvalue required
    By Bladactania in forum C Programming
    Replies: 2
    Last Post: 03-17-2009, 07:49 AM
  3. Lvalue required error
    By eklavya8 in forum C Programming
    Replies: 5
    Last Post: 01-03-2009, 04:47 PM
  4. Lvalue required in function main
    By arjay in forum C Programming
    Replies: 9
    Last Post: 10-02-2008, 02:21 AM
  5. Lvalue required, eh..
    By Linette in forum C++ Programming
    Replies: 13
    Last Post: 03-02-2002, 08:23 PM

Tags for this Thread