Having troubles

This is a discussion on Having troubles within the C++ Programming forums, part of the General Programming Boards category; Im trying to load up an array of struct variables. I get stuck in the loop in my function that ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    1

    Having troubles

    Im trying to load up an array of struct variables. I get stuck in the loop in my function that loads that is supposed to load the structs into the array. I'll post my code, and any help that you guys could provide would really be appreciated.

    I've just posted my load function, as my count function works fine, as does my menu, but I am leaving the struct in.
    I've left the Couts & system pauses in there, they dont need to be there, I just put them in to help in the debuging. This program loods the first element from the text file perfectly, then all I get from ther is jiberash.
    Thanks
    Dave
    Code:
    
    
    struct emp
           {
                string name;
                int empid;
                char type;
                
                //h=hourly, s=salaried
                float payrate;
                string dept;
                string title;
                
                };
    
    
    
    }
    //*********************************load***********************************
    //********************loads employees into an arrray
    void load()//(int count)
    {
        
        int tempi;
        tempi = empcount();
        emp E[tempi];
        
        fstream fin;
        fin.open("empdata.txt",ios::in);
    
     
    for(int i=0; i<=tempi; i++)
     {
        getline (fin, E[i].name);
        cout<<E[i].name;
        fin.ignore();
        system("pause");
        
        fin>>E[i].empid;
        cout<<E[i].empid;
        system("pause");
       
        fin>>E[i].type;
        cout<<E[i].type;
        system("pause");
        
        fin>>E[i].payrate;
        cout<<E[i].payrate;
        system("pause");
        
        getline (fin, E[i].dept);
        cout<<E[i].dept;
        system("pause");
        
        getline (fin, E[i].title);
        cout<<E[i].title;
        system("pause");
     }
    
    fin.close();
    
    
    }
        
    
    int main()
    {
        int mcount;
        
        mcount = empcount();  // empcount is the name of my count  f               .   
        cout<<"There are "<<mcount<<" employees in the file.    <<endl;
        menu();
        load();
        
        
        system("pause");
        return 0;
    };

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    1) Post your input file. Are you aware that:

    getline (fin, E[i].name);

    reads an entire line of data into the string E[i].name?

    2) Are you aware that when your load() function finishes, all the variables will be destroyed? So, calling load() will end up not accomplishing anything?
    Last edited by 7stud; 04-07-2005 at 01:07 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-15-2006, 04:14 AM
  2. Console-based file i/o troubles
    By Callith in forum C++ Programming
    Replies: 3
    Last Post: 12-25-2004, 08:22 PM
  3. sscanf troubles
    By williams75 in forum C Programming
    Replies: 5
    Last Post: 11-17-2002, 01:51 AM
  4. More Program Troubles...
    By face_master in forum Windows Programming
    Replies: 2
    Last Post: 08-01-2002, 05:40 AM
  5. having troubles
    By neandrake in forum C++ Programming
    Replies: 7
    Last Post: 03-07-2002, 08:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21