Thread: Lvalue error? Whats that?

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    35

    Lvalue error? Whats that?

    Hello there again. My programm runs swimmingly up until my bubble sort. The error I am getting is "Lvlalue required in function main" and I'm not to sure what that means. It is right at the bottom. I am also not to sure if I have the number of times it sorts correct. (It needs to loop N times, where N is the number of words the user wants to enter)

    (commenting out the bubble sort makes the program compilable, if you need to see what it should do).

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    char **p;
    char size[20];
    char buff[21];
    char temp[21];
    int N;
    int j,i,k,l,m,d,s,a,b;
    
    int main()
    {
    	do{
    		d=0;
    		printf("How many words would you like: ");
    		fflush(stdout);
    		for(j=0; j<20; j++)
    		 {
    		      size[j]=fgetc(stdin);
    		      if (size[j]=='\n')
    		      {
    		        size[j]='\0';
    		        break;
    		      }
    		 }/* end j-loop */
    			if (j==0){
    				printf("That input is empty.\n");
    				d=1;
    
    			}
    		    if (j==20) {
    		      printf("That input is too long.\n");
    		      while(fgetc(stdin)!='\n');
    		      d=1;
    		  }
    }while(d!=0);
    
    
    	printf("You want %d words.\n", atoi(size));
    
    	N=atoi(size);
    	p=(char**)malloc(N*sizeof(char*));
    		if (p==NULL)
    		{
    			printf("Memory error");
    			exit(0);
    		}
    	for (i=0; i<N; i++)
    		p[i]=(char*)malloc(21*sizeof(char));
    		if (p[i]==NULL)
    		{
    			printf("Memory error");
    			exit(0);
    		}
    	printf("Do not enter more than 20 characters.\n");
    
    	for(k=0; k<N; k++)
    	{
    		A:printf("Enter a word: ");
    		fflush(stdout);
    		for(m=0; m<21; m++)
    		{
    		      buff[m]=fgetc(stdin);
    		      if (buff[m]=='\n')
    		      {
    		        buff[m]='\0';
    		        break;
    		      }
    		 }
    
    		    if (m>=21)
    		    {
    		    	printf("Word is more than 20 characters.\n");
    		      	while(fgetc(stdin)!='\n');
          			goto A;
    			}
    			if (m==0)
    			{
    				printf("That input is empty\n");
    				goto A;
    			}
    		strcpy(p[k],buff);
    	}
    	printf("\n");
    	printf("Your unsorted list of Names:\n");
    	k=0;
    	for(k=0; k<N; k++)
    		printf("%s\n",p[k]);
    
    
    	for (a=N; a>0; a--)
    	{
    		for (b=0; b<a; b++)
    		{
    			if(strcmp(p[b],p[b+1])>0)
    			{
    				temp=p[b];
    				p[b]=p[b+1];
    				p[b+1]=temp;
    			}
    		}
    
    	}
    	printf("\n");
    	printf("Your sorted list of Names:\n");
    	k=0;
    	for(k=0; k<N; k++)
    		printf("%s\n",p[k]);
    
    
    
    return 0;
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    char temp[21];
    
    ...
    
    temp=p[b];
    Can't do that... use strcpy or something similar.
    "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
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do not use goto.
    Fix your indentation.
    Give variables (and labels, if you have to use them) a proper name.
    And don't hide labels in obscure places. Better to avoid them altogether.
    And what line is the error on?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    Code:
    	for (a=N; a>0; a--)
    	{
    		for (b=0; b<a; b++)
    		{
    			if (strcmp(p[j],p[j+1])>0)
    			{
    			    strcpy(temp,p[j]);
    			    strcpy(p[j],p[j+1]);
       			    strcpy(p[j+1],temp);
    			}
    		}
    
    	}
    This gives me a lot of errors it says my p[j] is an integer, and its recieving char *, but only wants a char. Plus my prof said doing (what I have posted above) is a simpler and easier way when dealing with pointers.

    Quote Originally Posted by Elysia View Post
    Do not use goto.
    Fix your indentation.
    Give variables (and labels, if you have to use them) a proper name.
    And don't hide labels in obscure places. Better to avoid them altogether.
    And what line is the error on?
    1. Why don't use goto?
    2. Could you show me where my indentation is wrong and what it should look like

    The error is on like 98
    Last edited by Iconate; 03-24-2008 at 12:15 PM. Reason: Reply to abover poster

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Oh yes, and don't use global variables.
    But the new code works.
    Further,

    On
    buff[m]=(char)fgetc(stdin);
    Use a cast, because fgetc returns int, not char.

    And... variables "s" and "l" are never used. Again, rename them to better descriptions.
    And I suggest you up compiler warnings to max (level 4).
    Last edited by Elysia; 03-24-2008 at 12:18 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by Iconate
    2. Could you show me where my indentation is wrong and what it should look like
    Just as an example:
    Code:
    	do{
    		d=0;
    		printf("How many words would you like: ");
    		fflush(stdout);
    		for(j=0; j<20; j++)
    		 {
    		      size[j]=fgetc(stdin);
    		      if (size[j]=='\n')
    		      {
    		        size[j]='\0';
    		        break;
    		      }
    		 }/* end j-loop */
    			if (j==0){
    				printf("That input is empty.\n");
    				d=1;
    
    			}
    		    if (j==20) {
    		      printf("That input is too long.\n");
    		      while(fgetc(stdin)!='\n');
    		      d=1;
    		  }
    }while(d!=0);
    Should be more like:
    Code:
    do{
        d=0;
        printf("How many words would you like: ");
        fflush(stdout);
        for(j=0; j<20; j++)
        {
            size[j]=fgetc(stdin);
            if (size[j]=='\n')
            {
                size[j]='\0';
                break;
            }
        }/* end j-loop */
        if (j==0){
            printf("That input is empty.\n");
            d=1;
        }
        if (j==20) {
            printf("That input is too long.\n");
            while(fgetc(stdin)!='\n');
            d=1;
        }
    }while(d!=0);
    There is a "preview" post capability of these boards most people seem to ignore. Preview the post to see what it's going to look like and try your best to pretty it up before actually posting. Problems like these are typically caused by a mixing of tabs/spaces in the source code that messes up the formatting when dumping into the forum when using copy/paste.
    "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

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Iconate View Post
    1. Why don't use goto?
    It makes obscure code; hard to read. Goto can jump all over the place and is therefore loathed by many. There is almost nothing that goto is good for, so avoid it.

    2. Could you show me where my indentation is wrong and what it should look like
    Glad you asked.
    You can try to make it look more like the example @ http://cpwiki.sf.net/Indentation
    Or try reading the in-depth guide at http://cpwiki.sf.net/User:Elysia/Indentation
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    Quote Originally Posted by Elysia View Post
    Glad you asked.
    You can try to make it look more like the example @ http://cpwiki.sf.net/Indentation
    Or try reading the in-depth guide at http://cpwiki.sf.net/User:Elysia/Indentation
    awesome, im taking a look at these now, thanks so much for your help. Also, for some reason in my program the value "2" for N doesn't work, but every other number does...I get a memory allocation error

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    if (size[j]=='\n')
    {
            size[j]='\0';
            break;
    }
    ...
    if (j==0){
       printf("That input is empty.\n");
       d=1;
    }
    you should be consistent in lacing brekets - do not mix two types of indentation
    or use
    Code:
    if()
    {
       ...
    }
    or
    Code:
    if(){
       ...
    }
    but do it for every code block the same way
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    While it's up you, I still prefer the
    Code:
    if()
    {
       ...
    }
    way because it's so much easier to see where they start and end.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    Is it really that bad to mix? If it is a really big block with ifs, whiles and fors inside, then I use
    Code:
    if(whatever)
    {
       bla bla...
    }
    and if it is a small block with nothing nesting inside I use
    Code:
    if(){
        ....
    }
    So my code almost always looks like this:
    Code:
    while(whatever)
    {
        if(something)
        {
             blaablaalbaa;
             blaaa blaaa;
    
             if(something){
                 code code;
                 code code;
             }
    
             etc. etc.;
        }
    }
    what do you think? Tell me if its bad because I'm also trying to get rid of my other bad habits:
    Code:
    thing1,thing2,thing3 instead of thing1, thing2, thing3
    a=b+2 instead of a = b + 2 (or better) a = b+2
    if((ch=fgetc(file))!=EOF) instead of if( (ch = fgetch(file)) != EOF)

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Huh? Your "bad habits" should be the other way around. The more readable, the better.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Tell me if its bad
    It is bad... when you just scan code for possible errors - you do not count how many lines are in the block... you look for pairs of brackets - if opening one is missing - you need to stop scanning and start looking for it. So you need to be consistent in your style...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #14
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    Quote Originally Posted by Elysia View Post
    Huh? Your "bad habits" should be the other way around. The more readable, the better.
    I mean... my bad habit is the first one... and I should change it for the second one.
    I never separate commas
    Code:
    thing1,thing2,thing3
    I never separate assignments
    Code:
    this=that*(2+(something/5))
    etc. etc. All the code I've seen always separates that kind of stuff.

  15. #15
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    Quote Originally Posted by vart View Post
    It is bad... when you just scan code for possible errors - you do not count how many lines are in the block... you look for pairs of brackets - if opening one is missing - you need to stop scanning and start looking for it. So you need to be consistent in your style...
    hmmmmm..... I think I'd be better off with
    Code:
    if()
    {
    }
    because when the blocks get way big it is a lot easier. But I use Notepad++ which always throws a neat line between indents, so
    Code:
    if(){
    }
    is easy to find too. But well, for other editors and plain text I think the first one is a better choice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. lvalue rvalue discussion
    By George2 in forum C++ Programming
    Replies: 18
    Last Post: 03-04-2008, 05:48 AM
  2. A non-const reference may only be bound to an lvalue?
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2007, 06:42 PM
  3. how to use reference for lvalue
    By jabka in forum C++ Programming
    Replies: 9
    Last Post: 04-22-2007, 02:08 PM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  5. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM