Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Help Problem with Lvalue required

Reply
Thread Tools

Help Problem with Lvalue required

 
 
Maurizio Porcu
Guest
Posts: n/a
 
      10-03-2005
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[i]; //error
pat=articoli[1].Nome; //error
}
}



 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      10-03-2005
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[i]; //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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-03-2005
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[i]; //error
> pat=articoli[1].Nome; //error


Arrays are not assignable in C++

> }
> }


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

V
 
Reply With Quote
 
Maurizio Porcu
Guest
Posts: n/a
 
      10-03-2005
give me recommend pelase
i student and have need to solve this problem

"Victor Bazarov" <(E-Mail Removed)> ha scritto nel messaggio
newsTf0f.37602$(E-Mail Removed)01.us.t o.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[i]; //error
> > pat=articoli[1].Nome; //error

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

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



 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      10-03-2005
Maurizio Porcu wrote:
> give me recommend pelase
> i student and have need to solve this problem


Don't top-post.

> "Victor Bazarov" <(E-Mail Removed)> ha scritto nel messaggio
> newsTf0f.37602$(E-Mail Removed)01.us.t o.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[i]; //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
 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      10-03-2005
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

 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      10-03-2005
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

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      10-03-2005
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
 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      10-03-2005

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

 
Reply With Quote
 
Aleksey Loginov
Guest
Posts: n/a
 
      10-04-2005

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 );

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using a method as an lvalue and "invalid lvalue in assignment"compilation error markryde@gmail.com C Programming 11 09-22-2008 10:42 AM
operators requiring lvalue/rvalue operands and resulting in rvalue/lvalue Kavya C Programming 9 10-28-2006 01:45 AM
Lvalue Required(wierd)... wessoo C Programming 13 06-26-2004 04:43 PM
Lvalue Required......Why? Sai Krishna M C Programming 5 11-24-2003 08:15 PM
Perl hangs when returning lvalue closure from another lvalue closure Julian Mehnle Perl Misc 0 07-17-2003 03:13 PM



Advertisments