Lvalue required, eh..

This is a discussion on Lvalue required, eh.. within the C++ Programming forums, part of the General Programming Boards category; Hi, I have this struct definition in my program: PHP Code: struct qtype  {     char question [ 70 ];     char answer ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    71

    Lvalue required, eh..

    Hi,

    I have this struct definition in my program:

    PHP Code:
    struct qtype {
       
    char question[70];
       
    char answer[4][70];
       
    char rightanswer;
    }; 
    this struct: qtype item[12];
    and this string: char line[70];
    Now, with this code:
    PHP Code:
       ifstream sourcefile ("quizfile.txt");
       
    sourcefile.getline (linesizeof(line) );
       
    item[0].question line
    (on that last line) I'm getting an Lvalue Required error..
    What's wrong?

    Linette

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    > item[0].question = line;

    You either need to use strcpy() for C-style strings, or std::string's if you want to use operator =.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    71

    aah.. thanks

    I wrote this, and it gave me a General Protection Fault error and didn't run..

    strcpy (item[0].question,line);

    What's wrong now?

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Looks okay, can you post more code ?
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    71
    sure. this is all i have so far (not complete).. no pointers either..

    PHP Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <string.h>
    #include <stdio.h>

    struct qtype {
        
    char question[70];
        
    char answer[4][70];
        
    char rightanswer;
    };

    void main () {

        
    ifstream sourcefile ("quizfile.txt");
        
    qtype item[12];
        
    char line[70];
        
    char userans[12];
        
    int c;

        if (
    sourcefile.fail())
            
    cerr << "Error opening quizfile.txt...";
        else {
            
    sourcefile.getline (linesizeof(line) );
            
    strcpy (item[0].question,line);
            
    cout << item[0].question << "\n\n";
        
    /*    for (c=0; c<4; c++) {
                sourcefile.getline (line, sizeof(line) );
                cout << line << endl;
            }
            cout << "\nAnswer: ";
            cin >> userans[0];
        */
         
    }



  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    71
    I'm really stuck, please help!

  7. #7
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Your code looks ok (apart from the void main()). What's the first line of quizfile.txt? What compiler/os are you using?

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    The problem that you talked about isn't in your latter code. Basically, the problem was you were trying to assign to an array, but arrays are not Lvalues. In your latter code, you've replaced
    Code:
    item[0].question = line;
    with
    Code:
    strcpy (item[0].question,line);
    So I'm not sure what your problem exactly is anymore.

    Personally, you'd be a lot better off using STL strings instead of char arrays.
    Callou collei we'll code the way
    Of prime numbers and pings!

  9. #9
    Registered User
    Join Date
    Jan 2002
    Posts
    71
    um, what's wrong with the void main?
    the first line of that file is a question ("1. What is the best way to describe a network?")
    my operating system is windows nt and i'm using turbo c++ compiler version 4.5

    by the way i do get output (the first line) when i run it, but it gives me that general protection fault and doesn't terminate until i use windows task manager or debug->terminate program..


  10. #10
    Registered User
    Join Date
    Jan 2002
    Posts
    71

    no luck..

    I tried changing the size of the string 'line'.. nope..
    tried adding a null character to the end of item[0].question (after copying line into it).. still no change..
    with the change in the size of that string, it displayed the output, and then when i tried to close the window (to terminate the program) it gave me that general protection thing again..

  11. #11
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Does your program still crash if you attempt to get input from cin, rather than the file? Have you tried it on a different compiler?

  12. #12
    Registered User
    Join Date
    Jan 2002
    Posts
    71
    thanks, that made me go and try it with this:
    PHP Code:
    cin >> line;
    strcpy (item[0].questionline); 
    and yes it crashed. so it's on the strcpy line.
    once again line is declared as: char line[70];
    and item as: qtype item[12];
    with qtype defined as:
    PHP Code:
    struct qtype {
       
    char question[70];
       
    char answer[4][70];
       
    char rightanswer;
    }; 
    do i have to use pointers with strcpy? the general protection thing has always happened to me because of pointers..



  13. #13
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >do i have to use pointers with strcpy?

    The name of an array is a pointer (of sorts). Have you tried a different compiler?

  14. #14
    Registered User
    Join Date
    Jan 2002
    Posts
    71
    No, I haven't tried a different compiler (don't have another one at home).. I guess others who replied might've had a different one..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. lvalue required as increment operand compile error
    By canadatom in forum C Programming
    Replies: 8
    Last Post: 06-13-2009, 12:49 PM
  2. Lvalue required
    By Bladactania in forum C Programming
    Replies: 2
    Last Post: 03-17-2009, 08:49 AM
  3. Lvalue required error
    By eklavya8 in forum C Programming
    Replies: 5
    Last Post: 01-03-2009, 04:47 PM
  4. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  5. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 05:33 PM

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