hi
please help me.i wrote a program for student now i want rewrite with classes but i dont know how. :(
my program:

#include<iostream.h>
#include<string.h>


struct student
{ char fname[30];
  char lname[30];
  long ID;
  float avg;
 };typedef struct student t1;

 void getinfo(t1 s[],int n)
  { int i;
	 for(i=0;i<n;i++)
	 { cout<<"Enter firest name:"<<endl;
	  cin>>s[i].fname ;
	  cout<<"Enter last name:"<<endl;
	  cin>>s[i].lname;
	  cout<<"Enter student number:"<<endl;
	  cin>>s[i].ID;
	  cout<<"Enter average:"<<endl;
	  cin>>s[i].avg;}
	  }



	 long int search(t1 s[],int n,long int no)
	{
	  int i;
		  for(i=0;i<n;i++)
	  {  	if(no == s[i].ID)
		 return  i;
		 else
		return  -1;
		 };





		  char search (t1 s[],int n,char f[])
		 { int i;

			for(i=0;i<n;i++)
			if ((strcmp((s[i]),f))==0)
			 return( cout<<i<<endl; )
			else
			 return (cout<< "-1"<<endl; )
			 }


			void sortID (t1 s[],int n)
			{ int i; long g[20];
			  for(i=0;i<n;i++)
				{ if (s[i].ID>s[i+1].ID)
				  { g[i]=s[i].ID;
					 s[i].ID=s[i+1].ID;
					 s[i+1].ID=g[i];
					 };
					 };  }


		 void sortavg(t1 s[],int n)
			  {  int i; long g[20];
			  for(i=0;i<n;i++)
				{ if (s[i].avg>s[i+1].avg)
				  { g[i]=s[i].avg;
					 s[i].avg=s[i+1].avg;
					 s[i+1].avg=g[i];
					 };};
						}



				void sortname(t1 s[],int n)
				{ char g[30]; int i,j;
				  for(i=0;i<n-1;i++)
				  for(j=i+1;j<n;j++)
					if ((strcmp(a[i],a[j]))>0)
					{ strcpy(g,a[i]);
					  strcpy(a[i],a[j]);
					  strcpy(a[j],g);
					  }; };



			 void find (t1 s[],int n,int k)
				 {
					if(k==0)
					 search (t1 s[],int n,char f) ;
					 else if(k==1)
					 search (t1 s[],int n,long int no) ;
					 for(i=0;i<n;i++)
					 { cout<<t1.fname<<endl;
					 cout<<t1.lname<<endl;
					 cout<<t1.ID<<endl;
					 cout<<t1.avg<<endl; };
				 }


	main()
			{int n;
			 cin>>n;

			 int *s; long int no;char f;

			  s=new int[n];
           	t1 s[n];


			  cout<<"Please enter information"<<endl;

				getinfo(s,n);

			  sortID (t1 s[],int n);
			  sortavg(t1 s[],int n);
			  sortname(t1 s[],int n);

			  cout<<"please enter student number for search:"<<endl;
			  cin>>no;
			  long int search(t1 s[],int n,long int no);

			  cout<<"please enter student last name for search:"<<endl;
			  get.cin>>f;
			  char search (t1 s[],int n,char f[]);

			  cout<<"please enter student number or last name for search:"<<endl;
			  cout<<"Please input 0 or 1" <<endl;
			  cout<< "0 for student ID and 1 for last name"<<endl;
			  cin>>k;
			  find(t1 s[],int n,int k);






	 if( search(s,n,ID) != -1 )
	  cout << "Find";
	 else
	  cout << "Not Find";

			  delete s;  };   }
jonsca commented: Don't bump old threads -1

Recommended Answers

All 2 Replies

Well... I don't even know where to begin.
First off, what's up with the nonexistant formatting?
Second, I ran it through a formatter and tried to compile it... but I realized every second line triggers a compilation error, some of them are undoubtedly caused by the abysmal formatting (e.g. functions inside of other functions).
How about fixing all the mistakes before continuing? If you need help with that, be my guest.

student is already a class, by the way.

To grab an arbitrary function:

char search (t1 s[],int n,char f[])
{
    int i;

    for(i=0; i<n; i++) //don't declare counter variables outside the loop
        if ((strcmp((s[i]),f))==0) //what on earth are you trying to do? You can't treat a student instance
//as a C string - this obviously won't compile
            return( cout<<i<<endl; ) //you do know that cout is for writing on the console?
//You probably meant return i;
                  else
                      return (cout<< "-1"<<endl; ) // see above. Also, after the return statement there
//needs to be a semicolon - the lack of which caused this formatting
                         }

You should also read up on std::string and std::sort.

Here is your code in a somewhat readable form. Now you can easily see that all your functions are inside the first search function.

#include<iostream.h>
#include<string.h>


struct student
{
    char fname[30];
    char lname[30];
    long ID;
    float avg;
};
typedef struct student t1;

void getinfo(t1 s[],int n)
{
    int i;
    for(i=0; i<n; i++)
    {
        cout<<"Enter firest name:"<<endl;
        cin>>s[i].fname ;
        cout<<"Enter last name:"<<endl;
        cin>>s[i].lname;
        cout<<"Enter student number:"<<endl;
        cin>>s[i].ID;
        cout<<"Enter average:"<<endl;
        cin>>s[i].avg;
    }
}



long int search(t1 s[],int n,long int no)
{
    int i;
    for(i=0; i<n; i++)
    {
        if(no == s[i].ID)
            return  i;
        else
            return  -1;
    };





    char search (t1 s[],int n,char f[])
    {
        int i;

        for(i=0; i<n; i++)
            if ((strcmp((s[i]),f))==0)
                return( cout<<i<<endl; )
                      else
                          return (cout<< "-1"<<endl; )
                             }


                 void sortID (t1 s[],int n)
    {
        int i;
        long g[20];
        for(i=0; i<n; i++)
        {
            if (s[i].ID>s[i+1].ID)
            {
                g[i]=s[i].ID;
                s[i].ID=s[i+1].ID;
                s[i+1].ID=g[i];
            };
        };
    }


    void sortavg(t1 s[],int n)
    {
        int i;
        long g[20];
        for(i=0; i<n; i++)
        {
            if (s[i].avg>s[i+1].avg)
            {
                g[i]=s[i].avg;
                s[i].avg=s[i+1].avg;
                s[i+1].avg=g[i];
            };
        };
    }



    void sortname(t1 s[],int n)
    {
        char g[30];
        int i,j;
        for(i=0; i<n-1; i++)
            for(j=i+1; j<n; j++)
                if ((strcmp(a[i],a[j]))>0)
                {
                    strcpy(g,a[i]);
                    strcpy(a[i],a[j]);
                    strcpy(a[j],g);
                };
    };



    void find (t1 s[],int n,int k)
    {
        if(k==0)
            search (t1 s[],int n,char f) ;
        else if(k==1)
            search (t1 s[],int n,long int no) ;
        for(i=0; i<n; i++)
        {
            cout<<t1.fname<<endl;
            cout<<t1.lname<<endl;
            cout<<t1.ID<<endl;
            cout<<t1.avg<<endl;
        };
    }


    main()
    {
        int n;
        cin>>n;

        int *s;
        long int no;
        char f;

        s=new int[n];
        t1 s[n];


        cout<<"Please enter information"<<endl;

        getinfo(s,n);

        sortID (t1 s[],int n);
        sortavg(t1 s[],int n);
        sortname(t1 s[],int n);

        cout<<"please enter student number for search:"<<endl;
        cin>>no;
        long int search(t1 s[],int n,long int no);

        cout<<"please enter student last name for search:"<<endl;
        get.cin>>f;
        char search (t1 s[],int n,char f[]);

        cout<<"please enter student number or last name for search:"<<endl;
        cout<<"Please input 0 or 1" <<endl;
        cout<< "0 for student ID and 1 for last name"<<endl;
        cin>>k;
        find(t1 s[],int n,int k);






        if( search(s,n,ID) != -1 )
            cout << "Find";
        else
            cout << "Not Find";

        delete s;
    };
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.