Thread: file reading

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    35

    Question file reading

    Hey all,
    I devised a program to read a file called student.txt that has in the firs line # of students and number of socres then has the scores
    student.txt:
    Code:
    3 3
    95 100 95
    90 85 95
    100 100 80
    here is the program
    Code:
    #include <iostream>    
    #include <conio.h>     
    #include <iostream>
    #include <fstream>
    #include <cassert>
    #include <vector>
    using namespace std;
    
    class FirstRow
    	{
    		int StudentCount;
           	int ScoreCount[5];
    };
    class Student
              {	
    	          char name[20];
    	          int mark[5];
    };
    
    int main(int argc, char *argv[]) 
    
    {       int lsStudentCount;
            int lsScoreCount[5];
            int *p;
            char lsstudent[5][20];
            int count = 0;
            int Row =1;
    
            Student sobj;
            FirstRow fstobj;
    
    		ifstream in("student.txt", ios::in);
            if(in.is_open()) 
    			{
    				in.read((char*) &fstobj, sizeof(fstobj));
    				lsStudentCount = fstobj.StudentCount;
    				lsScoreCount[0] = fstobj.ScoreCount[0];
    				lsScoreCount[1] = fstobj.ScoreCount[1];
    				lsScoreCount[2] = fstobj.ScoreCount[2];
    				lsScoreCount[3] = fstobj.ScoreCount[3];
    				lsScoreCount[4] = fstobj.ScoreCount[4];
               }
    
    		p = new(nothrow) int[lsStudentCount+1][6];
    		in.close();
    		ifstream inData("student.txt", ios::in);
    		if(in.is_open()) 
    		{
    			while(!in.eof()) 
    				{
    					if(Row == 1)
    					{
    						in.read((char*) &fstobj, sizeof(fstobj));
    						Row = 2;
    					}
    					else
      					{
    						in.read((char*) &sobj, sizeof(sobj));
    					if(!in.eof()) 
    					{
    						strcpy (lsstudent[count],sobj.name);
    						p[count][0] = sobj.[0];
    						p[count][1] = sobj.[1];
    						p[count][2] = sobj.[2];
    						p[count][3] = sobj.[3];
    						p[count][4] = sobj.[4];
    					}
    					count++;
    				}
    		}
    	}
    	in.close();
    	int studentsum = 0;
    	int studentpercentage;
    	int countx = 0;
    	int county = 0;
    
    	while(countx < lsStudentCount)
         { 
    		while(county <5)
                {
    				studentsum = studentsum + p[countx][county];
                 	county++;
    	   		}
           	studentpercentage = (studentsum * 100)/500;
           	p[countx][5] = studentpercentage;
          	studentsum = 0;
           	studentpercentage = 0;
           	county = 0;
           	countx++;
         }
    	int countx = 0;
    	int county = 0;
    	int scoresum = 0;
    	int scoreaverage;
    	while(county < 5)
         { 
    		while(countx < lsStudentCount)
                { 
    				scoresum = scoresum + p[countx][county];
                  	countx++;
               	}
    		scoreaverage = 	scoresum / lsScoreCount[county];
    		p[lsStudentCount][county] = scoreaverage;
    		scoresum = 0;
    		scoreaverage = 0;
    		countx = 0;
    		county++;
         }
    
    
    	int x = 0;
    	cout << "Student Results:";
    	while(x< lsStudentCount)
    		{ 
    			cout << p[x][5] << ": ";
           		cout << lsstudent[x] << "\n";
          		x++;
    		 }
    
    	int x = 0;
    	cout << "Score Results:";
    	while(x < 5)
    		{ 
    			cout << "1st score: ";
           		cout << p[lsStudentCount][x] << ": "<<"\n";
           		x++;
    		}
    }
    will not compile can anyone help

  2. #2
    Registered User
    Join Date
    Apr 2007
    Posts
    102
    what's your error?
    My Favorite Programming Line:
    Code:
    #define true ((rand() % 2) ? true : false)

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    errors
    Code:
    Error	1	error C2248: 'FirstRow::StudentCount' : cannot access private member declared inclass 'FirstRow'Error	
    2	error C2248: 'FirstRow::ScoreCount' : cannot access private member declared in class 'FirstRow'		
    Error	3	error C2248: 'FirstRow::ScoreCount' : cannot access private member declared in class 'FirstRow'		
    Error	4	error C2248: 'FirstRow::ScoreCount' : cannot access private member declared in class 'FirstRow'		
    Error	5	error C2248: 'FirstRow::ScoreCount' : cannot access private member declared in class 'FirstRow'		
    Error	6	error C2248: 'FirstRow::ScoreCount' : cannot access private member declared in class 'FirstRow'		
    Error	7	error C2440: '=' : cannot convert from 'int (*)[6]' to 'int'	
    Error	8	error C2248: 'Student::name' : cannot access private member declared in class 'Student'		
    Error	9	error C2109: subscript requires array or pointer type		
    Error	10	error C2059: syntax error : '['	
    Error	11	error C2039: 'p' : is not a member of 'Student'	
    Error	12	error C2039: 'count' : is not a member of 'Student'	
    Error	13	error C2039: 'sobj' : is not a member of 'Student'	
    Error	14	error C2059: syntax error : '['	
    Error	15	error C2039: 'p' : is not a member of 'Student'	
    Error	16	error C2039: 'count' : is not a member of 'Student'	
    Error	17	error C2039: 'sobj' : is not a member of 'Student'	
    Error	18	error C2059: syntax error : '['	
    Error	19	error C2039: 'p' : is not a member of 'Student'	
    Error	20	error C2039: 'count' : is not a member of 'Student'	
    Error	21	error C2039: 'sobj' : is not a member of 'Student'	
    Error	22	error C2059: syntax error : '['	
    Error	23	error C2039: 'p' : is not a member of 'Student'	
    Error	24	error C2039: 'count' : is not a member of 'Student'	
    Error	25	error C2039: 'sobj' : is not a member of 'Student'	
    Error	26	error C2059: syntax error : '['	
    Error	27	error C2039: 'count' : is not a member of 'Student'	
    Error	28	error C2039: 'in' : is not a member of 'Student'	
    Error	29	error C2039: 'close' : is not a member of 'Student'	
    Error	30	error C2109: subscript requires array or pointer type	
    Error	31	error C2109: subscript requires array or pointer type	
    Error	32	error C2374: 'countx' : redefinition; multiple initialization	
    Error	33	error C2374: 'county' : redefinition; multiple initialization	
    Error	34	error C2109: subscript requires array or pointer type	
    Error	35	error C2109: subscript requires array or pointer type	
    Error	36	error C2109: subscript requires array or pointer type	
    Error	37	error C2374: 'x' : redefinition; multiple initialization	
    Error	38	error C2109: subscript requires array or pointer type
    Last edited by gunghomiller; 08-06-2007 at 08:39 PM.

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    A class's variable's visibility is by default private, which means that you can't access it directly from outside of the class. To get around this, use the public: specifier.

    Example:
    Code:
    class myClass
    {
    public:
       // public variables
    };

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    thanks now I have these errors: can anyone provide insight?
    Code:
    Error	1	error C2440: '=' : cannot convert from 'int (*)[6]' to 'int'	
    Error	2	error C2109: subscript requires array or pointer type		
    Error	3	error C2059: syntax error : '['		
    Error	4	error C2039: 'p' : is not a member of 'Student'		
    Error	5	error C2039: 'count' : is not a member of 'Student'	
    Error	6	error C2039: 'sobj' : is not a member of 'Student'		
    Error	7	error C2059: syntax error : '['		
    Error	8	error C2039: 'p' : is not a member of 'Student'		
    Error	9	error C2039: 'count' : is not a member of 'Student'	
    Error	10	error C2039: 'sobj' : is not a member of 'Student'		
    Error	11	error C2059: syntax error : '['		
    Error	12	error C2039: 'p' : is not a member of 'Student'		
    Error	13	error C2039: 'count' : is not a member of 'Student'		
    Error	14	error C2039: 'sobj' : is not a member of 'Student'		
    Error	15	error C2059: syntax error : '['		
    Error	16	error C2039: 'p' : is not a member of 'Student'		
    Error	17	error C2039: 'count' : is not a member of 'Student'		
    Error	18	error C2039: 'sobj' : is not a member of 'Student'		
    Error	19	error C2059: syntax error : '['		
    Error	20	error C2039: 'count' : is not a member of 'Student'		
    Error	21	error C2039: 'in' : is not a member of 'Student'		
    Error	22	error C2039: 'close' : is not a member of 'Student'		
    Error	23	error C2109: subscript requires array or pointer type		
    Error	24	error C2109: subscript requires array or pointer type		
    Error	25	error C2374: 'countx' : redefinition; multiple initialization		
    Error	26	error C2374: 'county' : redefinition; multiple initialization		
    Error	27	error C2109: subscript requires array or pointer type		
    Error	28	error C2109: subscript requires array or pointer type		
    Error	29	error C2109: subscript requires array or pointer type		
    Error	30	error C2374: 'x' : redefinition; multiple initialization		
    Error	31	error C2109: subscript requires array or pointer type

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    When confronted with a list of errors, always look at the cause of the first one in the list, then try again when you've sorted that - oftentimes one error can cause a whole cascade of other 'phantom' errors to occur.

    In general, don't use arrays and pointers for something like this, they're really not needed, and seem to be causing you alot of confusion. You have already #include'd the <vector> header in your program, I strongly reccomend you save yourself alot of hassle and use that instead.
    Last edited by Bench82; 08-07-2007 at 02:37 PM.

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    ok ... not to sound like a smart butt but can someone help me that is why i came here i exhausted searching what the compiler outputed on the internet and i dont know what else to do to make it work

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    post the small part of the code where the error occured indicating in comment the exact line with the first error
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    Quote Originally Posted by vart View Post
    post the small part of the code where the error occured indicating in comment the exact line with the first error
    this is the line it is line 181
    line:
    Code:
    *p = new(nothrow) int[lsStudentCount+1][6];//error C2440: '=' : cannot convert from 'int (*)[6]' to 'int'

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    it should be
    p = new...

    and p should be declared as pointer to array of 6 ints, not as pointer to int if you want to allocate it this way
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM