Managing an Array of Students.

This is a discussion on Managing an Array of Students. within the C++ Programming forums, part of the General Programming Boards category; I am in the process pulling data from a file. The data contains lines of Students and their info. Example ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    10

    Managing an Array of Students.

    I am in the process pulling data from a file. The data contains lines of Students and their info. Example of one line:

    Elmore Dennis 78 MED23332

    So basically, its last name, first name, his exam score, his gender, his initials, and his ID number. There are about 20 lines of this. I want to put them in an array, and do a bunch of stuff with it. So my first step....I just want to create my array and output it to my own brand new file. Here is the code I have so far:

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <iomanip.h>
    #include <string.h>
    #include <ctype.h>
    
    ofstream outfile;
    ifstream infile;
    
    struct studentrec
    {
      char name[21];
      int exam;
      char gender;
      char id[8];
    };
    
    int main()
    
    {
    
      infile.open("rh1165.dat");
      outfile.open("out6.txt");
      outfile.setf(ios::fixed,ios::floatfield);
      outfile.setf(ios::showpoint);
      outfile<<setprecision(2);
    
      studentrec student[40];
      int count = 0;
    
      infile.getline(student[count].name, 20);
      while(infile)
      {
        infile>>student[count].exam>>student[count].gender;
        infile.getline(student[count].id, 7);
        count++;
        infile.getline(student[count].name, 20);
      }
    outfile<<studentrec;
      return 0;
    
    }
    At the end of the program, I just stuck the "outfile<<studentrec;" in for kicks, which resulted in an error. I'm very new at this stuff and what I have even confuses me. Any help appreciated Thanks!
    eskimos have feelings too...

  2. #2
    end0
    Guest
    you might need to overload << for studentrec, I'm not sure if the compiler provides a default definition

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    10

    Question

    so i would have to use something besides "<<"?
    Or is the 'studentrec' name not valid?
    eskimos have feelings too...

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    infile.getline(student[count].name, 20);

    the above is legal but illogical. What if the name was Ed Fox? If you read in the frist 20 char as indidated above the name field would end up as "Ed Fox 78 MEF23332" when all you wanted was "Ed Fox".

    I would suggest reading in the data one field at a time and outputting the data one field at a time (or use the write/read member functions and write to the file in binary).

    Code:
     #include <iostream.h>
    #include <fstream.h>
    #include <iomanip.h>
    #include <string.h>
    #include <ctype.h>
    
    ofstream outfile;
    ifstream infile;
    
    struct studentrec
    {
      char name[21];
      int exam;
      char gender;
      char id[8];
    };
    
    int main()
    
    {
    
      infile.open("rh1165.dat");
      outfile.open("out6.txt");
      outfile.setf(ios::fixed,ios::floatfield);
      outfile.setf(ios::showpoint);
      outfile<<setprecision(2);
    
      studentrec student[40];
      int count = 0;
      
      string firstName;
      string lastName;
    
      infile >> firstName;
      while(infile)
      {
        infile >> lastName;
        student[count].name = firstName + lastName;
        infile>>student[count].exam;
        infile >>student[count].gender;
        infile.getline(student[count].id, 7);
        count++;
        infile >> first name;
      }
       
       int i;
       for(i = 0; i < count; i++)
       {  
          outfile << student[i].name;
          outfile << student[i].exam;
          outfile << student[i].gender;
          outfile << student[i].id;
       }
    
      return 0;
    
    }
    
    to overload the << operator for studentrec you could:
    
    struct studentrec
    {
      char name[21];
      int exam;
      char gender;
      char id[8];
      friend studentrec & operator >> (const ostream & os, const studentrec & rec);
    };
    
    
    ostream & operator << (const ostream & os, const studentrec & rec)
    {
          os << student[i].name;
          os << student[i].exam;
          os << student[i].gender;
          os << student[i].id;
          os << endl;
         return os;
    }
       
    
    so now you could do:
    
    for(i = 0; i < count; i++)
    {
      outfile << student[i];
    }
    
    but doesn't save much typing unless you use << operator with studentrec on several occassions in your program however.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    10
    Thank you. That helps out for the start of my program. Except, I'm getting these errors. I'm working on it, but not having luck fixing them at the moment.


    cxx: Error: prog6.cxx, line 38: identifier "string" is undefined
    string firstName;
    --^
    cxx: Error: prog6.cxx, line 39: identifier "string" is undefined
    string lastName;
    --^
    cxx: Error: prog6.cxx, line 45: expression must be a modifiable lvalue
    student[count].name = firstName + lastName;
    ----^
    cxx: Info: 3 errors detected in the compilation of "prog6.cxx".
    eskimos have feelings too...

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    May the rest of the members of the Board forgive me for the continuation of non-Standard code :
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <iomanip.h>
    #include <ctype.h>
    #include <conio.h>
    
    ofstream outfile;
    ifstream infile;
    
    struct studentrec
    {
      char lastName[10];
      char firstName[10];
      int exam;
      char gender;
      char id[8];
    };
    
    int main()
    
    {
    
      infile.open("rh1165.dat");
      outfile.open("out6.txt");
      outfile.setf(ios::fixed, ios::floatfield);
      outfile.setf(ios::showpoint);
      outfile<<setprecision(2);
    
      studentrec student[40];
      int count = 0;
    
      while(infile)
      {
        infile >> student[count].lastName;
        infile >> student[count].firstName;    
        infile>>student[count].exam;
        infile >>student[count].gender;
        infile.getline(student[count].id, 7);
        count++;    
      }
    
       int i;
       for(i = 0; i < count; i++)
       {
          outfile << student[i].firstName;
          outfile << " ";
          outfile << student[i].lastName;
          outfile << " ";
          outfile << student[i].exam;
          outfile << " ";
          outfile << student[i].gender;
          outfile << " ";
          outfile << student[i].id;
          outfile << "\n";
       }
    
      infile.close();
      outfile.close();
    
      getch();
      return 0;
    
    }
    And, thanks for the snow, eskimo083. I know you folks sent it down to us. One or two inches, indeed!

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    10
    sure...blame the snow on me. boy i forget every year how much i hate it until it actually falls. i do admit that doing circles in parking lots and sliding out of turns is fun though

    as for the code....argh. you all hear this from newbies every day prolly, but i hate C++ at this moment in time so frustrating..

    my compiler does not recognize the header file "conio.h", seeing as how i'm doing this in UNIX, which is always fun too.

    anyone feel free to add to this mess of a post that i created if you have any ideas. and i do really appreciate everyone's help so much. i'm just plugging away trying all your ideas and using help books and just trying to make progress. your help really is priceless though.

    off to try some more....

    Todd
    eskimos have feelings too...

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    10
    Update (for those who read this much of my dumb topic)

    I managed to get something that works after working with a buddy half the nite.

    Code:
    #include <fstream.h>
    #include <string.h>
    #include <ctype.h>
    #include <iomanip.h>
    
    
    struct studentrec
    {
    	char name[21];
    	int exam;
    	char gender;
    	char id[8];
    };
    
    void main()
    {
    	ifstream in("rh1165.dat");
    	ofstream out("out6.txt");
    
    	studentrec student[36];
    	char valid[36][20];
    
    	//function prototypes
    	void header(ofstream &out);
    	void page1(ifstream &in, ofstream &out, studentrec student[]);
    	void page2(ofstream &out, studentrec student[]);
    	void page3(ofstream &out, studentrec student[], char valid[36][20]);
    	void page4(ofstream &out, studentrec student[], char valid[36][20]);
    
    	header(out);
    	page1(in, out, student);
    	header(out);
    	page2(out, student);
    	header(out);
    	page3(out, student, valid);
    	header(out);
    	page4(out, student, valid);
    }
    
    void header(ofstream &out)
    {
    	out << "        Name          Exam  Gender  Student ID";
    }
    
    void page1(ifstream &in, ofstream &out, studentrec student[])
    {
    	int count = 0;
    	
    	//read in information
    	while(!in.eof())
    	{
    		in.get(student[count].name, 21);
    		in >> student[count].exam;
    		in >> student[count].gender;
    		in.get(student[count].id, 8);
    		in >> ws;
    		count++;
    	}
    	
    	//print out information
    	out << endl << endl;
    	for(int i = 0; i < 36; i++)
    	{
    		out << setw(20) << student[i].name   << "   "
    			<< setw(3)  << student[i].exam   << "     "
    			<< setw(1)  << student[i].gender << "     "
    			<< setw(8)  << student[i].id     << endl;
    	}
    	out << '\f';
    
    }
    
    void page2(ofstream &out, studentrec student[])
    {
    	studentrec temp;
    
    	//perform bubble sort
    	for(int i = 0; i < 36; i++)
    	{
    		for(int j = 0; j < 35; j++)
    		{
    			if((strncmp(student[j].name, student[j + 1].name, 20)) > 0)
    			{
    				temp = student[j];
    				student[j] = student[j + 1];
    				student[j + 1] = temp;
    			}
    		}
    	}
    
    	//print out information
    	out << endl << endl;
    	for(i = 0; i < 36; i++)
    	{
    		out << setw(20) << student[i].name   << "   "
    			<< setw(3)  << student[i].exam   << "     "
    			<< setw(1)  << student[i].gender << "     "
    			<< setw(8)  << student[i].id     << endl;
    	}
    	out << '\f';
    }
    
    void page3(ofstream &out, studentrec student[], char valid[36][20])
    {
    	char valid_code[][20] = {
    						  "Invalid Name",
    						  "Invalid Score",
    						  "Invalid Gender",
    						  "Invalid Student ID"
    						};
    	for(int i = 0; i < 36; i++)
    		strncpy(valid[i], "Valid", 20);
    	
    	for(i = 0; i < 36; i++)
    	{
    		//check name validity
    		for(int j = 0; j < 20; j++)
    		{
    			if(!isalpha(student[i].name[j]) && !isspace(student[i].name[j]))
    			{
    				strncpy(valid[i], valid_code[0], 20);
    				break;
    			}
    		}
    	}
    
    	//check score validity
    	
    	for(i = 0; i < 36; i++)
    	{
    		if(student[i].exam < 0 || student[i].exam > 100)
    		{
    			strncpy(valid[i], valid_code[1], 20);
    		}
    	} 
    
    	//check gender validity
    	for(i = 0; i < 36; i++)
    	{
    		if(student[i].gender != 'F' && student[i].gender != 'M')
    		{
    			strncpy(valid[i], valid_code[2], 20);
    		}
    	}
    
    	//check student ID validity
    	for(i = 0; i < 36; i++)
    	{
    		if(!isupper(student[i].id[0]) || !isupper(student[i].id[1]) ||
    		   !isdigit(student[i].id[2]) || !isdigit(student[i].id[3]) ||
    		   !isdigit(student[i].id[4]) || !isdigit(student[i].id[5]) ||
    		   !isdigit(student[i].id[6]))
    		{
    			strncpy(valid[i], valid_code[3], 20);
    		}
    	}
    
    
    	//print out information
    	out << "       Valid" << endl << endl;
    	for(i = 0; i < 36; i++)
    	{
    		out << setw(20) << student[i].name   << "   "
    			<< setw(3)  << student[i].exam   << "     "
    			<< setw(1)  << student[i].gender << "     "
    			<< setw(8)  << student[i].id     << "      "
    			<< valid[i] << endl;
    	}
    	out << '\f';
    }
    
    void page4(ofstream &out, studentrec student[], char valid[36][20])
    {
    	out << "       Valid" << endl << endl;
    	
    	int count = 0;
    	studentrec valid_sorted[36];
    
    	//refill data with only valid records
    	for(int i = 0; i < 36; i++)
    	{
    		if((strncmp(valid[i], "Valid", 20)) == 0)
    		{
    			valid_sorted[count] = student[i];
    			count++;
    		}
    	}
    
    	//shell sort
    
    
    	//print out information
    	for(i = 0; i < count; i++)
    	{
    		out << setw(20) << valid_sorted[i].name   << "   "
    			<< setw(3)  << valid_sorted[i].exam   << "     "
    			<< setw(1)  << valid_sorted[i].gender << "     "
    			<< setw(8)  << valid_sorted[i].id     << "      "
    			<< endl;
    	}
    	out << '\f';
    }
    So basically this program (so far) displays the array, sorts it, looks for invalid data, removes it...all that fun stuff. And I have guidelines, so I had to use a bubble sort at least once, things like that. Void main....yes its evil (although I don't know why yet). I may still fix that.

    Todd
    eskimos have feelings too...

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    i do admit that doing circles in parking lots and sliding out of turns is fun though
    The woman who went broadside in front of me on the Lodge didn't have as much fun as you, I'll wager. (No, I didn't T-bone her, but that sure wasn't her fault. )

    Good to hear that things are muddling along better than they were. That <conio.h> thing is what I was referring to about the non-Standard code. Actually, any headers with .h extensions are now deprecated. The old C Standard library files such as <stdlib.h> are now simply preceded with a 'c' and have the extension dropped, i.e. <cstdlib>. The C++ library files just drop the .h, i.e. <iostream.h> to <iostream>.

    The quick and dirty way to implement the standard code is to precede main() with 'using namespace std;', then write the code as you've already been doing.

    Others, myself included, prefer preceding keywords with 'std::'. Example: std::cout and std::cin. This method keeps the global namespace from being loaded with the entire Standard as 'using namespace std;' does.

    P.S. Research int main() vs. void main(). There's enough discussion in past threads on the subject to fill the Library of Congress. (That missile being fired in Salem's avatar isn't targeted at void main(). It's targeted at the person who used it! )

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  10. #10
    Registered User
    Join Date
    Nov 2002
    Posts
    10
    Thanks for all the info skipper. And I'm glad to see you're ok and not out shopping for a new vehicle
    eskimos have feelings too...

  11. #11
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Thank you. But, methinks we've got a lot of winter ahead of us...as opposed to last year. Hope I'm wrong.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 08:01 PM
  2. Inserting new member into array of structs
    By cathym in forum C Programming
    Replies: 6
    Last Post: 04-17-2005, 08:10 AM
  3. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-12-2004, 12:15 AM
  4. Managing a heap using an array.
    By rahuls in forum C Programming
    Replies: 3
    Last Post: 03-20-2003, 02:49 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM

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