469,950 Members | 2,353 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,950 developers. It's quick & easy.

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

Oct 3 '05 #1
13 7297
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

Oct 3 '05 #2
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
Oct 3 '05 #3
give me recommend pelase
i student and have need to solve this problem

"Victor Bazarov" <v.********@comAcast.net> ha scritto nel messaggio
news:oT*******************@newsread1.mlpsca01.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

Oct 3 '05 #4
Maurizio Porcu wrote:
give me recommend pelase
i student and have need to solve this problem
Don't top-post.
"Victor Bazarov" <v.********@comAcast.net> ha scritto nel messaggio
news:oT*******************@newsread1.mlpsca01.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
Oct 3 '05 #5
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

Oct 3 '05 #6
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

Oct 3 '05 #7
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
Oct 3 '05 #8

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

Oct 3 '05 #9

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

Oct 4 '05 #10
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" <po************@tiscali.it> ha scritto nel messaggio
news:43*********************@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
}
}

Oct 4 '05 #11
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

Oct 4 '05 #12
yes but i use borland c++ and i have to write in c++
and i have problem with this source


"mlimber" <ml*****@gmail.com> ha scritto nel messaggio
news:11**********************@g43g2000cwa.googlegr oups.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

Oct 4 '05 #13
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.

Oct 5 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Sai Krishna M | last post: by
13 posts views Thread by wessoo | last post: by
56 posts views Thread by maadhuu | last post: by
14 posts views Thread by Akhil | last post: by
16 posts views Thread by raghu | last post: by
22 posts views Thread by mdh | last post: by
4 posts views Thread by Shraddha | last post: by
16 posts views Thread by jackie | last post: by
9 posts views Thread by tsushio | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.