Help Problem with Lvalue required

Discussion in 'C++' started by Maurizio Porcu, Oct 3, 2005.

  1. Hello i have this my code but have problem with char=char

    # include <conio.h>
    # include <stdio.h>
    struct Pate {
    char Nome[20];
    };

    main()
    {
    char lettere[3] = { 'A','P','Z'};
    Pate articoli[3]={{"gli"},{"un"},{"una"}};
    Pate
    preposizioni[10]={{"di"},{"a"},{"da"},{"in"},{"con"},{"su"},{"per"},{"tra"},
    {"fra"}};

    char text[5000],pat[100], te[1];
    int no,count,pos[20],i;
    clrscr();
    cout<<"\n\nInsert text:";
    gets(text);

    cout<<"Insert one string:";
    // gets(pat);
    for (i=0;i<3;i++){
    //pat=lettere; //error
    pat=articoli[1].Nome; //error
    }
    }
     
    Maurizio Porcu, Oct 3, 2005
    #1
    1. Advertising

  2. Maurizio Porcu

    mlimber Guest

    Maurizio Porcu wrote:
    > Hello i have this my code but have problem with char=char


    No, you have a problem with char[] = char[]. You'll probably find it
    easier to work with std::string.

    >
    > # include <conio.h>
    > # include <stdio.h>


    conio.h is not standard. stdio.h is for C. C++ should use <cstdio> or
    better <iostream>.

    > struct Pate {
    > char Nome[20];
    > };


    How about:

    typedef char Nome[20];

    or better:

    #include <string>

    >
    > main()


    int main()

    > {
    > char lettere[3] = { 'A','P','Z'};
    > Pate articoli[3]={{"gli"},{"un"},{"una"}};
    > Pate
    > preposizioni[10]={{"di"},{"a"},{"da"},{"in"},{"con"},{"su"},{"per"},{"tra"},
    > {"fra"}};


    Make these all const.

    >
    > char text[5000],pat[100], te[1];


    An array of one?

    > int no,count,pos[20],i;
    > clrscr();


    Non-standard.

    > cout<<"\n\nInsert text:";
    > gets(text);


    You're mixing cout (when you didn't actually include <iostream>!) and
    gets? Prefer cin to gets.

    >
    > cout<<"Insert one string:";
    > // gets(pat);
    > for (i=0;i<3;i++){
    > //pat=lettere; //error
    > pat=articoli[1].Nome; //error


    Use std::string to get this type of convenience. Otherwise, you'll need
    to include <string.h> or <cstring> to get strcpy.

    > }


    return 0;

    > }


    Cheers! --M
     
    mlimber, Oct 3, 2005
    #2
    1. Advertising

  3. Maurizio Porcu wrote:
    > Hello i have this my code but have problem with char=char


    No, you don't. You have a problem with char[]=char[].

    > # include <conio.h>
    > # include <stdio.h>
    > struct Pate {
    > char Nome[20];
    > };
    >
    > main()


    int main()

    > {
    > char lettere[3] = { 'A','P','Z'};
    > Pate articoli[3]={{"gli"},{"un"},{"una"}};
    > Pate
    > preposizioni[10]={{"di"},{"a"},{"da"},{"in"},{"con"},{"su"},{"per"},{"tra"},
    > {"fra"}};
    >
    > char text[5000],pat[100], te[1];
    > int no,count,pos[20],i;
    > clrscr();
    > cout<<"\n\nInsert text:";
    > gets(text);
    >
    > cout<<"Insert one string:";
    > // gets(pat);
    > for (i=0;i<3;i++){
    > //pat=lettere; //error
    > pat=articoli[1].Nome; //error


    Arrays are not assignable in C++

    > }
    > }


    Don't use character arrays, use 'std::string'.

    V
     
    Victor Bazarov, Oct 3, 2005
    #3
  4. give me recommend pelase
    i student and have need to solve this problem

    "Victor Bazarov" <> ha scritto nel messaggio
    news:eek:Tf0f.37602$01.us.to.verio.net...
    > Maurizio Porcu wrote:
    > > Hello i have this my code but have problem with char=char

    >
    > No, you don't. You have a problem with char[]=char[].
    >
    > > # include <conio.h>
    > > # include <stdio.h>
    > > struct Pate {
    > > char Nome[20];
    > > };
    > >
    > > main()

    >
    > int main()
    >
    > > {
    > > char lettere[3] = { 'A','P','Z'};
    > > Pate articoli[3]={{"gli"},{"un"},{"una"}};
    > > Pate
    > >

    preposizioni[10]={{"di"},{"a"},{"da"},{"in"},{"con"},{"su"},{"per"},{"tra"},
    > > {"fra"}};
    > >
    > > char text[5000],pat[100], te[1];
    > > int no,count,pos[20],i;
    > > clrscr();
    > > cout<<"\n\nInsert text:";
    > > gets(text);
    > >
    > > cout<<"Insert one string:";
    > > // gets(pat);
    > > for (i=0;i<3;i++){
    > > //pat=lettere; //error
    > > pat=articoli[1].Nome; //error

    >
    > Arrays are not assignable in C++
    >
    > > }
    > > }

    >
    > Don't use character arrays, use 'std::string'.
    >
    > V
     
    Maurizio Porcu, Oct 3, 2005
    #4
  5. Maurizio Porcu wrote:
    > give me recommend pelase
    > i student and have need to solve this problem


    Don't top-post.

    > "Victor Bazarov" <> ha scritto nel messaggio
    > news:eek:Tf0f.37602$01.us.to.verio.net...
    >
    >>Maurizio Porcu wrote:
    >>
    >>>Hello i have this my code but have problem with char=char

    >>
    >>No, you don't. You have a problem with char[]=char[].
    >>
    >>
    >>># include <conio.h>
    >>># include <stdio.h>
    >>>struct Pate {
    >>> char Nome[20];
    >>>};
    >>>
    >>>main()

    >>
    >>int main()
    >>
    >>
    >>>{
    >>> char lettere[3] = { 'A','P','Z'};
    >>> Pate articoli[3]={{"gli"},{"un"},{"una"}};
    >>> Pate
    >>>

    >
    > preposizioni[10]={{"di"},{"a"},{"da"},{"in"},{"con"},{"su"},{"per"},{"tra"},
    >
    >>>{"fra"}};
    >>>
    >>> char text[5000],pat[100], te[1];
    >>> int no,count,pos[20],i;
    >>> clrscr();
    >>> cout<<"\n\nInsert text:";
    >>> gets(text);
    >>>
    >>> cout<<"Insert one string:";
    >>>// gets(pat);
    >>>for (i=0;i<3;i++){
    >>>//pat=lettere; //error
    >>>pat=articoli[1].Nome; //error


    If you just want to transfer all 20 characters from 'articoli[1].Nome'
    to 'pat', use 'memcpy':

    memcpy(pat, articoli[1].Nome, 20);

    >>
    >>Arrays are not assignable in C++
    >>
    >>
    >>>}
    >>>}

    >>
    >>Don't use character arrays, use 'std::string'.
    >>
    >>V


    V
     
    Victor Bazarov, Oct 3, 2005
    #5
  6. Maurizio Porcu

    mlimber Guest

    Maurizio Porcu wrote:
    > give me recommend pelase
    > i student and have need to solve this problem


    Read in your C++ book about using std::string. It works something like
    this:

    #include <iostream>
    #include <string>
    using namespace std;

    int main()
    {
    cout << "Enter your name: \n";
    string name;
    getline( cin, name );

    if( name == "mlimber" ) // compare two strings
    {
    name = "chump"; // Assign a new string
    }

    // A constant string
    const string hello = "Ciao, ";

    // display the result
    cout << hello << name << '.' << endl;

    return 0;
    }

    Cheers! --M
     
    mlimber, Oct 3, 2005
    #6
  7. Maurizio Porcu

    mlimber Guest

    Victor Bazarov wrote:
    > If you just want to transfer all 20 characters from 'articoli[1].Nome'
    > to 'pat', use 'memcpy':
    >
    > memcpy(pat, articoli[1].Nome, 20);


    Better to use strcpy (or strncpy) with strings.

    Cheers! --M
     
    mlimber, Oct 3, 2005
    #7
  8. mlimber wrote:
    > Victor Bazarov wrote:
    >
    >>If you just want to transfer all 20 characters from 'articoli[1].Nome'
    >>to 'pat', use 'memcpy':
    >>
    >> memcpy(pat, articoli[1].Nome, 20);

    >
    >
    > Better to use strcpy (or strncpy) with strings.


    No.

    'strcpy' can "accidentally" go beyond 20 (if the terminator is missing
    in the first 20), and 'strncpy' will append zero chars if it encounters
    the zero character before copying 20 chars. So, neither will do what is
    needed.

    Given

    char five[5] = { '1','2','3','4','5' };
    char seven[7] = "ab\0cd\0";
    char sevenmore[7];

    try

    strcpy(sevenmore, five); // BOOM! Undefined behaviour

    or

    strncpy(sevenmore, seven); // what's the result?

    V
     
    Victor Bazarov, Oct 3, 2005
    #8
  9. Maurizio Porcu

    mlimber Guest

    Victor Bazarov wrote:
    > mlimber wrote:
    > > Victor Bazarov wrote:
    > >
    > >>If you just want to transfer all 20 characters from 'articoli[1].Nome'
    > >>to 'pat', use 'memcpy':
    > >>
    > >> memcpy(pat, articoli[1].Nome, 20);

    > >
    > >
    > > Better to use strcpy (or strncpy) with strings.

    >
    > No.
    >
    > 'strcpy' can "accidentally" go beyond 20 (if the terminator is missing
    > in the first 20), and 'strncpy' will append zero chars if it encounters
    > the zero character before copying 20 chars. So, neither will do what is
    > needed.
    >
    > Given
    >
    > char five[5] = { '1','2','3','4','5' };
    > char seven[7] = "ab\0cd\0";
    > char sevenmore[7];
    >
    > try
    >
    > strcpy(sevenmore, five); // BOOM! Undefined behaviour
    >
    > or
    >
    > strncpy(sevenmore, seven); // what's the result?
    >
    > V


    Good points, although the comp.lang.c FAQ advocates using strcpy (8.3
    and the second paragraph of 13.2). In any case, these are just more
    reasons to prefer std::string.

    Cheers! --M
     
    mlimber, Oct 3, 2005
    #9
  10. Victor Bazarov wrote:
    > If you just want to transfer all 20 characters from 'articoli[1].Nome'
    > to 'pat', use 'memcpy':
    >
    > memcpy(pat, articoli[1].Nome, 20);
    >


    or even std::copy ( articoli[1].Nome, articoli[1].Nome+20, pat );
     
    Aleksey Loginov, Oct 4, 2005
    #10
  11. i has used the memcpy(pat, articoli[0].Nome, 3);
    but if use pat and sent this in a function not go
    if i use pat with gets(pat) the function go well


    "Maurizio Porcu" <> ha scritto nel messaggio
    news:4341876e$0$8489$...
    > Hello i have this my code but have problem with char=char
    >
    > # include <conio.h>
    > # include <stdio.h>
    > struct Pate {
    > char Nome[20];
    > };
    >
    > main()
    > {
    > char lettere[3] = { 'A','P','Z'};
    > Pate articoli[3]={{"gli"},{"un"},{"una"}};
    > Pate
    >

    preposizioni[10]={{"di"},{"a"},{"da"},{"in"},{"con"},{"su"},{"per"},{"tra"},
    > {"fra"}};
    >
    > char text[5000],pat[100], te[1];
    > int no,count,pos[20],i;
    > clrscr();
    > cout<<"\n\nInsert text:";
    > gets(text);
    >
    > cout<<"Insert one string:";
    > // gets(pat);
    > for (i=0;i<3;i++){
    > //pat=lettere; //error
    > pat=articoli[1].Nome; //error
    > }
    > }
    >
    >
    >
     
    Maurizio Porcu, Oct 4, 2005
    #11
  12. Maurizio Porcu

    mlimber Guest

    Maurizio Porcu wrote:
    > i has used the memcpy(pat, articoli[0].Nome, 3);
    > but if use pat and sent this in a function not go
    > if i use pat with gets(pat) the function go well


    You're posting to the C++ forum, yet you're using C-style techniques.
    You should either post to comp.lang.c or follow our previous suggestion
    to use std::string instead of character arrays.

    Cheers! --M
     
    mlimber, Oct 4, 2005
    #12
  13. yes but i use borland c++ and i have to write in c++
    and i have problem with this source






    "mlimber" <> ha scritto nel messaggio
    news:...
    > Maurizio Porcu wrote:
    > > i has used the memcpy(pat, articoli[0].Nome, 3);
    > > but if use pat and sent this in a function not go
    > > if i use pat with gets(pat) the function go well

    >
    > You're posting to the C++ forum, yet you're using C-style techniques.
    > You should either post to comp.lang.c or follow our previous suggestion
    > to use std::string instead of character arrays.
    >
    > Cheers! --M
    >
     
    Maurizio Porcu, Oct 4, 2005
    #13
  14. Maurizio Porcu

    mlimber Guest

    Maurizio Porcu wrote:
    > yes but i use borland c++ and i have to write in c++
    > and i have problem with this source


    Sure, but C++ is a superset of C (mostly), which means that most C
    programs will build with under C++. In other words, you can use a C++
    compiler to write C programs, and, in fact, that seems to be what you
    are doing. However, the C approach to solving problems often varies
    widely from the C++ apprach because the latter relies on the C++
    standard library (including std::string) and OO abstraction, which are
    not available in C. Since our advice here will be in terms of those
    standard C++ facilities, I'd suggest that if you prefer C techniques
    (even if you're using a C++ compiler), you'll get more helpful advice
    in comp.lang.c.

    Cheers! --M

    PS, It's considered bad manners to put your replies at the top of
    previous posts as you did with this one.
     
    mlimber, Oct 5, 2005
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sai Krishna M

    Lvalue Required......Why?

    Sai Krishna M, Nov 20, 2003, in forum: C Programming
    Replies:
    5
    Views:
    18,505
    Christopher Benson-Manica
    Nov 24, 2003
  2. wessoo

    Lvalue Required(wierd)...

    wessoo, Jun 24, 2004, in forum: C Programming
    Replies:
    13
    Views:
    789
    Martin Ambuhl
    Jun 26, 2004
  3. Kavya
    Replies:
    9
    Views:
    513
    Dik T. Winter
    Oct 28, 2006
  4. Replies:
    11
    Views:
    697
    James Kuyper
    Sep 22, 2008
  5. Julian Mehnle
    Replies:
    0
    Views:
    239
    Julian Mehnle
    Jul 17, 2003
Loading...

Share This Page