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;
}