struct
Lekce 6\06_06.cpp||In function 'int main()':|
\Lekce 6\06_06.cpp|32|error: invalid array assignment|
||=== Build finished: 1 errors, 0 warnings ===|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
#include <iostream>
struct prehled
{
char name[200];
int suma;
};
int main()
{
using namespace std;
int darci;
cout << "Zadejte pocet darcu: ";
cin >> darci;
prehled * pt = new prehled [darci];
for (int i = 0; i <darci;i++)
{
cout << "\nZadejte jmeno: ";
cin >> pt[i].name;
cout << "Zadejte sumu: ";
cin >> pt[i].suma;
}
prehled * lv = new prehled [darci]; // lv = low volume
int pocitadlo = 0;
cout << "Velci darci: ";
for (int n = 0; n <darci; n++)
{
if (pt[n].suma < 10000)
{
lv[pocitadlo].suma = pt[n].suma;
lv[pocitadlo].name = pt[n].name;
pocitadlo++;
}
else
{
cout << "\n\nJmeno: " << pt[n].name;
cout << "\nSuma: " << pt[n].suma;
}
}
delete [] pt;
cout << "Mali darci:";
pocitadlo = 0;
for (int a = 0; a<pocitadlo;a++)
{
cout << "\nJmeno: " << lv[pocitadlo].name;
cout << "Suma: " << lv[pocitadlo].suma;
}
delete [] lv;
return 0;
}
|
lv[pocitadlo].name = pt[n].name;
You cannot do that. You have some options:
1. Use memcpy (
http://www.cplusplus.com/reference/cstring/memcpy/)
2. Copy each char in name[] array individualy:
1 2
|
for(i=0; i<sizeof(lv[pocitadlo].name) i++)
lv[pocitadlo].name[i] = pt[n].name[i];
|
3. Easiest: Declare name as
std::string
1 2 3 4 5 6
|
#include <string>
struct prehled
{
std::string name;
int suma;
};
|
As a sidenote, why
pocitadlo
is always set to zero? Shouldn't it be replaced with
n
in first loop and with
a
in the second?
Last edited on
Why memcpy and not strcpy?
thank you!
And Why I can not transmit array from struct to struct?
And Why I can not transmit array from struct to struct? |
You can with a custom constructor:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
struct prehled
{
// default constructor:
prehled()
{
memset(name, '\0', 200);
suma = 0;
}
// custom constructor:
prehled(const char* _name, int _suma = 0)
{
if (strlen(_name) < 200)
strcpy(name, _name);
else
memset(name, '\0', 200);
suma = _suma;
}
// copy constructor:
prehled(const prehled& source)
{
strcpy(name, source.name);
suma = source.suma;
}
char name[200];
int suma;
};
int main()
{
prehled* a = new prehled("MyName");
prehled* b = new prehled(*a);
delete a;
delete b;
return 0;
}
|
1 2 3 4 5
|
if (strlen(_name) < 200)
strcpy(name, _name);
else
memset(name, '\0', 200); // Needed in all occurrences?
suma = _suma;
|
|
Waste of performance.
Use this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
struct prehled
{
prehled(const char * Name, int Suma) {
strcpy(name,Name);
suma = Suma;
}
prehled() : suma(0) {
name[0] = 0;
}
prehled(const prehled& o) { (*this) = o; }
prehled& operator= (const prehled& o) {
strcpy(name, o.name);
suma = o.suma;
}
char name[200];
int suma;
};
|
Then you can substitute
1 2
|
lv[pocitadlo].suma = pt[n].suma;
lv[pocitadlo].name = pt[n].name;
|
with:
Last edited on
Topic archived. No new replies allowed.