Linked lists...need help with a little something

This is a discussion on Linked lists...need help with a little something within the C++ Programming forums, part of the General Programming Boards category; I have written a code that takes data from a text file and inputs it into a linked lists then ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    122

    Linked lists...need help with a little something

    I have written a code that takes data from a text file and inputs it into a linked lists then allows the user to manipulate the data. What I have in the file is a name, id, weight, and height. The program currently calculates the BMI for each member and prints this out too. The only thing I allow the user to manipulate is the weight for a certain member. What I need is to have the output display the CURRENT weight and the INITIAL weight. I get it to display the current weight after the changes but not the initial. Thanks in advance.

    Here is my code:

    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <cmath>
    #include <string>
    
    using namespace std;
    
    struct memberType
    {
           string name;
           int id;
           double height;
           double weight;
           double bmi;
    };
    
    typedef memberType infoType;
    
    struct nodeType
    {
         infoType info;
         nodeType  *link;
    };
    
    double calcBmi (double weight, double height);
    void printOne (infoType x);memberType getOne(ifstream& inFile);
    
    void print (nodeType *list);
    void printHealthy (nodeType *list);
    void printOut (nodeType *list, ofstream& outFile);
    nodeType * addToList (infoType a, nodeType *list);
    void printOneOut (infoType one, ofstream& outFile);
    int search (int id, nodeType *list);
    nodeType * change (nodeType *list, double i, double& w);
    
    int main ()
    {
        nodeType *healthy = NULL;
        int id;
        char n;
        infoType a;
        string name, inputFile;
        double height, weight, bmi;
        
    
        ifstream inFile;
        ofstream outFile;
         
         cout << "Enter the name of the input file: ";
         cin >> inputFile;
         cout << endl << endl;
          
         inFile.open(inputFile.c_str());
         outFile.open("members.txt");
         
         if(!inFile) 
         {
             cout << "Invalid file name" << endl;
             system("pause");
             return 1;
             }
    
        a = getOne (inFile);
    
        while (inFile)
        {
            healthy = addToList (a, healthy);
            a = getOne (inFile);
        }
       
        cout << "__________________________________________________________________"
             << "______\n";
        cout << "____________________________|ENTIRE LIST|_________________________"
             << "______" << endl;
        cout << left << setw(17) << "Name" 
             << left << setw(14) << "ID" 
             << left << setw(20) << "Height" 
             << left << setw(18) << "Weight"
             << right << "BMI" << endl;
        cout << "__________________________________________________________________"
             << "______\n";
        print (healthy);
    
        cout << "__________________________________________________________________"
             << "______\n";
        cout << "___________________________|HEALTHY PEOPLE|_______________________"
             << "______" << endl;
        cout << left << setw(17) << "Name" 
             << left << setw(14) << "ID" 
             << left << setw(20) << "Height" 
             << left << setw(18) << "Weight"
             << right << "BMI" << endl;
        cout << "__________________________________________________________________"
             << "______\n";
        printHealthy (healthy);
    
        cout << "Would you like to change a member's weight? (Y = Yes, N = No): ";
        cin >> n;
        cout << endl;
    
        while (n != 'n' || n != 'N')
        {
            if (n == 'y' || n == 'Y')
            {
            cout << "__________________________________________________________________"
                 << "______\n";
            cout << "\nEnter the member's ID: " ;
            cin >> id;
            cout << endl;
    
            int y = search(id, healthy);
    
            if (y == -1)
                cout << "Person with id " << id << " not found.\n" << endl;
            else
            {
                cout << "Enter the new weight: " ;
                cin >> weight;
                cout << endl;
    
                change(healthy, y, weight);
                cout << "================================\n";
                cout << "= Weight change has been made. =\n";
                cout << "================================\n\n";
                cout << "________________________________________________________"
                     << "________________\n\n";
            }
            }
            else if (n == 'n' || n == 'N')
            {
                  cout << "________________________________________________________"
                       << "________________\n";
                  cout << "______________________________|NEW LIST|________________"
                       << "________________" << endl;
                  cout << left << setw(17) << "Name" 
                       << left << setw(14) << "ID" 
                       << left << setw(20) << "Height" 
                       << left << setw(18) << "Weight"
                       << right << "BMI" << endl;
                  cout << "________________________________________________________"
                       << "________________\n";
                  print (healthy);
    
                  cout << "________________________________________________________"
                       << "________________\n";
                  cout << "____________________________|HEALTHY LIST|______________"
                       << "________________" << endl;
                  cout << left << setw(17) << "Name" 
                       << left << setw(14) << "ID" 
                       << left << setw(20) << "Height" 
                       << left << setw(18) << "Weight"
                       << right << "BMI" << endl;
                  cout << "________________________________________________________"
                       << "________________\n";
                  printHealthy (healthy);
    
                  outFile << "This file contains an output of the members.  Members" 
                          << " with a healthy BMI are listed first" << endl;
                  outFile << " and those with a BMI below 18.5 are second followed "
                          << "by those with a BMI of 25 or higher.\n\n" << endl;
                  outFile << "_____________________________________________________"
                          << "___________________\n";
                  outFile << "____________________________|HEALTHY LIST|___________"
                          << "___________________" << endl;
                  outFile << left << setw(17) << "Name" 
                          << left << setw(14) << "ID" 
                          << left << setw(20) << "Height" 
                          << left << setw(18) << "Weight"
                          << right << "BMI" << endl;
                  outFile << "_____________________________________________________"
                          << "___________________\n";
                  printOut (healthy, outFile);
    
                  cout << "The final list was saved in \"Vrba.txt\" \n\n" ;
    
                  break;
                  }
            else
                  cout << "Your selection of " << n << " is incorrect.\n\n";
    
            cout << "\nWould you like to change a member's weight?"
                 << " (Y = Yes, N = No): ";
            cin >> n;
            cout << "\n\n";
           }
    
        system ("pause");
        return 0;
    }
    
    double calcBmi (double weight, double height)
    {
       double bodyMassIndex;
    
       bodyMassIndex = 703 * weight / (pow (height,2));
       return bodyMassIndex;
    }
    
    memberType getOne (ifstream& inFile)
    {
       memberType one;
    
       inFile >> one.name >> one.id >> one.height >> one.weight;
    
       one.bmi = calcBmi (one.weight, one.height);
    
       return one;
    }
    
    nodeType * addToList (infoType x, nodeType *list)
    {
       nodeType *temp;
    
       temp = new nodeType;
       temp->info = x;
       temp->link=NULL;
    
       if (list == NULL)
       {
           list = temp;
           return list;
       }
    
       nodeType *current = list->link;
       nodeType *first = list;
       nodeType *prev = list;
    
       while (prev != NULL)
       {
            if (temp->info.id < first->info.id)
            {
                temp->link = first;
                return temp;
            }
            else if ((temp->info.id > first->info.id) && (first->link == NULL))
            {
                first->link = temp;
                return first;
            }
            else if (temp->info.id < current->info.id)
            {
                temp->link = current;
                prev->link = temp;
                list = first;
                return list;
            }
    
            else if (current->link == NULL)
            {
                current->link = temp;
                list = first;
                return list;
            }
           current = current->link;
           prev = prev->link;
       }
    
       list = temp;
    
        return list;
    }
    
    void print (nodeType *list)
    {
         nodeType *current;
         current = list;
    
         while (current != NULL)
         {
           printOne ( current->info) ;
           cout << endl;
           current = current->link;
         }
         cout << endl;
    
    }
    
    void printHealthy (nodeType *list)
    {
         nodeType *current;
         current = list;
    
         while (current != NULL)
         {
           if ((current->info.bmi >= 18.5) && (current->info.bmi <= 25))
           {
                printOne ( current->info) ;
                cout << endl;
           }
            current = current->link;
         }
         cout << endl;
    
    }
    
    void printOne (memberType one)
    {
         one.bmi = calcBmi (one.weight, one.height);
         cout << fixed << showpoint << setprecision (1);
         cout << left << setw(8) << one.name << "\t" 
              << left << setw(8) << one.id << "\t" 
              << left << setw(8) << one.height << "\t" 
              << right << setw(8) << one.weight << "\t"
              << right << setw(8) << one.bmi;
    }
    int search (int id, nodeType *list)
    {
        nodeType *current = list;
    
        for (int i=0; current != NULL; i++)
        {
            if (current->info.id == id)
                return i;
            current = current->link;
        }
    
        return -1;
    }
    
    nodeType * change (nodeType *list, double i, double& weight)
    {
       nodeType *current = list;
    
       for (int a=0; a < i; a++)
          current = current->link;
    
        current->info.weight = weight;
    }
    
    void printOut (nodeType *list, ofstream& outFile)
    {
         nodeType *current = list;
    
         while (current != NULL)
         {
           if ((current->info.bmi >= 18.5) && (current->info.bmi <= 25))
           {
                printOneOut ( current->info, outFile) ;
                outFile << endl;
           }
            current = current->link;
         }
    
         current = list;
    
         while (current != NULL)
         {
           if (current->info.bmi < 18.5)
           {
                printOneOut ( current->info, outFile) ;
                outFile << endl;
           }
            current = current->link;
         }
    
         current = list;
    
         while (current != NULL)
         {
           if (current->info.bmi > 25)
           {
                printOneOut ( current->info, outFile) ;
                outFile << endl;
           }
            current = current->link;
         }
    
         outFile << endl;
    }
    
    void printOneOut (infoType one, ofstream& outFile)
    {
         one.bmi = calcBmi (one.weight, one.height);
         outFile << fixed << showpoint << setprecision(2);
         outFile << left << setw(8) << one.name << "\t" 
                 << left << setw(8) << one.id << "\t" 
                 << left << setw(8) << one.height << "\t" 
                 << right << setw(8) << one.weight << "\t"
                 << right << setw(8) << one.bmi;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    What you could do is to return the old weight from function change():
    Code:
                old_weight = change(healthy, y, weight);
    But the problem is you could only print the last old weight, which might not be the initial weight.

    It sounds like you need to add a member to your class to store the initial weight, and initially this would be the same as weight. Then whenever weight is updated, it won't be overwriting the initial weight.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    Thanks a lot. It was simpler than I thought Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Singly Linked Lists: Clarification Needed
    By jedispy in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2006, 05:30 PM
  2. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 05:32 PM
  3. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 09:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 07:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21