Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Learning c++

Name: Help 2013-05-29 17:48

I suck at programming but Im trying hard.

So I'm trying to learn c++, and basically I have a program that needs to open a file, and read the 4 columns into 4 separate Arrays. If the user wants to search by last name then the data must be sorted by last name and first name. If the user wants to search by ID number then the data must be sorted by ID number. Then when the user wants to stop searching, it should count the number of rows (records) in the file. When the user quits, the program should write the data to a new file, sorted by last name.

I figure I need 3 functions, One for sorting on last name and first name, one for sorting in ID number and one for swapping the data when sorting. Here's what I have so far, for some reason its just breaking, no error or anything.

Im new so be nice please.

int swap(string f[], int x)
{
    string temp;
    temp=f[x];
    f[x]=f[x+1];
    f[x+1]=temp;
    return x;
}
int main()
{
    int i, j, length;
    string first[50], last[50], from[50], num[50], search[5], store[50]={0};
    bool name=false, number=false;
    ifstream fin;
    fin.open("C:\\Users\\Jake\\Desktop\\school\\q3\\inter programmin\\Database.txt");
    i=0;
    while(!fin.eof())
    {
        fin >> first[i] >> last[i] >> from[i] >> num[i];
        i++;
    }
    length=i;
    i=0;
    cout << "enter a last name (first letter cap) or an ID number: ";
    cin >> search[1];
    j=0;
    for(i=0; i<length; i++)
    {
        if(last[i]==search[1])
        {
            cout << i << ": " << first[i] << " " << last[i] << " " << from[i] << " " << num[i] << endl;
            store[j]=i;
            name=true;
        }
        if(num[i]==search[1])
        {
            cout << i << ": " << first[i] << " " << last[i] << " " << from[i] << " " << num[i] << endl;
            store[j]=i;
            number=true;
        }
        j++;
    }
    for(i=0; i<j; i++)
    {
        if(name)
        {
            if(last[store[i]]>last[store[i]])
            {
                swap(first[store[i]], store[i]);
            }
        }
        if(number)
        {
            if(num[store[i]]>num[store[i]])
            {
                swap(first[store[i]], store[i]);
            }
        }
    }
    cin.get();
    return 0;
}

Name: Anonymous 2013-05-30 5:50

FYI here's how you swap two variables:
A -= (B = (A += B) - B)

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List