Image

Imagetjsucks138 wrote in Imagecpp 😡frustrated

I am trying to sort a file by average with a bubble sort...I can't get it to work...Any help would be GREATLY appreciated...everything works until I get to the sorting and then I screw it up real nice :)


 



#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
 
struct Record
{
    char fname[21];
    char lname[21];
    int grades[3];
    double avg;
};
 
int main()
{
    Record rec;
    int x=0;
    int y=0;
    int ctr;
    float temp;
    int totalpeople;
    double stddev;
    double totalgrades;
    double sum;
    double avg;
    double totalavg;
    std::ifstream infile;
    std::cout.setf(std::ios::fixed|std::ios::showpoint);
    std::cout.precision(1);
    infile.open("text3.dat", std::ios::in);
    if(!infile)
    {
    std::cerr<<"\n\nInput file not opened.\n"<<std::endl;
    exit(-1);
    }
    infile >> rec.fname
           >> rec.lname;
    sum = 0.0;
    totalpeople=0;
    totalgrades=0;
    for(ctr = 0; ctr < 5; ctr  )
    {
        infile >> rec.grades[ctr];
        sum =rec.grades[ctr];
    }


    for(;!infile.eof();)
    {
        totalpeople  ;
 avg = sum/ctr;
 std::cout.unsetf(std::ios::right);
        std::cout.setf(std::ios::left);
        std::cout << std::setw(14) << rec.fname
                  << std::setw(14) << rec.lname;
        std::cout.unsetf(std::ios::left);
        std::cout.setf(std::ios::right);
 stddev=avg-totalavg;
        
 for(ctr = 0; ctr < 5; ctr  )
        std::cout << std::setw(5) << rec.grades[ctr];
        std::cout << std::setw(7) << avg;
        std::cout << std::setw(7) << stddev;
 if(avg>90)
 {
        std::cout << "\tA" <<std::endl;
 }
 else if (avg>80)
 {
        std::cout << "\tB" <<std::endl;
 }
 else if (avg>70)
 {
        std::cout << "\tC" <<std::endl;
 }
 else if (avg>60)
 {
        std::cout << "\tD" <<std::endl;
 }
 else if (avg<59)
 {
        std::cout << "\tF" <<std::endl;
 }
 
 totalgrades =avg;
     totalavg=totalgrades/totalpeople;
        infile >> rec.fname
               >> rec.lname;
        sum = 0.0;
 for(ctr = 0; ctr < 5; ctr  )
        {
            infile >> rec.grades[ctr];
            sum =rec.grades[ctr];
        }
    }
 
    infile.close();
    return(0);
}