Hi there....

1stly sorry for the novice and beginner question but I really need help since I've been get fiddling to figure out the error but still have no idea whats wrong with it...here are's the code I'm using

#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define MaxSize 100
#define MaxFound 30

class List{

    int element;
    int index;
    int myList[MaxSize];
    char __filename[50];

    public:
    List(char filename[50]){

        strcpy(__filename,filename);

    }

    int readFile(){
        element = 0;
        index = -1;
        FILE *datafile;
        int i=0;

        datafile = fopen(__filename,"r");
        while((!feof(datafile))&&(i<MaxSize)){
                fscanf(datafile,"%d",&myList[i].element);
                myList[i].index = i;
               i++;
        }//end while
        return i;
    }

    //%d --> integer
    //%f -->float
    //%s -->string


    void displayList(int MySize){
        int j;

        cout<<"\n";
        for(j=0;j<MySize;j++){
            cout<<myList[j].element<<"  "<<endl;;
        }
    }


}; //end clas



main(){

      int fileSize;

      List obj1("senarai.txt");
      fileSize = obj1.readFile();
      obj1.displayList(fileSize);

      return 0;
}

So I really need help since I'm still beginner in C++ hoep someone can shed some light to me at least tell me whats wrong with this code....I'm also attach an image of the error that keep coming pop up to me.....same error...and I'm using borlandc++ 4.5

Recommended Answers

All 5 Replies

It seems you are reffering to myList as a struct / class, while it is just an array.
myList[j].element or myList[j].index is not valid. Use myList[j] to address the j'th element in myList.
Alternatively, declare struct ListMember with two members - index & value and create an array of ListMember (but this is too much for the task you are trying to do)

Also, in displayList, someone might give you a parameter bigger than your actual list's size and you will access memory not owned by you

Okay 1stly thansk for the reply and feedbacks...Okay..actually what I try to done is how to feed array with different datatype in my case...because I read data from txt file into array here's are my texfile format

john 30
matt 40
michael 50

so how to feed the array with this two datatype I mean char and integer.....load from textfile...I've been reading about vector but it seems I cant use it because currently I'm using borland c++ V4.5 therefore I cant include vector.h in my program....so I really need help in how to ahieve it at least tell me the rite path....any help are really appreciated

well maybe you can create an array of string and read in everything into the array

myArray[]>>myTextFile;

Alternatively, You can store in two different arrays. For instance you read the contents of the file until a whitespace is reached, store it in the first array then read the contents after the whitespace and store that in the second array

The correct way is to create a new class / struct (called "node" for example) that will have 2 members: char m_name[50], int m_grade. When you read from the file, you create a new instance of "node" for every line, read until white space the name into m_name and the number into m_grade and push the instance into the array (the array is array of "nodes" of course).

Hope this helps

thanks for the help for u both..I guess have to shift to new compiler since this traditional array realy give me headache....thanks again guys...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.