My code is not reading my data file well. The code is supposed to read fractions from decending order, but it does not work half way through.

please help!

#include <iostream>
#include <fstream>
using namespace std;
void read_data(int nums[],int den[],int size);
void clean(int Cnum[],int nums[],int den[],int Cden[],int size);
void low(int Cnum[],int Cden[],int size);
void sort(int Cnum[],int Cden[], int size);
void report(int Cnum[],int Cden[],int nums[],int den[],int size);


int main()

{   
    const int A_SIZE = 12;
    int num[A_SIZE];
    int den[A_SIZE];
    int Cnum[A_SIZE];
    int Cden[A_SIZE];

    read_data(num,den,A_SIZE);
    report(Cnum,Cden,num,den,A_SIZE);



    clean(Cnum,num,den,Cden,A_SIZE);
    low(Cnum,Cden,A_SIZE);

    cout<<endl<<endl;
    cout<< " The sorted values, in decending order:" << endl;
    sort(Cnum,Cden,A_SIZE);




return 0;

}



void read_data(int nums[],int den[],int size)
{
    ifstream dataIn;
    dataIn.open("data.txt");
    if(dataIn.fail())
    {
        cout << "File does not exist." << endl;
        exit(1);
    }

    int count;
    for (count=0;  count<size;  count++)
    {
        dataIn >>nums[count];

    }
    for (count=0; count <size; count++)
    {

        dataIn >>den[count];
    }


    dataIn.close();
}
void clean(int Cnum[],int nums[],int den[],int Cden[],int size)
{
    cout << endl << endl;
    cout << " The good fractions are:" << endl;
    int j = 0;
    for (int index = 0; index < size; index++)
    {
        if (den[index] != 0)
        {
            Cnum[j] = nums[index];
            Cden[j] = den[index];


            cout << " Fractions :" << "  " <<Cnum[j]<<"/"<<Cden[j]<<" = " <<static_cast<double> (Cnum[j])/Cden[j]<<endl;
            j++;
        }

    }


}
void low(int Cnum[],int Cden[],int size)
{
    int loc=0;
    double lowest;
    lowest = Cnum[0]/Cden[0];
    for (int c=1; c < size; c++)
    {
        if(Cnum[c]/Cden[c] < lowest)
        {
            loc = c;
            lowest = Cnum[c]/Cden[c];

        }

    }
    cout << endl << endl;
    cout << "The fraction with the lowest value is :"<<Cnum[loc]<<"/"<<Cden[loc]<< endl;



}
void sort(int Cnum[], int Cden[], int size)
{

    int Ntemp,Dtemp;
    bool swap;


    do
    {   swap=false;

        for (int C=0; C < (size-1) ; C++)
        {
            if(Cnum[C]/Cden[C]<Cnum[C+1]/Cden[C+1])
            {
                Ntemp=Cnum[C];
                Dtemp=Cden[C];
                Cnum[C]=Cnum[C+1];
                Cden[C]=Cden[C+1];
                Cden[C+1]=Dtemp;
                Cnum[C+1]=Ntemp;

                swap=true;

            }
            /*if(Cden[C]<Cden[C+1])
            {
                Dtemp=Cden[C];
                Cden[C]=Cden[C+1];
                Cden[C+1]=Dtemp;
                swap=true;

            }*/

        }
    }while(swap);
    for(int i=0;i<size;i++)
    {
        cout<<"Fraction"<<i<<"  "<<Cnum[i]<<"/"<<Cden[i]<<"="<<static_cast<double>(Cnum[i])/Cden[i]<<endl;
    }



}
void report(int Cnum[],int Cden[],int nums[],int den[],int size)
{

    int i;
    for(i=0;i<size;i++)
    {

        if(den[i] == 0)
        {
        cout << " Fraction " << i << "  " << nums[i] << "/" << den[i] << "  = " << "DNE" << endl;
        }
        else
        {
        cout << " Fraction " << i << "  " << nums[i] << "/" << den[i] << " = " << static_cast<double> ( nums[i])/den[i]<< endl;
        }


    }





}

We need to see a sample of the data. The code is trying to read half the file into one array then the other half into a different array, sequentially. If the data isn't set up that way then your code won't work right. In order to fix it we'll need to see exactly how your data is organized.

Also i guess you need to look into recursive fucntion as you could accomplish this task easy with that. i hate long codes. it makes me wanna sleep. long codes are more error prone.

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.