pointer to array of objects of struct

This is a discussion on pointer to array of objects of struct within the C++ Programming forums, part of the General Programming Boards category; Hi all .. i need urgent help please i made the following struct Code: struct student { char name[10]; // ...

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    127

    pointer to array of objects of struct

    Hi all .. i need urgent help please
    i made the following struct
    Code:
    struct student
    {
    	char name[10];   // for the name of the student
        int BN;// for the bench number
    	int year ,section,exam;
      double mark;
    }; //for the mark of the student
    in the main function i tried to make the following
    Code:
    int i;
    cout<<"enter i ";
    cin>>i;
    
     student *p =new student[i];
      int j=0;
    cout<<"enter the name:";
    cin>>p[j].name;
    but it fails

    what is the problem with the pointer of the array of students

    it gives error
    so anyone can help me please
    thanks in advance

  2. #2
    Registered User
    Join Date
    Feb 2004
    Posts
    127
    hey all



    please help e in this problem .. i didnt find any reply ..

    is it wrong to make a pointer to array of objects of a struct or what is wrong in the code?
    this is the whole cod of the program
    Code:
    
    #include<iostream>
    #include<cstdlib>
    #include<fstream>
    #include<iomanip>
    using namespace std;
    void sort_fn(student k,int size);
    void show_fn(student k ,int size);
    
    struct student
    {
    	char name[10];   // for the name of the student
        int BN;// for the bench number
    	int year ,section,exam;
      double mark;
    }; //for the mark of the student
    
    
    int main()
    {
    	student m;
    	int range[6]={0,0,0,0,0,0};
    	double sum ,av; // sum is the sum of the marks of students and av is their average
    	int i=0;  //counting the nuber of students
    	
    	sum =0;
        ifstream fin;
        ofstream fout;
        fin.open("E://mark.txt");
        if (fin.fail())
    	{
           cout<<"Can not open file\n";
            exit(1);
    	}
      fout.open("E://results.txt");
      fin>>m.name;
      while(!fin.eof())
      {
    	  fout<<m.name;
    	  fout<<"  ";     //space
    	  fin>>m.BN;
    	  fout<<m.BN;
    	  fout<<"  ";
    	  fin>>m.year;
    	  fout<<m.year;
    	  fout<<"  ";
    	  fin>>m.section;
    	  fout<<m.section;
    	  fout<<"\t";
    	  fin>>m.exam;
    	  fout<<m.exam;
    	  fout<<"\t";
    	  fin>>m.mark;
    	  if ((m.mark>=0)&&(m.mark<5))
    		  range[0]++;
    	  else if((m.mark>=5)&&(m.mark<10))
    		  range[1]++;
    	  else if((m.mark>=10)&&(m.mark<15))
    		  range[2]++;
    	  else if((m.mark>=15)&&(m.mark<20))
    		  range[3]++;
    	  else if ((m.mark>20)&&(m.mark<25))
    		  range[4]++;
    	  else
    		  range[5]++;
    
    	  sum+=m.mark;
    	  fout<<m.mark;
    	  fout<<endl;
    	  i++;
    	  fin>>m.name;
      }
      av=sum/i;
      fout<<" the average of the students is :"<<av <<endl;
      fout<<" the number of students having score between 0 and less than 5 :"<<range[0]<<endl;
      fout<<" the number of students having score between 5 and less than 10 :"<<range[1]<<endl;
      fout<<" the number of students having score between 10 and less than 15 :"<<range[2]<<endl;
      fout<<" the number of students having score between 15 and less than 20 :"<<range[3]<<endl;
      fout<<" the number of students having score between 20 and less than 25 :"<<range[4]<<endl;
      fout<<" the number of students having score between 20 and 30       :"<<range[5]<<endl;
      student *p =new student[i];
      int j=0;
      fin>>p[j].name;
      while(!fin.eof())
      {
    	 
    	  fin>>p[j].BN;
    	  
    	
    	  fin>>p[j].year;
    	  
    	  fin>>p[j].section;
    	 
    	  fin>>p[j].exam;
    	 
    	  fin>>p[j].mark;
    	 
    	   
    	 j++;
    	  fin>>p[j].name;
      }
      sort_fn(p,i);
      show_fn(p,i);
    
      fin.close();
      fout.close();
      delete []p;
      return 0;
      
    
    }
    
    void sort_fn(student & k,int size)
    {
    	student temp;
    	for (int i=0 ;i<size-1 ;i++)
    	{
    		for (int j=i+1;j<size ;j++)
    		{
    			if (k[i].mark<k[j].mark)
    			{
    				temp=k[i];
    				k[i]=k[j];
    				k[j]=temp;
    			}
    		}
    	}
    }
    
    void show_fn(student & k ,int size)
    {
    	for (int i=0;i<size;i++)
    		cout<<k[i].name<<setw(10)<<k[i].section<<setw(15)<<k[i].mark<<endl;
    }

    thanks in advance

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    first if all, please reread the forum rules, esspecialy the one partaining to "bumping" of threads.

    Secodnly you are passing a pointer by reference, which is not allowed. I've fixed the code, and it is below...I'm not at a pc with a compiler at the moment so don't know if it does what you want....it should compile though.

    Code:
    #include<iostream>
    #include<cstdlib>
    #include<fstream>
    #include<iomanip>
    using namespace std;
    
    struct student
    {
    	char name[10];   // for the name of the student
        int BN;// for the bench number
    	int year ,section,exam;
      double mark;
    }; //for the mark of the student
    
    void sort_fn(student *k,int size)
    {
    	student temp;
    	for (int i=0 ;i<size-1 ;i++)
    	{
    		for (int j=i+1;j<size ;j++)
    		{
    			if (k[i].mark<k[j].mark)
    			{
    				temp=k[i];
    				k[i]=k[j];
    				k[j]=temp;
    			}
    		}
    	}
    }
    
    void show_fn(student *k ,int size)
    {
    	for (int i=0;i<size;i++)
    		cout<<k[i].name<<setw(10)<<k[i].section<<setw(15)<<k[i].mark<<endl;
    }
    
    int main()
    {
    	student m;
    	int range[6]={0,0,0,0,0,0};
    	double sum ,av; // sum is the sum of the marks of students and av is their average
    	int i=0;  //counting the nuber of students
    	
    	sum =0;
        ifstream fin;
        ofstream fout;
        fin.open("E://mark.txt");
        if (fin.fail())
    	{
           cout<<"Can not open file\n";
            exit(1);
    	}
      fout.open("E://results.txt");
      fin>>m.name;
      while(!fin.eof())
      {
    	  fout<<m.name;
    	  fout<<"  ";     //space
    	  fin>>m.BN;
    	  fout<<m.BN;
    	  fout<<"  ";
    	  fin>>m.year;
    	  fout<<m.year;
    	  fout<<"  ";
    	  fin>>m.section;
    	  fout<<m.section;
    	  fout<<"\t";
    	  fin>>m.exam;
    	  fout<<m.exam;
    	  fout<<"\t";
    	  fin>>m.mark;
    	  if ((m.mark>=0)&&(m.mark<5))
    		  range[0]++;
    	  else if((m.mark>=5)&&(m.mark<10))
    		  range[1]++;
    	  else if((m.mark>=10)&&(m.mark<15))
    		  range[2]++;
    	  else if((m.mark>=15)&&(m.mark<20))
    		  range[3]++;
    	  else if ((m.mark>20)&&(m.mark<25))
    		  range[4]++;
    	  else
    		  range[5]++;
    
    	  sum+=m.mark;
    	  fout<<m.mark;
    	  fout<<endl;
    	  i++;
    	  fin>>m.name;
      }
      av=sum/i;
      fout<<" the average of the students is :"<<av <<endl;
      fout<<" the number of students having score between 0 and less than 5 :"<<range[0]<<endl;
      fout<<" the number of students having score between 5 and less than 10 :"<<range[1]<<endl;
      fout<<" the number of students having score between 10 and less than 15 :"<<range[2]<<endl;
      fout<<" the number of students having score between 15 and less than 20 :"<<range[3]<<endl;
      fout<<" the number of students having score between 20 and less than 25 :"<<range[4]<<endl;
      fout<<" the number of students having score between 20 and 30       :"<<range[5]<<endl;
      student *p = new student[i];
      int j=0;
      fin>>p[j].name;
      while(!fin.eof())
      {
    	 
    	  fin>>p[j].BN;
    	  
    	
    	  fin>>p[j].year;
    	  
    	  fin>>p[j].section;
    	 
    	  fin>>p[j].exam;
    	 
    	  fin>>p[j].mark;
    	 
    	   
    	 j++;
    	  fin>>p[j].name;
      }
      sort_fn(p,i);
      show_fn(p,i);
    
      fin.close();
      fout.close();
      delete [] p;
      
      
      return 0;
      
    
    }

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  4. #4
    Registered User
    Join Date
    Feb 2004
    Posts
    127
    Thanks man

    it didnt give errorsin compilation but still didnt give results where that means that it didnt work

    and really can you tell me please what is the problems with my first code where i noticed that all you have done is that you put the other functions before main function and eventually you said that i am passing a pointer by reference which as you said is un allowed so how can i pass it?
    do i have to pass it by value?

    thanks in advance

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > fout<<" the average of the students is :"<<av <<endl;
    > fout<<" the number of students having score between 0 and less than 5 :"<<range[0]<<endl;
    > fout<<" the number of students having score between 5 and less than 10 :"<<range[1]<<endl;
    > fout<<" the number of students having score between 10 and less than 15 :"<<range[2]<<endl;
    > fout<<" the number of students having score between 15 and less than 20 :"<<range[3]<<endl;
    > fout<<" the number of students having score between 20 and less than 25 :"<<range[4]<<endl;
    > fout<<" the number of students having score between 20 and 30 :"<<range[5]<<endl;
    > student *p =new student[i];
    > int j=0;
    > fin>>p[j].name;
    > while(!fin.eof())

    You need to seek back to the beginning of the file here:
    Code:
      fout<<" the average of the students is :"<<av <<endl;
      fout<<" the number of students having score between 0 and less than 5 :"<<range[0]<<endl;
      fout<<" the number of students having score between 5 and less than 10 :"<<range[1]<<endl;
      fout<<" the number of students having score between 10 and less than 15 :"<<range[2]<<endl;
      fout<<" the number of students having score between 15 and less than 20 :"<<range[3]<<endl;
      fout<<" the number of students having score between 20 and less than 25 :"<<range[4]<<endl;
      fout<<" the number of students having score between 20 and 30       :"<<range[5]<<endl;
      student *p =new student[i];
      int j=0;
      fin.clear();
      fin.seekg(0);
      fin>>p[j].name;
      while(!fin.eof())

  6. #6
    Registered User
    Join Date
    Feb 2004
    Posts
    127
    thanks for ur reply

    but can u tell me what does that do?
    Code:
    fin.clear();
    fin.seekg(0);
    anyway the problem still exist with the same errors as before

    Thanks in advance

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by M_Ghani

    but can u tell me what does that do?
    Code:
    fin.clear();
    fin.seekg(0);
    Look 'em up here: http://www.cppreference.com/cppio.html
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >anyway the problem still exist with the same errors as before

    What errors are you getting?
    1) Did the code compile? If not, what are the error messages?
    2) If the code compiled, what happens when you run the program?
    3) Post a copy of your E:\\mark.txt file

    >fin.open("E://mark.txt");

    It may not matter, but your forward slashes should be backslashes:

    fin.open("E:\\mark.txt");

  9. #9
    Registered User
    Join Date
    Feb 2004
    Posts
    127
    I dont think it's a problem of slahes and backslashes because this program was working well when i use it only to write the average of marks in a file but when it comes to sorting the errors are so much

    the code didnt compile and it makes 102 errors (I cant imagine that myself .. maybe a new world record )
    the error messages are shown below
    Compiling...
    d.cpp
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(8) : error C2065: 'student' : undeclared identifier
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(8) : error C2146: syntax error : missing ')' before identifier 'k'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(8) : error C2182: 'sort_fn' : illegal use of type 'void'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(8) : error C2059: syntax error : ')'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(9) : error C2146: syntax error : missing ')' before identifier 'k'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(9) : error C2182: 'show_fn' : illegal use of type 'void'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(9) : error C2059: syntax error : ')'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(22) : error C2146: syntax error : missing ';' before identifier 'm'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(22) : error C2065: 'm' : undeclared identifier
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(37) : error C2228: left of '.name' must have class/struct/union type
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(40) : error C2228: left of '.name' must have class/struct/union type
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(42) : error C2228: left of '.BN' must have class/struct/union type
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(43) : error C2228: left of '.BN' must have class/struct/union type
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(45) : error C2228: left of '.year' must have class/struct/union type
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(46) : error C2228: left of '.year' must have class/struct/union type
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(48) : error C2228: left of '.section' must have class/struct/union type
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(49) : error C2228: left of '.section' must have class/struct/union type
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(51) : error C2228: left of '.exam' must have class/struct/union type
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(52) : error C2228: left of '.exam' must have class/struct/union type
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(54) : error C2228: left of '.mark' must have class/struct/union type
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(55) : error C2228: left of '.mark' must have class/struct/union type
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(55) : error C2228: left of '.mark' must have class/struct/union type
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(57) : error C2228: left of '.mark' must have class/struct/union type
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(57) : error C2228: left of '.mark' must have class/struct/union type
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(59) : error C2228: left of '.mark' must have class/struct/union type
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(59) : error C2228: left of '.mark' must have class/struct/union type
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(61) : error C2228: left of '.mark' must have class/struct/union type
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(61) : error C2228: left of '.mark' must have class/struct/union type
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(63) : error C2228: left of '.mark' must have class/struct/union type
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(63) : error C2228: left of '.mark' must have class/struct/union type
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(68) : error C2228: left of '.mark' must have class/struct/union type
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(69) : error C2228: left of '.mark' must have class/struct/union type
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(72) : error C2228: left of '.name' must have class/struct/union type
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(83) : error C2065: 'p' : undeclared identifier
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(83) : error C2061: syntax error : identifier 'student'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(87) : error C2109: subscript requires array or pointer type
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(87) : error C2228: left of '.name' must have class/struct/union type
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : error C2143: syntax error : missing ';' before '{'
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(89) : fatal error C1003: error count exceeds 100; stopping compilation
    Error executing cl.exe.

    d.exe - 102 error(s), 0 warning(s)


    about the code it's
    Code:
    
    #include<iostream>
    #include<cstdlib>
    #include<fstream>
    #include<iomanip>
    using namespace std;
    void sort_fn(student k,int size);
    void show_fn(student k ,int size);
    
    struct student
    {
    	char name[10];   // for the name of the student
        int BN;// for the bench number
    	int year ,section,exam;
      double mark;
    }; //for the mark of the student
    
    
    int main()
    {
    	student m;
    	int range[6]={0,0,0,0,0,0};
    	double sum ,av; // sum is the sum of the marks of students and av is their average
    	int i=0;  //counting the nuber of students
    	
    	sum =0;
        ifstream fin;
        ofstream fout;
        fin.open("E://mark.txt");
        if (fin.fail())
    	{
           cout<<"Can not open file\n";
            exit(1);
    	}
      fout.open("E://results.txt");
      fin>>m.name;
      while(!fin.eof())
      {
    	  fout<<m.name;
    	  fout<<"  ";     //space
    	  fin>>m.BN;
    	  fout<<m.BN;
    	  fout<<"  ";
    	  fin>>m.year;
    	  fout<<m.year;
    	  fout<<"  ";
    	  fin>>m.section;
    	  fout<<m.section;
    	  fout<<"\t";
    	  fin>>m.exam;
    	  fout<<m.exam;
    	  fout<<"\t";
    	  fin>>m.mark;
    	  if ((m.mark>=0)&&(m.mark<5))
    		  range[0]++;
    	  else if((m.mark>=5)&&(m.mark<10))
    		  range[1]++;
    	  else if((m.mark>=10)&&(m.mark<15))
    		  range[2]++;
    	  else if((m.mark>=15)&&(m.mark<20))
    		  range[3]++;
    	  else if ((m.mark>20)&&(m.mark<25))
    		  range[4]++;
    	  else
    		  range[5]++;
    
    	  sum+=m.mark;
    	  fout<<m.mark;
    	  fout<<endl;
    	  i++;
    	  fin>>m.name;
      }
      av=sum/i;
      fout<<" the average of the students is :"<<av <<endl;
      fout<<" the number of students having score between 0 and less than 5 :"<<range[0]<<endl;
      fout<<" the number of students having score between 5 and less than 10 :"<<range[1]<<endl;
      fout<<" the number of students having score between 10 and less than 15 :"<<range[2]<<endl;
      fout<<" the number of students having score between 15 and less than 20 :"<<range[3]<<endl;
      fout<<" the number of students having score between 20 and less than 25 :"<<range[4]<<endl;
      fout<<" the number of students having score between 20 and 30       :"<<range[5]<<endl;
    
      student *p =new student[i];
      int j=0;
      fin.clear();
      fin.seekg(0);
      fin>>p[j].name;
      while(!fin.eof())
      {
    	 
    	  fin>>p[j].BN;
    	  
    	
    	  fin>>p[j].year;
    	  
    	  fin>>p[j].section;
    	 
    	  fin>>p[j].exam;
    	 
    	  fin>>p[j].mark;
    	 
    	   
    	 j++;
    	  fin>>p[j].name;
      }
      sort_fn(p,i);
      show_fn(p,i);
    
      fin.close();
      fout.close();
      delete []p;
      return 0;
      
    
    }
    
    void sort_fn(student & k,int size)
    {
    	student temp;
    	for (int i=0 ;i<size-1 ;i++)
    	{
    		for (int j=i+1;j<size ;j++)
    		{
    			if (k[i].mark<k[j].mark)
    			{
    				temp=k[i];
    				k[i]=k[j];
    				k[j]=temp;
    			}
    		}
    	}
    }
    
    void show_fn(student & k ,int size)
    {
    	for (int i=0;i<size;i++)
    		cout<<k[i].name<<setw(10)<<k[i].section<<setw(15)<<k[i].mark<<endl;
    }

    thanks in advance

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >void sort_fn(student k,int size);
    >void show_fn(student k ,int size);
    
    Notice at this point student is undefined.
    
    >
    >struct student
    
    Now you define student.
    
    >{
    >	char name[10];   // for the name of the student
    >    int BN;// for the bench number
    >	int year ,section,exam;
    >  double mark;
    >}; //for the mark of the student
    Instead define student first:
    Code:
    struct student
    {
    	char name[10];   // for the name of the student
        int BN;// for the bench number
    	int year ,section,exam;
      double mark;
    }; //for the mark of the student
    
    void sort_fn(student k,int size);
    void show_fn(student k ,int size);

  11. #11
    Registered User
    Join Date
    Feb 2004
    Posts
    127
    thanks alot really i didnt take care about that .. thanks alot

    but there are still errors found

    Compiling...
    d.cpp
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(108) : error C2664: 'sort_fn' : cannot convert parameter 1 from 'struct student *' to 'struct student'
    No constructor could take the source type, or constructor overload resolution was ambiguous
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(109) : error C2664: 'show_fn' : cannot convert parameter 1 from 'struct student *' to 'struct student'
    No constructor could take the source type, or constructor overload resolution was ambiguous
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(126) : error C2676: binary '[' : 'struct student' does not define this operator or a conversion to a type acceptable to the predefined operator
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(126) : error C2228: left of '.mark' must have class/struct/union type
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(126) : error C2676: binary '[' : 'struct student' does not define this operator or a conversion to a type acceptable to the predefined operator
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(126) : error C2228: left of '.mark' must have class/struct/union type
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(128) : error C2676: binary '[' : 'struct student' does not define this operator or a conversion to a type acceptable to the predefined operator
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(129) : error C2676: binary '[' : 'struct student' does not define this operator or a conversion to a type acceptable to the predefined operator
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(129) : error C2676: binary '[' : 'struct student' does not define this operator or a conversion to a type acceptable to the predefined operator
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(130) : error C2676: binary '[' : 'struct student' does not define this operator or a conversion to a type acceptable to the predefined operator
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(139) : error C2676: binary '[' : 'struct student' does not define this operator or a conversion to a type acceptable to the predefined operator
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(139) : error C2228: left of '.name' must have class/struct/union type
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(139) : error C2676: binary '[' : 'struct student' does not define this operator or a conversion to a type acceptable to the predefined operator
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(139) : error C2228: left of '.section' must have class/struct/union type
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(139) : error C2676: binary '[' : 'struct student' does not define this operator or a conversion to a type acceptable to the predefined operator
    E:\Program Files\Microsoft Visual Studio\MyProjects\dsdsds\d.cpp(139) : error C2228: left of '.mark' must have class/struct/union type
    Error executing cl.exe.

    d.exe - 16 error(s), 0 warning(s)


    thanks in advance

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >void sort_fn(student k,int size);
    >void show_fn(student k ,int size);

    You are passing an array of student structs, so:
    void sort_fn(student k[],int size);
    void show_fn(student k[] ,int size);

    And don't forget to change these:
    >void sort_fn(student & k,int size)
    >void show_fn(student & k ,int size)

    void sort_fn(student k[],int size)
    void show_fn(student k[] ,int size)

  13. #13
    Registered User
    Join Date
    Feb 2004
    Posts
    127
    thanks alot

    it works now

    thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. struct pointer
    By t014y in forum C Programming
    Replies: 5
    Last Post: 01-26-2009, 02:50 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. pointer problem or so...
    By TL62 in forum C Programming
    Replies: 19
    Last Post: 01-12-2008, 10:45 PM
  4. Pointer to array of string and Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 06-15-2007, 06:04 AM
  5. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM

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