I have met a proplem in correcting the following file and please submit to me by correcting the errores and if you can please help me in sorting and updating this file

#include <iostream>
#include<fstream>
#include<string.h>
using namespace std;
struct gum
{
    string nam,fnam,typ_item,nam_pack,coun_cons;
    int tin_no,reg_no,tot_item,tot_pack;
    float weigh;
};
void write(gum e[],int siz);
string sort(gum f[],int siz);
void updat(gum g[],int siz);
void cpy(gum k[],int siz);


int main()
{
    int a,i=0;
    struct gum ey[4];
    while (i<2)
    {
        cout<<" enter name"<<endl;
        cin>>ey[i].nam;
        cout<<"enter father name "<<endl;
        cin>>ey[i].fnam;
        cout<<"enter tax identification number"<<endl;
        cin>>ey[i].tin_no;
        cout<<"\nenter enter total items"<<endl;
        cin>>ey[i].tot_item;
        cout<<"enter type of items"<<endl;
        cin>>ey[i].typ_item;
        cout<<"enter type\name of packeges"<<endl;
        cin>>ey[i].nam_pack;
        cout<<"enter total packeges"<<endl;
        cin>>ey[i].tot_pack;
        cout<<"enter country of consienment "<<endl;
        cin>>ey[i].coun_cons;
        cout<<"enter total weight"<<endl;
        cin>>ey[i].weigh;

        i++;
    }
    write(ey,2);//since we can write in the sort function below it is optional i.e we can live it
    sort(ey,2);
    cout<<"enter 1 to update,2 to copy"<<endl;
    cin>>a;
    switch(a)
    {
        case 1:
        updat(ey,2);
        break;
        case 2:
        cpy(ey,2);
        break;
    }
    return 0;
}

//WRITING
void write(gum e[],int siz)
   {
    ofstream eyob("d:\\gumeruk.text",ios::out);
    for(int i=0;i<siz;i++)
    {
        eyob<<e[i].nam<<"\n"<<e[i].fnam<<"\n"<<e[i].tin_no<<"\n"<<e[i].tot_item<<"\n"<<e[i].typ_item<<"\n"<<e[i].nam_pack<<"\n"<<e[i].tot_pack<<e[i].coun_cons<<"\n"<<e[i].weigh;
    }
   }

//SORTING
string sort (gum f[],int siz)
{
    ofstream fek("d:\\gumeruk.text",ios::out);
    for (int i=0;i<siz;i++)
    {
        for(int j=i+1;j<siz;j++)
        {
        if(strcmp(f[i].nam,f[j].nam)==0)            //help me
        {
            if(strcmp(f[i].fnam,f[j].fnam)==-1)       //help me
            return f[i].nam;
        }
        else
        if(strcmp(f[i].nam,f[j].nam)==-1)              //help me
        return f[i].nam;
        }
        fek<<f[i].nam<<"\n"<<f[i].fnam<<"\n"<<f[i].tin_no<<"\n"<<f[i].tot_item<<"\n";
        fek<<f[i].typ_item<<"\n"<<f[i].nam_pack<<"\n"<<f[i].tot_pack<<"\n"<<f[i].coun_cons<<"\n"<<f[i].weigh;
    }
}

//UPDATING
void updat(gum g[],int siz)
{
    ifstream kal;
    kal.open("d:\\gumeruk.text",ios::in);
    int k,tin;
    cout<<"enter tax id number"<<endl;
    cin>>tin;
    for(int i=0;i<siz;i++)
    {
        if(tin==g[i].tin_no)
        {
            cout<<"enter the field code to be updated"<<endl;
            cin>>k;
            switch(k)
            {
            case 1:
            {
                kal>>g[i].nam;
                cout<<g[i].nam;
                break;
            }
            case 2:
            {
                kal>>g[i].fnam;
                cout<<g[i].fnam;
                break;
            }
            case 3:
            {
                kal>>g[i].tin_no;
                cout<<g[i].tin_no;
                break;
            }
            case 4:
            {
                kal>>g[i].tot_item;
                cout<<g[i].tot_item;
                break;
            }
            case 5:
            {
                kal>>g[i].typ_item;
                cout<<g[i].typ_item;
                break;
            }
            case 6:
            {
                kal>>g[i].nam_pack;
                cout<<g[i].nam_pack;
                break;
            }
            case 7:
            {
                kal>>g[i].tot_pack;
                cout<<g[i].tot_pack;
                break;
            }
            case 8:
            {
                kal>>g[i].coun_cons;
                cout<<g[i].coun_cons;
                break;
            }
            case 9:
            {
                kal>>g[i].weigh;
                cout<<g[i].weigh;
                break;
            }
            default :
                cout<<"enter the field again"<<endl;
                cin>>k;
                continue;
            }
        }
        else cout<<" invalid tin id please try again"<<endl;
        cin>>tin;
        continue;
    }
}
//COPYING
void cpy(gum k[],int siz)
{

    char sto_nam;
    cout<<"enter the name of the storage apropriatly"<<endl;
    cin>>sto_nam;
    ofstream geta("d:\\gumeruk.text",ios::out);
    for(int i=0;i<siz;i++)
    {
    geta<<k[i].nam<<"\n"<<k[i].fnam<<"\n"<<k[i].tin_no<<"\n"<<k[i].tot_item<<"\n"<<k[i].typ_item;
    geta<<k[i].nam_pack<<"\n"<<k[i].tot_pack<<"\n"<<k[i].coun_cons<<"\n"<<k[i].weigh;
    }
}


}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Please use code tags [code] //code here [/code].

Your sort function is not actually sorting anything. You should pass in the array of structs and not return anything. Look up bubble sort and you should find lots of examples, but basically you do

temp = a
a = b
b = temp

with the array elements (you can assign the structs directly to one another).

The best thing to do is create and test each of your functions separately and then integrate the whole package.

I guess you need to declare gum as a pointer or you will have to pass gum's pointer to each function (call by reference) in order to deal with it directly. For sorting algorithm, you could use Google to look for any sort algorithm. I usually use Gnome sort because it is better than Bubble sort in general cases, but it may be a little bit more complex than Bubble sort for you to understand.

This article has been dead for over six months. Start a new discussion instead.