i have an array of pointers to structs. but i am trying to get under the doinsert function, which prompt for a new person to go into the list, but i cannot change the file itself, all must be done in memory.
but i cant assign the new person last name, first name and age, to the next available spot on the list.
i cant figure out how to assign, or point over.
basically, this function puts a new person into the struct, LASTNAME, FIRSTNAME, AGE.
but i use cin, but i cant pass the char to the struct.
i dont know how to do this, it says it cant covernt char[30] to *char
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
void doinsert(Listtype &List)
{ //option 3
system("CLS");
char lname[30], fname[30];
int agein, recin, posin=0;
cout << "Enter a record in this format: LASTNAME <SPACE> FIRSTNAME <SPACE> AGE" << endl;
cin >> lname >> fname >> agein ;
cout << "Insert this record at what posotion: ";
//cin >> recin;
//this is where i cant make it work, i want to add new person, but i cant change the actual file, its all in memory, coping the address is the only way i see fit.
//List.person[List.size]->lastname=&lname;
//List.person[List.size]->firstname=&fname;
//List.person[List.size]->age=&agein;
//i=posin;
List.size++;
cin.ignore();
cin.ignore();
}
|
the full program is here
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 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
#include <iostream>
#include <fstream>
#include <conio.h>
#include <assert.h>
#include <string.h>
using namespace std;
struct Listnode
{
char lastname[30];
char firstname[30];
int age;
};
struct Listtype
{
Listnode* person[30];
int size;
};
Listtype initialize(void);
void filllist(Listtype &List);
void printlist(Listtype &List);
void doinsert(Listtype &List);
void dodelete(Listtype &List);
void deallocate(Listtype &List);
//void repeat();
int menu();
int main ()
{
Listtype List;
int choice;
List=initialize();
do
{
system("CLS");
choice = menu ();
switch (choice)
{
case 1: filllist(List); break;
case 2: printlist(List); break;
case 3: doinsert(List); break;
case 4: dodelete(List); break;
case 5: deallocate(List); break;
case 6: break;
default : cout << "\nINVALID...1-6 ONLY!\n";
cin.ignore();
cin.ignore();
}
} while (choice != 6);
}
int menu()
{
int choice =0;
cout<< "MENU FOR LAB1" <<endl;
cout<< "1. Fill the list from file LAB1DATA.TXT" <<endl;
cout<< "2. Print the list (include the size of the list)" <<endl;
cout<< "3. Insert a record into the list" <<endl;
cout<< "4. Delete a record from the list"<< endl;
cout<< "5. Delete all records from list" <<endl;
cout<< "6. Quit" <<endl;
cout<< endl;
cout << "ENTER YOUR CHOICE:";
cin >> choice;
cout << endl;
return choice;
}
Listtype initialize()
{
Listtype L;
for (int i=0; i<30; i++)
L.person[i]=NULL;
L.size=0;
return L;
}
void filllist(Listtype &List)
{
char lastname[30];
char firstname[30];
int age, i=0;
ifstream infile ("lab1data.txt", ios::in);
assert (!infile.fail());
while (infile>>lastname)
{
infile>> firstname;
infile>> age;
List.person[i]=new Listnode;
strcpy_s(List.person[i]->lastname, lastname);
strcpy_s(List.person[i]->firstname, firstname);
List.person[i]->age=age;
i++;
}
List.size=i;
}
void printlist(Listtype &List)
{
system("CLS");
int i;
for (i=0; i<List.size; i++)
cout << i+1 << " " << List.person[i]->lastname<<" " <<List.person[i]->firstname<<" " <<List.person[i]->age<<endl;
cout<< "\nList Size= " << List.size<< " [Hit return to continue]" <<endl;
cin.ignore();
cin.ignore();
return;
//repeat();
}
void doinsert(Listtype &List)
{ //option 3
system("CLS");
char lname[30], fname[30];
int agein, recin, posin=0;
cout << "Enter a record in this format: LASTNAME <SPACE> FIRSTNAME <SPACE> AGE" << endl;
cin >> lname >> fname >> agein ;
cout << "Insert this record at what posotion: ";
//cin >> recin;
//this is where i cant make it work, i want to add new person, but i cant change the actual file, its all in memory, coping the address is the only way i see fit.
//List.person[List.size]->lastname=&lname;
//List.person[List.size]->firstname=&fname;
//List.person[List.size]->age=&agein;
//i=posin;
List.size++;
cin.ignore();
cin.ignore();
}
void dodelete(Listtype &List)
{
printf("You've choosed option 4\n\r");
//repeat();
}
void deallocate(Listtype &List)
{
printf("You've choosed option 5\n\r");
//repeat();
}
|