Thread: createing my constructer

  1. #1
    Registered User Sekti's Avatar
    Join Date
    Feb 2002
    Posts
    163

    createing my constructer

    ok the class is file in a header file and im makeing the constructor I am trying to make a bunch of strings to equal 0 heres what I have tried:

    file::file()
    {
    file_one = "0";
    }

    trying to compile is says:
    Lvalue required in function file::file()

    so then I tried:
    file::file()
    {
    file_one = '0';
    }

    and it said the same thing I am new to this what is going on?
    +++
    ++
    + Sekti
    ++
    +++

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    What did u declare file_one to be? If you want, you can declare them to be the null terminating character.

    file_one="\0";

  3. #3
    Registered User Sekti's Avatar
    Join Date
    Feb 2002
    Posts
    163

    I did that

    and it still said the same thing it can be \0 or just a 0 or anything like that
    +++
    ++
    + Sekti
    ++
    +++

  4. #4
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    what is file_one... (as golfinguy4 stated)

    show the rest of you code.

    I don't think your problem is there... did you terminate your class with a semicolon? Is your class implementation and declaration in the same file?

    show code... use [ code ] [ /code ] tags...
    Blue

  5. #5
    Registered User Sekti's Avatar
    Join Date
    Feb 2002
    Posts
    163

    ok heres the code

    Heres file.h

    Code:
    #ifndef file_var
    #define file_var
    
    class file
    {
    public:
    file();
    ~file();
    void encrypt ();
    void decrypt ();
    char EncryptChar (char ch);
    void EncryptString (char *str);
    
    protected:
    char file_one[256];
    char file_two[256];  
    char contents[1000];
    char key[5];
    };
    
    #endif
    heres main_01.cpp

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <windows.h>
    #include "file.h"
    
    file::file()
    {
    file_one="\0";
    file_two="\0";
    contents="\0";
    key="\0";
    }
    
    char file::EncryptChar(char ch)
    {
        key[0] = key[1] + key[3];
        key[4] = key[0] * key[2];
        ch = key[4];
        return ch;
    }
    
    void file::EncryptString(char *str)
    {
        while (*str != '\0')   *str = EncryptChar(*str++);
    }
    
    void file::encrypt()
    {
    cout<<"Enter Files Name"<<endl;
    cout<<":";
    cin.getline(file_one, 256, '\n');
    cout<<"Enter File to Save it to"<<endl;
    cout<<":";
    cin.getline(file_two , 256, '\n');
    cout<<"Enter 5 Digit Key"<<endl;
    cout<<":";
    cin.getline(key, 5, '\n');
    
    /* Open File */
    ifstream ein;
    ein.open(file_one);
    
    while(ein.eof() != 1)
    {
    int x = 0;
    x++;
    ein>>contents[x];
    }
    
    /* Encrypt */
    EncryptString(contents);
    
    /* Save */
    ofstream eout;
    eout.open(file_two);
    
    for(int x = 0; x != 1000; x++)
    {
    eout<<contents[x];
    }
    
    eout.close();
    
    /* Finished */
    cout<<"Encryption completed..."<<endl;
    cout<<file_one<<" was encypted to "<<file_two<<endl;
    cout<<"The key is "<<key<<endl;
    
    Sleep(2500);
    
    exit(0);
    
    }
    and when I try to compile it on borland 5.5 this happens:

    ! Error E2277 .\main_01.cpp 11: Lvalue required in function file::file()
    ! Error E2277 .\main_01.cpp 12: Lvalue required in function file::file()
    ! Error E2277 .\main_01.cpp 13: Lvalue required in function file::file()
    ! Error E2277 .\main_01.cpp 14: Lvalue required in function file::file()
    +++
    ++
    + Sekti
    ++
    +++

  6. #6
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    ahhh.. got it...


    They are arrays.

    You need the [] and a reference array point


    Code:
    int i;
    
    for (i=0; i < 256;++i)
    file_one[i]='\0';
    Blue

  7. #7
    Registered User Sekti's Avatar
    Join Date
    Feb 2002
    Posts
    163

    oh man

    no that didnt work what is the Lvalue it still says that
    Last edited by Sekti; 03-09-2002 at 07:16 PM.
    +++
    ++
    + Sekti
    ++
    +++

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >file_one="\0";
    memset ( file_one, 0, 256 );

    And include either string.h or cstring when you work with C strings.

    -Prelude
    My best code is written with the delete key.

  9. #9
    Registered User Sekti's Avatar
    Join Date
    Feb 2002
    Posts
    163

    still...

    doesnt work I have tried what you said I included both of them did the memset thing didnt work I did the for loop thing with and without the memset thing didnt work what is the Lvalue thing if I knew what it was talking about...
    +++
    ++
    + Sekti
    ++
    +++

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    It compiles fine for me when I use memset, here's the constructor code that worked:
    Code:
    file :: file()
    {
      memset(file_one,'\0',256);
      memset(file_two,'\0',256);
      memset(contents,'\0',1000);
      memset(key,'\0',5);
    }
    An Lvalue is a variable or chunk of memory that allows assignment to itself. So you can assign to an integer i, but not to an expression (i + 3). You can't assign a string to an array of char directly except during initialization:
    char string[] = "This is a string"; // This works
    string = "This is another string"; // Bzzzt! Illegal

    -Prelude
    My best code is written with the delete key.

  11. #11
    Registered User Sekti's Avatar
    Join Date
    Feb 2002
    Posts
    163

    oh man

    oh I was doing it wrong thanks so much man!
    +++
    ++
    + Sekti
    ++
    +++

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class constructer question
    By bman1176 in forum C++ Programming
    Replies: 5
    Last Post: 10-30-2002, 04:53 PM
  2. Createing a function with string?
    By elfjuice in forum C++ Programming
    Replies: 7
    Last Post: 06-04-2002, 04:16 PM