hi.. i've got my program slightly finished.. it needs to have one function do a compare of a member of a struct but not sure how it's done.. i've been fiddling with this all night last night.. so here is the code..
this is the entire source..
Code:
#include <iostream.h>
#include <fstream.h>
#include <assert.h>
#include <string.h>
//#include <conio.h>
//#include <stdlib.h>
struct Listnode // i cannot edit structures; this is a given..
{
char lastname[30];
char firstname[30];
int age;
};
struct Listtype
{
Listnode * person[30];
int size;
};
void initialize (Listtype &List);
void fillList (Listtype &List);
void printList (Listtype &List);
void doInsert (Listtype &List);
void doDelete (Listtype &List);
int menu (void);
main() // i cannot edit main function; this is a given..
{
Listtype List;
int choice;
initialize (List);
do
{
// system("cls"); it clears output.. this is included in menu() instead
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: break;
default: cout << "\nINVALID... 1-5 ONLY!\n";
cin.ignore();
cin.ignore();
}
}
while (choice != 4);
}
int menu (void)
{
int choice;
cout << "\nMENU FOR LAB1\n";
cout << "1. Fill the list from file laba1data.txt\n";
cout << "2. Print the list (include the size of the list)\n";
cout << "3. Insert a record into the list\n";
cout << "4. Delete a record from the list\n";
cout << "5. Quit\n\n";
cout << "ENTER YOUR CHOICE: ";
cin >> choice;
// clrscr();
// system("cls");
return choice;
}
void initialize (Listtype &List)
{
for (int i=0; i<30; i++)
{
List.person[i]=NULL;
}
}
void fillList (Listtype &List)
{
char firstname[30], lastname[30];
int age, i=0;
List.size=0;
ifstream infile ("/home/school/laps/csit536/lab1data.txt", ios::in);
assert (!infile.fail());
while (infile>>lastname)
{
infile>>firstname;
infile>>age;
List.person[i] = new Listnode;
strcpy(List.person[i]->lastname, lastname);
strcpy(List.person[i]->firstname, firstname);
List.person[i]->age=age;
i++;
}
List.size=i;
}
void printList (Listtype &List)
{
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 << "\nSize of list= " << List.size << endl;
return;
}
void doInsert (Listtype &List)
{
char lastname[30], firstname[30];
int positioninsert, inserted, age;
cout << "Enter a record in this format: LASTNAME <SPACE> FIRSTNAME <SPACE> AGE \n";
cin >> lastname >> firstname >> age;
cout << "Insert this record at what position: ";
cin >> positioninsert;
inserted = positioninsert - 1;
// inserted is the position of inserted in the array
List.size++;
for ( ;positioninsert<List.size; positioninsert++)
List.person[positioninsert]=List.person[inserted];
// count how many times List.person is bumped down for insert.. positioninsert is incremented for further bumping in longer lists
List.person[inserted] = new Listnode;
strcpy(List.person[inserted]->lastname, lastname);
strcpy(List.person[inserted]->firstname, firstname);
List.person[inserted]->age = age;
}
void doDelete (Listtype &List)
{
char lastname[30], firstname[30];
// Listtype todelete;
int i=0, positionremove;
cout << "Enter the name of the record to delete: ";
cin >> lastname >> firstname;
cout << "lastname is " << lastname << " and firstname is " << firstname << endl;
cout << "List.person[i]->lastname is " << List.person[i]->lastname << " and List.person[i]->firstname is " << List.person[i]->firstname << endl;
for (i; i<List.size; i++)
{
// if (lastname == List.person[i]->lastname && firstname == List.person[i]->firstname)
if (strcmp(lastname, List.person[i]->lastname) && strcmp(firstname, List.person[i]->firstname))
{
positionremove = i;
cout << "position remove is executed" << endl;
for (i; i<List.size; i++)
{
List.person[i]=List.person[i+1];
}
break;
}
else if (i=List.size)
{
cout << "NAME NOT IN LIST" << endl;
cout << "Hit return to continue" << endl;
}
}
delete List.person[positionremove];
List.size--;
}
this part is where i'm stuck..
Code:
void doDelete (Listtype &List)
{
char lastname[30], firstname[30];
// Listtype todelete;
int i=0, positionremove;
cout << "Enter the name of the record to delete: ";
cin >> lastname >> firstname;
cout << "lastname is " << lastname << " and firstname is " << firstname << endl;
cout << "List.person[i]->lastname is " << List.person[i]->lastname << " and List.person[i]->firstname is " << List.person[i]->firstname << endl;
for (i; i<List.size; i++)
{
// if (lastname == List.person[i]->lastname && firstname == List.person[i]->firstname) // this method doesn't work..
if (strcmp(lastname, List.person[i]->lastname) && strcmp(firstname, List.person[i]->firstname))
// so does this method..
{
positionremove = i;
cout << "position remove is executed" << endl;
for (i; i<List.size; i++)
{
List.person[i]=List.person[i+1];
}
break;
}
else if (i=List.size)
{
cout << "NAME NOT IN LIST" << endl;
cout << "Hit return to continue" << endl;
}
}
delete List.person[positionremove];
List.size--;
}
i just want the if statement to execute if lastname & firstname == List.person[i]->lastname & List.person[i]->firstname, respectively..
i've tried outputing the value and it comes out right but the if statement doesn't execute.. help..?