structs + dynamic allocations - output problem
Aug 29, 2015 at 4:59pm UTC
When i try to cout the array after activate the functions. the program is stocked! (you can only see the kid's details).
With my best regards :)
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
#include <iostream>
#include <string.h>
using namespace std;
const int MAX_LETTERS = 30;
struct person_t
{
int id;
char *firstName;
person_t *mom;
person_t *dad;
};
struct family_t
{
char *lastName;
person_t* mom;
person_t *dad;
int numberOfKids;
person_t* kids;
};
void readPerson(person_t *person);
void printPerson(person_t *person);
void readFamily(char *dad, char *mom);
void printFamily(family_t *family);
void main()
{
person_t person1;
person_t person2;
readPerson(&(person1));
cout << '\n' ;
readPerson(&(person2));
cout << '\n' ;
family_t *family;
cout << "Enter a name of family's dad (if there is): " ;
cin >> family->dad;
cout <<
cout << '\n' ;
printFamily(&(family));
delete []person1.firstName;
delete []person2.firstName;
delete []family.lastName;
cout << '\n' ;
}
void readPerson(person_t *person)
{
char temp[MAX_LETTERS];
cout << "Please enter a name (max 30 letters): " ;
cin >> temp;
person->firstName = new char [strlen(temp) + 1];
strcpy(person->firstName, temp);
cout << '\n' ;
cout << "Please enter an id: " ;
cin >> person->id;
person->dad = NULL;
person->mom = NULL;
}
void printPerson(person_t *person)
{
cout << "Person's name: " << person->firstName << '\n' ;
cout << "Perons's id: " << person->id << '\n' ;
if (person->dad != NULL)
{
cout << "Person's dad: " << person->dad << '\n' ;
}
else if (person->mom !=NULL)
{
cout << "Person's mom: " << person->mom << '\n' ;
}
}
void readFamily(char *dad, char *mom)
{
family_t *family;
dad = &family->dad;
char temp[MAX_LETTERS];
cout << "Please enter a family's dad: " ;
cin >> *dad=*(famliy->dad);
cout << "Enter a name for family (max 30 letters): " ;
cin >> temp;
family->lastName = new char [strlen(temp) + 1];
strcpy(temp, family->lastName);
cout << "Enter number of kids: " ;
cin >> family->numberOfKids;
family->kids = new person_t[family->numberOfKids];
for (int i = 0; i < family->numberOfKids; i++)
{
cout << "Enter details for kid number " << i + 1 << '\n' ;
readPerson(&(family->kids[i]));
}
}
void printFamily(family_t *family)
{
cout << "Family name: " << family->lastName << '\n' ;
if (family->dad != NULL && family->mom!=NULL)
{
cout << "Family's dad: " << family->dad << '\n' ;
cout << "Family's mom: " << family->mom << '\n' ;
}
else
{
cout << "There isn't a dad or a mon in the family." << '\n' ;
}
cout << "Kids in the family: " << '\n' ;
for (int i = 0; i < family->numberOfKids; i++)
{
printPerson(&(family->kids[i]));
}
for (int i = 0; i < family->numberOfKids; i++)
{
delete []&(family->kids[i]);
}
delete [] & (family->kids);
delete []&(family->numberOfKids);
}
Last edited on Aug 29, 2015 at 5:00pm UTC
Aug 29, 2015 at 7:25pm UTC
¿stocked?
> you can only see the kid's details
your code doesn't compile, you cannot run it and see its ouput.
Aug 29, 2015 at 8:11pm UTC
ok, so what i need to do to fix it?
Aug 29, 2015 at 11:08pm UTC
show your actual code.
foo.cpp|29 col 11| error: ‘::main’ must return ‘int’
|| void main()
|| ^
|| foo.cpp: In function ‘int main()’:
foo.cpp|43 col 6| error: no match for ‘operator>>’ (operand types are ‘std::istream {aka std::basic_istream<char>}’ and ‘person_t*’)
|| cin >> family->dad;
|| ^
foo.cpp|44 col 7| error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘std::ostream {aka std::basic_ostream<char>}’)
|| cout <<
|| ^
foo.cpp|47 col 23| error: cannot convert ‘family_t**’ to ‘family_t*’ for argument ‘1’ to ‘void printFamily(family_t*)’
|| printFamily(&(family));
|| ^
foo.cpp|51 col 17| error: request for member ‘lastName’ in ‘family’, which is of pointer type ‘family_t*’ (maybe you meant to use ‘->’ ?)
|| delete[]family.lastName;
|| ^
|| foo.cpp: In function ‘void readFamily(char*, char*)’:
foo.cpp|90 col 6| error: cannot convert ‘person_t**’ to ‘char*’ in assignment
|| dad = &family->dad;
|| ^
foo.cpp|93 col 16| error: ‘famliy’ was not declared in this scope
|| cin >> *dad=*(famliy->dad);
|| ^
Topic archived. No new replies allowed.