| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Maurizio Porcu" |
| Date: |
03 Oct 2005 02:34:19 PM |
| Object: |
Help Problem with Lvalue required |
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
}
}
.
|
|
| User: "Victor Bazarov" |
|
| Title: Re: Help Problem with Lvalue required |
03 Oct 2005 02:45:15 PM |
|
|
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
.
|
|
|
| User: "Maurizio Porcu" |
|
| Title: Re: Help Problem with Lvalue required |
03 Oct 2005 02:57:27 PM |
|
|
give me recommend pelase
i student and have need to solve this problem
"Victor Bazarov" <v.Abazarov@comAcast.net> ha scritto nel messaggio
news:oTf0f.37602$Tf5.27413@newsread1.mlpsca01.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[i]; //error
pat=articoli[1].Nome; //error
Arrays are not assignable in C++
}
}
Don't use character arrays, use 'std::string'.
V
.
|
|
|
| User: "Victor Bazarov" |
|
| Title: Re: Help Problem with Lvalue required |
03 Oct 2005 03:17:28 PM |
|
|
Maurizio Porcu wrote:
give me recommend pelase
i student and have need to solve this problem
Don't top-post.
"Victor Bazarov" <v.Abazarov@comAcast.net> ha scritto nel messaggio
news:oTf0f.37602$Tf5.27413@newsread1.mlpsca01.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[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
.
|
|
|
| User: "Aleksey Loginov" |
|
| Title: Re: Help Problem with Lvalue required |
04 Oct 2005 12:11:42 AM |
|
|
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 );
.
|
|
|
|
| User: "mlimber" |
|
| Title: Re: Help Problem with Lvalue required |
03 Oct 2005 03:31:41 PM |
|
|
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
.
|
|
|
| User: "Victor Bazarov" |
|
| Title: Re: Help Problem with Lvalue required |
03 Oct 2005 03:50:29 PM |
|
|
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
.
|
|
|
| User: "mlimber" |
|
| Title: Re: Help Problem with Lvalue required |
03 Oct 2005 04:14:11 PM |
|
|
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
.
|
|
|
|
|
|
|
| User: "mlimber" |
|
| Title: Re: Help Problem with Lvalue required |
03 Oct 2005 03:30:01 PM |
|
|
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
.
|
|
|
|
|
|
| User: "mlimber" |
|
| Title: Re: Help Problem with Lvalue required |
03 Oct 2005 02:41:39 PM |
|
|
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
.
|
|
|
|
| User: "Maurizio Porcu" |
|
| Title: Re: Help Problem with Lvalue required |
04 Oct 2005 03:13:06 PM |
|
|
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" <porcu.maurizio@tiscali.it> ha scritto nel messaggio
news:4341876e$0$8489$5fc30a8@news.tiscali.it...
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
}
}
.
|
|
|
| User: "mlimber" |
|
| Title: Re: Help Problem with Lvalue required |
04 Oct 2005 03:21:55 PM |
|
|
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
.
|
|
|
| User: "Maurizio Porcu" |
|
| Title: Re: Help Problem with Lvalue required |
04 Oct 2005 03:31:47 PM |
|
|
yes but i use borland c++ and i have to write in c++
and i have problem with this source
"mlimber" <mlimber@gmail.com> ha scritto nel messaggio
news:1128457315.238793.313590@g43g2000cwa.googlegroups.com...
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
.
|
|
|
| User: "mlimber" |
|
| Title: Re: Help Problem with Lvalue required |
05 Oct 2005 08:04:30 AM |
|
|
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.
.
|
|
|
|
|
|
|

|
Related Articles |
|
|