Lvalue required error

This is a discussion on Lvalue required error within the C Programming forums, part of the General Programming Boards category; I am trying to run the simple program below and are receiving the errors "Lvalue required in function main()" when ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    7

    Lvalue required error

    I am trying to run the simple program below and are receiving the errors "Lvalue required in function main()" when I am trying to reassign the variable name to something else. Why and how would I go about this correctly?

    Code:
    #include <stdio.h>
    
    int main()
    {
    
    char name[100];
    int i = 0;
    
    printf("Please enter you're name \n");
    printf(">>>");
    scanf("%s", name);
    
    if (name == "John") {
    name = "George W. Bush";
    }
    
    if (name == "Doogie"){
    name = "Clifford the big red dog";
    }
    
    for (i = 0; i < 1000; i++){
    printf("Welcome %s! \n", name);
    }
    
    return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,914
    name is an array, thus you cannot assign to it (although you can assign to elements of name).

    Also, you should not be using == to compare name with various string literals. Use strcmp() instead.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,782
    String is not a type in C, it is an array. An array of characters.
    To assign a name to a char array, use strcpy.
    To compare two strings, use strcmp. Trying to compare two arrays will never get you the result you want.
    Also read SourceForge.net: Scanf woes - cpwiki
    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
    Sep 2009
    Posts
    7
    Thanks for the tips with the strcpy and strcmp functions. I got the below working to some limited extent. How would I go about converting the user input to lowercase (i.e. if they enter john instead of John it obviously won't work). Also how do you generate a random number in C?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    
    char name[100];
    char name2[100];
    
    printf("Please enter you're name \n");
    printf(">>>");
    scanf("%s", name);
    
    if (strcmp (name, "John") == 0){
    strcpy (name2, "George W. Bush");
    }
    
    else if (strcmp (name, "Doogie") == 0){
    strcpy (name2, "Clifford the big red dog");
    }
    
    else
    (strcpy (name2, name));
    
    for (i = 0; i < 10; i++){
    printf("Don't be an EMO %s! \n", name2);
    }
    
    return 0;
    }

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by Dink87522 View Post
    How would I go about converting the user input to lowercase (i.e. if they enter john instead of John it obviously won't work)
    Look up toupper and tolower which are found in <ctype.h>. Or, if don't care about portability, you could use something like strcasecmp (case insensitive strcmp for Linux, FreeBSD, etc.), or whatever it is for Windows. I think it might be stricmp.

    Quote Originally Posted by Dink87522 View Post
    Also how do you generate a random number in C?
    See here and here.
    Last edited by kermit; 10-04-2009 at 08:49 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21