So I'm trying to enter all the information for this program and it's giving me a hard time. The ones with (need help) are the tough steps. The text file and functions are found in the attachments.

U-WATCH-UR-WEIGHT

"At the beginning and end of each month, members of the club are weighed in. Each member's first name, last name, initial weight and final weight are recorded on a line in the file weight.txt. This file contains the weights at the beginning and end of January. The file will not contain more than 30 records. Read into 4 arrays, each member's first name, last name, initial and final weights. A fifth array must be created to store the difference between the final and the initial weight for the month. If a member loses weight, this difference will be a positive number. If a member gains weight, this difference will be a positive number. If a member loses weight, he/she is awarded a month's membership fee. Create a sixth array that will store the word 'FREE' or 'DUE' depending on whether or not the member lost weight."

I already have step one: Open the file you need to read from.

Step two: Read in all the records and fill in the vectors (4 vectors will be read, 2 vectors will be calculated/created). Keep track of the # of records you have read in so you can resize your vectors after reading them all in. (desperate help needed)

Step 3: Print out a welcome message, with a brief explanation of the program(no problems here).

Step 4: Start a do while loop (loop executed unless user selects (5) Exit)
4a. Print out the menu
(1.)Sort by member's name
(2.)Sort by weight loss
(3.)Search for a member's last name (need help)
(4.)Calculate the average weight loss (need help)
(5.)Exit the program
4b. Ask the user to enter an option (no help needed)
4c. Use a switch statement to process option (no help needed)

  • Case 1: Use bubble sort to sort vectors alphabetically by last or first name (be sure to swap all 6 vectors). Call the Print Function (no help needed)
  • Case 2: Use a bubble sort to sort vectors numerically by weight loss (be sure to swap all six vectors) Call the Print Function (no help needed)
  • Case 3: As the user for a member (last name) to search for.
    Call a function (see last page) passing in the vector of names and the member to search for. The function will return an index value. )major help needed)
    - If the index value is -1, print out a message that the consumer was not found in the list.
    - If the index was not -1, it contains the location in the vector of the consumer, print out a letter to the member reminding him that his next month's fee is "FREE or "DUE". Include the member's name (first and last) and weight loss. (need help)
    Case 4:
  • Initialize the total weight loss to zero.
  • Loop through the vectors keeping track of the total weight loss for all members.
  • Calculate the average, rounded to the nearest tenth.
  • Print out a message with the average weight loss/gain.

Here's the code I have so far:

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>

using namespace std;

int SearchN(vector <string> &lastname, string searchName)
{
    bool found = false;
    int place = 0;

    while (found!=true&&place < lastname.size())
    {
       if (searchName == lastname[place])
          found = true;
       else
           place++;
    }

    if (found==true)
       return place;
    else
        return -1;
}

void printIt(vector <string> &firstname, vector <string> &lastname,
             vector <int> &oldweight, vector <int> &newweight,
             vector <int> &difference, vector <string> &membfee)

{
   cout<<setw(15)<<"firstname"<<setw(10)<<"lastname"<<setw(11)<<"oldweight"
   <<setw(11)<<"newweight"<<setw(11)<<"difference"<<setw(8)<<"membfee"<<endl;


   for (int i = 0; i < lastname.size(); i++)
   {
       cout<<setw(15)<<firstname[i]<<setw(10)<<lastname[i]<<setw(11)<<
       oldweight[i]<<setw(11)<<newweight[i]<<setw(11)<<difference[i]<<
       setw(8)<<membfee[i]<<endl;
   }
}
int main(int argc, char *argv[])
{
    int count=0;
    bool found=false;
    int place=0;
    char option;
    string searchName(30);           // searchName declared as string
    vector <string> membfee(30);
    vector <string> firstname(30);    // person's first name
    vector <string> lastname(30);     // person's last name
    vector <int> difference(30);      // difference of weight lost/gained
    vector <int> newweight(30);     // final weight after losing/gaining weight
    vector <int> oldweight(30);      // starting weight
    vector <int> totalweightloss(30);
    ifstream inweight;
    int tempnewweight;
    int tempoldweight;
    int tempdifference;
    string tempfirstname;
    string templastname;
    string tempmembfee;

    inweight.open("weight.txt");
    if (inweight.fail())
    {
       cout<<"Error opening the file!";
       system ("PAUSE");
       exit(1);
       cout<<endl;
    }
    else
    {

    cout<<"Welcome to the U-Watch-Ur-Weight Program! This program calculates"
    "how much weight a person gained or lost each month."<<endl;

    while(inweight>>firstname[count])
    {
        inweight>>lastname[count]>>oldweight[count]>>newweight[count];
        difference[count]=newweight[count]-oldweight[count];

        if (difference[count]<=0)
          {
             membfee[count]="FREE";
          }
          else
             membfee[count]="DUE";
             count++;

    }

    firstname.resize(count);
    lastname.resize(count);
    oldweight.resize(count);
    newweight.resize(count);
    difference.resize(count);
    membfee.resize(count);

do
{
    cout<<"You can: "<<endl;
    cout<<"\t1)Sort by member's last name"<<endl;
    cout<<"\t2)Sort by weight loss"<<endl;
    cout<<"\t3)Search for a member's last name"<<endl;
    cout<<"\t4)Calculate the average weight loss"<<endl;
    cout<<"\t5)Exit the program"<<endl;
    cout<<endl;
    cout<<"Choose an option: ";
    cin>>option;

    switch(option)
    {
    case 1:
         option = 1;

         for (int k = 1; k < lastname.size(); k++)
         {
             for (int j = 0; j < lastname.size()-k; j++)
             {
                 if (lastname[j] > lastname[j+1])
                 {
                    tempfirstname=firstname[j];
                    firstname[j]=firstname[j+1];
                    firstname[j+1]=tempfirstname;
                    templastname=lastname[j];
                    lastname[j]=lastname[j+1];
                    lastname[j+1]=templastname;
                    tempoldweight=oldweight[j];
                    oldweight[j]=oldweight[j+1];
                    oldweight[j+1]=tempoldweight;
                    tempnewweight=newweight[j];
                    newweight[j]=newweight[j+1];
                    newweight[j+1]=tempnewweight;
                    tempdifference=difference[j];
                    difference[j]=difference[j+1];
                    difference[j+1]=tempdifference;
                    tempmembfee=membfee[j];
                    membfee[j]=membfee[j+1];
                    membfee[j+1]=tempmembfee;
                 }
             }
         }
         printIt(firstname, lastname, oldweight, newweight, difference, membfee);
         break;

    case 2:
         option = 2;

         for (int k = 1; k < lastname.size(); k++)
         {
             for (int j = 0; j < lastname.size()-k; j++)
             {
                 if (difference[j] > difference[j+1])
                 {
                    tempfirstname=firstname[j];
                    firstname[j]=firstname[j+1];
                    firstname[j+1]=tempfirstname;
                    templastname=lastname[j];
                    lastname[j]=lastname[j+1];
                    lastname[j+1]=templastname;
                    tempoldweight=oldweight[j];
                    oldweight[j]=oldweight[j+1];
                    oldweight[j+1]=tempoldweight;
                    tempnewweight=newweight[j];
                    newweight[j]=newweight[j+1];
                    newweight[j+1]=tempnewweight;
                    tempdifference=difference[j];
                    difference[j]=difference[j+1];
                    difference[j+1]=tempdifference;
                    tempmembfee=membfee[j];
                    membfee[j]=membfee[j+1];
                    membfee[j+1]=tempmembfee;
                 }
             }
         }
         printIt(firstname, lastname, oldweight, newweight, difference,
         membfee);
         break;

    case 3:
         option = 3;
         int index;
         cout<<"Enter the person's last name: ";
         cin>>searchName;

         int searchN(lastname, searchName);



while (found!=true&&place < lastname.size())
    {
       if (searchName == lastname[place])
          found = true;
       else
           place++;
    }

         if (found == true&&difference>0)
         {
         cout<<"Dear "<<firstname<<" "<<lastname<<","<<endl;
         cout<<endl;
         cout<<"Last month your weight gain was "<<difference<<" pounds. We would"<<endl;
         cout<<"like to remind you that your next month's fee is "<<endl;
         cout<<membfee<<". Don't give up - we are here to help!"<<endl;
         cout<<endl;
         cout<<"Sincerely,"<<endl;
         cout<<"Ima Hottie"<<endl;
         cout<<"Director Julia"<<endl;
         }

         else if (found == true&&difference<=0)
         {
         cout<<"Dear "<<firstname<<" "<<lastname<<","<<endl.
         cout<<endl;
         cout<<"Last month your weight loss was "<<difference<<" pounds. We would"<<endl;
         cout<<"like to remind you that your next month's fee is"<<endl;
         cout<<membfee<<". Congratulations on your weight loss!"<<endl;
         cout<<endl;
         cout<<"Sincerely,"<<endl;
         cout<<"Ima Hottie"<<endl;
         cout<<"Director"<<endl;
         }

       else
         cout<<"Sorry, the name "<<searchName<<" was not found in our file."<<endl;
         break;


    case 4:
         cout<<setprecision(1);
         cout<<fixed;
         option = 4;
         totalweightloss=0;

 for (int i=1; i < difference; i++)  //careful declaring in loop
    {
    totalweightloss = totalweightloss + count;
    }
    totalweightloss = difference + count;

    cout<<"The average weight loss/gain is "<<totalweightloss<<endl;



         break;
    case 5:
         cout<<"Thank you for running the U-Watch-Ur-Weight Program! Please";
         "come again"<<endl;
         break;
    default 6:
         cout<<"Invalid option. Please re-enter a valid option."<<endl;
         break;
    }

} while (option != 5);

    cout<<setw(15)<<firstname<<setw(10)<<lastname<<setw(11)<<
       oldweight<<setw(11)<<newweight<<setw(11)<<difference<<
       setw(8)<<membfee<<endl;

    inweight.close();
    system("PAUSE");
    return EXIT_SUCCESS;
}

Thanks you so much for helping me with this!

Attachments
int SearchN(vector <string> &lastname, string searchName)
{
    bool found = false;
    int place = 0;

    while (found!=true&&place < lastname.size())
    {
       if (searchName == lastname[place])
          found = true;
       else
           place++;
    }

    if (found==true)
       return place;
    else
        return -1;
}

void printIt(vector <string> &firstname, vector <string> &lastname,
             vector <int> &oldweight, vector <int> &newweight,
             vector <int> &difference, vector <string> &membfee)
Stella Stover 200 180
Minni Mouse 130 120
John Smith 160 154
Lotta Pounds 115 127
Richard Dawson 123 129
Paddy Whack 125 115
Not Withit 127 127
Gayle Force 205 191
Trully Toned 246 247
Kyle Morris 275 253
Fish Face 173 168
Bessy Bossy 158 162
Hank Holdup 158 154
Lady Godiva 138 133
Hawk Eye 199 172
Sally Slim 100 112
Sun Shine 215 213
Bobby Bear 179 170
George Whitmore 213 220

If you are having trouble with step 2, you should not have any code for step 3 and beyond.

Edit out all the stuff you cannot test without step 2 being finished.
Then perfect step 2.
Add step 3 and make sure it works.
Then start adding step 4, a piece at a time.

The hardest way to write any program is to write the whole thing then start testing. You get lost fast.

Functions can go before main() as you have it, or they can go after main() but in that case you'll need a prototype before main to tell the compiler what to expect later on.

In your example, for your method SearchN int SearchN(vector <string> &lastname, string searchName); or simply int SearchN(vector<string> &, string); would have to be placed before main().

This article has been dead for over six months. Start a new discussion instead.