First off, I'd like to say this is a hw problem. I have gutted out other functions that are fine and left only the one im having a migraine on X_X. So, the problem is reading data in from a file which i cant seem to do.
sample file entries from LAB1DATA.txt
Code:
joe smith 12
joe smoe 66
joe someone 19
My gutted work:
Code:
#include <iostream.h>
#include <fstream.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
struct Node
{
char firstname[30];
char lastname[30];
int age;
};
struct Listtype
{
Node * people[10];
int size;
};
//Function Prototypes
Listtype initialize(Listtype&);//gutted out
int menu();//gutted out
void deallocate(Listtype&);//gutted out
void printList(Listtype);//gutted out
void fillList(Listtype&);
void doInsert(Listtype&);//gutted out
void doDelete(Listtype&); //gutted out
void main() //left here so you can get the gist of what the prog is suppose to do.
{
Listtype List;
int choice;
initialize(List);
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: break;
default : cout<<"\n INVALID...1-5 ONLY!\n";
cin.ignore();
cin.ignore();
}
}while( choice != 5 );
deallocate(List);
}
void fillList(Listtype &List)
{
int i = 0;
char fname[30];
char lname[30];
int tempAge;
system("cls");
ifstream infile("c:LAB1DATA.txt");
assert( !infile.fail() );
while( infile>>fname>>lname>>tempAge )
{
List.people[i] = new Node;
strcpy(List.people[i]->lastname, lname);
strcpy(List.people[i]->firstname, fname);
List.people[i]->age = tempAge;
cout<<List.people[i]->lastname << List.people[i]->firstname << List.people[i]->age <<endl ;
i++;
}
List.size = i;
}
i originally wrote the while loop in fillList() as:
Code:
int i = 0;
while( !infile.eof() )
{
List.people[i] = new Node;
infile<<List.people[i]->lastname << List.people[i]->firstname << List.people[i]->age;
i++;
}
this didnt work, so i refered to sample input loop from my instructors handout which is in the above code - which also does not work the way how im using it :/. I have no clue why it isnt reading the data from the file. Any suggestions? thnxs in advance
minor note - im using Microsoft Visual C++ 6.0, as the school lab uses this compiler and my professor REQUIRES us to have our labs to work on school computers which is a pain, so ive confined myself to use Microsoft Visual C++ 6.0 and not dev C++ : /