Help Problem with Lvalue required

M

Maurizio Porcu

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
}
}
 
M

mlimber

Maurizio said:
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 said:
struct Pate {
char Nome[20];
};

How about:

typedef char Nome[20];

or better:

#include said:

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


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 said:

return 0;

Cheers! --M
 
V

Victor Bazarov

Maurizio said:
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
 
M

Maurizio Porcu

give me recommend pelase
i student and have need to solve this problem

Victor Bazarov said:
Maurizio said:
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
 
V

Victor Bazarov

Maurizio said:
give me recommend pelase
i student and have need to solve this problem

Don't top-post.
Maurizio said:
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);

V
 
M

mlimber

Maurizio said:
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
 
M

mlimber

Victor said:
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
 
V

Victor Bazarov

mlimber said:
Victor said:
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
 
M

mlimber

Victor said:
mlimber said:
Victor said:
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
 
A

Aleksey Loginov

Victor said:
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 );
 
M

Maurizio Porcu

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
 
M

mlimber

Maurizio said:
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
 
M

Maurizio Porcu

yes but i use borland c++ and i have to write in c++
and i have problem with this source






mlimber said:
Maurizio said:
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
 
M

mlimber

Maurizio said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top