I have a homework assignment that im not really sure how to approach as of right now, I've done some research but. I have not found really anything that would pertain to what I'm doing. Any help would be appreciated <3 . So far this is what I have:

/******************************************************************************
1.WRITE A PROGRAM THAT TAKES AT LEAST SIX PERSONS FROM A FILE, AND LISTS THEM IN
ORDER: (i) SSN ASCENDING, AND (ii) BMI DECREASING 

2. INPUT AT LEAST SIX PERSONS FROM A FILE. Prompt the user to enter one SSN of 
the given six. For that SSN, find the lowest and highest RBMI among the other 
five. Report the person names and RBMI values. 

3. Among all the persons, find the two persons having the least RBMI. Report 
their names and the value of their RBMI. 
*******************************************************************************/

#include <iostream>
#include <math.h>
#include <fstream>

using namespace std;

struct person
{               //define a struct

  string pName;
  float BMI;            // 18.4 
  long SSN;         // 10 digit 

};


int main ()
{

  string fLine;
  int pcount = 0;

  ifstream fRap;
  fRap.open ("Rappers.txt");

  while (!fRap.eof ())
    {

      getline (fRap, fLine);
      //myFavMovieStar[pcount].pname = fLine;
      getline (fRap, fLine);
      //myFavMovieStar[pcount].pname = fLine;
      getline (fRap, fLine);
      //myFavMovieStar[pcount].pname = fLine;

      pcount++;
    }

  fRap.close ();

  cout << "pcount is: " << pcount << endl;

  person myFavRapper[pcount];

  fRap.open ("Rappers.txt");

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

      getline (fRap, fLine);
      myFavRapper[i].pName = fLine;
      getline (fRap, fLine);
      myFavRapper[i].SSN = stol (fLine);
      getline (fRap, fLine);
      myFavRapper[i].BMI = stof (fLine);

    }

  fRap.close ();

  cout << "Done reading this file." << endl;

  for (int i = 0; i < pcount; i++)
    {
      cout << myFavRapper[i].pName << " | " << myFavRapper[i].
    SSN << " | " << myFavRapper[i].BMI << endl;
    }

  cout << "" << endl;

  cout << "SSN's Ascending: " << endl;





  cout << "" << endl;

  cout << "BMI's Decreasing: " << endl;



}

Here is the contents of the text file:

Jahseh Onfroy
123456789
19.4
Aristos Petrou
345678912
25.1
Gustav Ahr
234567891
21.5
Scott Arceneaux
101110111
22.4
Jazz Butler
134243332
22.2
Symere Woods
678912345
25.7

Recommended Answers

All 3 Replies

While it's a partial answer, it shows the concepts using your text file.

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <vector>
#include <algorithm>

using namespace std;

std::string f_ssn;
std::string array_namef[10] = { "" };
std::string array_namel[10] = { "" };
std::string array_ssn[10] = { "" };
std::string array_bmi[10] = { "" };

struct Rappers {
    string ssn; 
    string bmi;
    string name;
    int count;
};

int rappers()
{
    Rappers r1;
    string fLine;

    int pcount = 0;
    ifstream fRap;
    fRap.open("Rappers.txt");
    while (!fRap.eof())
    {
        getline(fRap, fLine);   
        r1.name = fLine; 
        array_namef[pcount] = fLine;
        getline(fRap, fLine);
        r1.ssn = fLine;
        array_ssn[pcount] = fLine;
        getline(fRap, fLine);
        r1.bmi = fLine;
        array_bmi[pcount] = fLine;
        std::cout << r1.name << "\t" <<  r1.ssn << "\t" << r1.bmi << std::endl;

    pcount++;
    }     

    std::cout << "" << std::endl;
    return 0;
}

struct Rapper {
    string bmi;
    string ssn;
    string name;
};

void Print(vector<Rapper>& v) {

    for (int i = 0; i < v.size(); ++i) {
        std::cout << v[i].bmi << ' ' << v[i].ssn << ' ' << v[i].name << std::endl;
    }
        std::cout << std::endl;
}


    int rap_sort() {
        rappers();
        Rappers r1;
        string fLine;
        int pcount = 0;

        vector<Rapper> v;
        ifstream fRap;
        fRap.open("Rappers.txt");
        while (!fRap.eof())
        {
            getline(fRap, fLine);
            r1.name = fLine;
            array_namef[pcount] = fLine;
            getline(fRap, fLine);
            r1.ssn = fLine;
            array_ssn[pcount] = fLine;
            getline(fRap, fLine);
            r1.bmi = fLine;
            array_bmi[pcount] = fLine;

        //  cout << r1.name << "\t" << r1.ssn << "\t" << r1.bmi << endl;
            pcount++;
            v.push_back({ r1.bmi, r1.ssn, r1.name });
        }

        std::cout << "Before sort...\n" << std::endl;
        Print(v);

        sort(v.begin(), v.end(), [](Rapper a, Rapper b) {
            return a.bmi > b.bmi;
            });

        std::cout << "Decending...\n" << std::endl;
        Print(v);

        sort(v.begin(), v.end(), [](Rapper a, Rapper b) {
            return a.bmi < b.bmi;
            });
        std::cout << "Accending...\n" << std::endl;
        Print(v);

        std::cout << "Enter SSN: ";
        std::cin >> f_ssn;

        return 0;
    }

int main()
{
    rap_sort();
    return 0;
}

Assuming the file is utterly consistent (3 line sets, ssn w/o dashes in the second line), you can toss a line, read a line as a string or int, toss a line in a 3 line loop. Now, you can drop you SSN into an (ordered) set, which eliminates duplicates and sorts, either as the string or an int (also fits in a long, unsigned int, unsigned long -- 9 digits is 3 commas is 30 bits). Then dump the set to your output.

If there are file variations or intermittent dashes, they you need to sense the good lines and chase out the dashes.

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.