loading into structs

This is a discussion on loading into structs within the C++ Programming forums, part of the General Programming Boards category; Hi guys... I am working with a struct for the first time and having trouble just loading my data (from ...

  1. #1
    Registered User mouse163's Avatar
    Join Date
    Dec 2002
    Posts
    49

    Unhappy loading into structs

    Hi guys...
    I am working with a struct for the first time and having trouble just loading my data (from an external file into it).
    Can someone take apeek at my Load function and tell me what the outline to load should look like?
    I have an ex file that has
    first names
    last names
    4 exam grades

    Here's ehat I have so far:
    Code:
    #include<iostream>
    #include<iomanip>
    #include<fstream>
    using namespace std; 
    
    struct Student{
    char lastname[20];
    char firstname[20];
    double exams[4];
    double average;
    char letgrade;
    bool passed;};
    
    const int MAXSTUDENTS = 25;
    
    
    Student ClassList[MAXSTUDENTS];
    
    
    //function Prototypes
    
    void Load(Student[], int&);
    void setupFiles(); 
    
    ifstream infile("C:\\My Documents\\Cee_\\HW\\HW_Final\\IFF.txt");
    ofstream outfile("C:\\My Documents\\Cee_\\HW\\HW_Final\\OFF.txt");
    int main()
    {
    
    int ns = 0;
    
    //calls
    
    setupFiles();
    
    Load(ClassList, ns);
    
    
    
    
    infile.close(); 
    outfile.close(); 
    
    return 0;
    
    
    }
    
    
    
    //set up files fx===========================================
    void setupFiles() {
    
    if(!infile){ 
    cerr << "Cannot open input file" << endl; 
    
    } //if
    
    if(!outfile){ 
    cerr << "Cannot open output file" << endl; 
    } //if
    
    }
    
    
    
    //Function Load
    void Load(Student CL[], int& ns)
    {
    
    int y =0;
    while (infile >> CL[ns].lastname){
    (CL[ns].firstname);
    while (y = 0; y < 4; y ++){
    (CL[ns].exams[y]);
    ns++;
    
    }//while1
    }//while2
    
    
    
    }

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    80
    You have several options here. First off, I would advise you to not declare your streams globally. Yes, it is convenient, but is bad style, etc.

    Next, I would probably re-do your loopng structures. Why not just do:

    Code:
    while(inflile)
    {
    
           // read in values using extraction
    }
    You can extract all the data for each student each iteration through the loop. Of course, increment the loop each time so you progress though your array of students.

    Hope this helps

  3. #3
    Registered User mouse163's Avatar
    Join Date
    Dec 2002
    Posts
    49
    Hi thanks I am going to actually separate the global defs into an .h file and then the functions and main into client .cpp files.

    Pretty much where i am confused is as to HOW to structure the loop to get the info IN. The format loop I am using is giving me syntax errors and since I have not done this yet (loading the struc) i am kinda at a loss as to how to do it.
    I cannot find any examples and I am taking a class that is
    more or less a 'teach yourself' so i have no example from there either.
    can you be more specific as to how to read in values using extraction?
    i wish i knew what you meant but i don't.
    thanks,
    Mouse

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    80
    Try this on for size:

    Code:
    int i = 0;
    
    while(infile)
    {
       infile >> ClassList[i].lastname >> ClassList[i].firstname >> ClassList[i].exams;
       i++;
    }
    That oughta be what you're looking for, assuming that you want to read an entire record each time through the loop. If not, the code will still work, as infile.good() will be false, and the loop will exit. You might want to do some error checking, etc, after the loop. Let me know if this isn't what you need.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    while (infile >> CL[ns].lastname)
    {
      (CL[ns].firstname);
      while (y = 0; y < 4; y ++)
      {
          (CL[ns].exams[y]);
           ns++;//////////////////////////////////here's the problem, I think.  With this line here each student is only going to get one exam grade in the exams array, with the firtst student getting a report in exams[0] the second student in exams[1], the third in exams[2], the fourth in exams[3], the fifth in exams[0] again, etc.  I'll leave it up to you to figure out where to put this line.  
    
    This is an example of why I think the coding style displayed here is easier to debug than the one you used, although the one you used is a common style.  
       }//while1
    }//while2
    Last edited by elad; 12-12-2002 at 12:18 PM.

  6. #6
    Registered User mouse163's Avatar
    Join Date
    Dec 2002
    Posts
    49

    trying

    I tried something like what you have here...but it deosn't seem like my stuff is laoding...
    I thought since there are only 4 exams grades to use a separate loop to load those
    but when i try to cout to the screen to see what i have loaded
    nothing is showing at all:
    plus i am getting an io exception to debugg!
    here's the mod version:
    Code:
    //Function Load
    	void Load(Student CL[], int& ns)
    	{
         
    	  int i = 0;
    	  int y = 0;
    while(infile)
    {
       infile >> CL[i].lastname >> CL[i].firstname;
       i++;
    
    
          
    		  for (y = 0; y < 4; y++){
    					(CL[ns].exams[y]);
    					
    					}//for
    						
    
    						 
    	  }//while
    
    		cout << CL[ns].lastname << endl;
    		cout << CL[ns].firstname << endl;
    		cout << CL[ns].exams[y] << endl;
    
    	}
    thanks so much for the help...
    it gets pretty frustrating!
    Mouse

  7. #7
    Registered User mouse163's Avatar
    Join Date
    Dec 2002
    Posts
    49
    Hi Elad ---thanks for your input...
    i am getting a wierd error code at this line:
    Code:
      while (y = 0; y < 4; y ++)
    
    
    it is saying:C:\Program Files\Microsoft Visual Studio\MyProjects\HW_F\HW_F.CPP(91) : error C2143: syntax error : missing ')' before ';'
    i don't get it...also it iserroring me at the y loop saying that
    Code:
    while (int y = 0; y < 4; y ++)
    		{
    		(CL[ns].exams[y]);
    
    
    :\Program Files\Microsoft Visual Studio\MyProjects\HW_F\HW_F.CPP(91) : error C2065: 'y' : undeclared identifier
    C:\Program Files\Microsoft Visual Studio\MyProjects\HW_F\HW_F.CPP(91) : warning C4552: '<' : operator has no effect; expected operator with side-effect
    i am really confused...I do get the ns++ hint you gave but it's the rest of the loop that seems like it should work to me but doesn't??!!



  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    the double while loop was fine as far as I'm concerned. I would drop the i counter and just go with the ns counter as you had before.

    I ask you to think about what ns is representing? It's the number of the given student whose exam grades you will be reading in, correct? In that case, when do you want to change the student number---after a single exam grade is read in, or after all 4 exam grades have been read in? Just move the increment of ns to the apprpriate spot and I think all your troubles are solved, although I don't have a compiler to prove it at the moment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cargo loading system
    By kyle rull in forum C Programming
    Replies: 1
    Last Post: 04-20-2009, 12:16 PM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. BMP Loading Structs
    By IdioticCreation in forum C++ Programming
    Replies: 4
    Last Post: 07-05-2007, 12:42 PM
  4. Loading a bitmap (Without using glaux)
    By Shamino in forum Game Programming
    Replies: 7
    Last Post: 03-16-2006, 08:43 AM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM

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