/*
Create a program that uses a count controlled loop to read the patient's "readings" from a file and adds them up individually,
calculates the average for each person (ID), and displays the two totals. 

Input -
(numPatients-)
    20
    idNum  numBP   bpReading (1, 2, 3, 4)
    1234 -  4  -  150 156 120 130
    5678 -  3  -  200 198 187
    9101 -  4  -  157 152 166 160
    1121 -  4  -  140 140 142 137
    1131 -  1  -  158
    1141 -  4  -  210 220 206 211
    1151 -  3  -  205 206 207
    1161 -  2  -  199 192
    1171 -  2  -  120 117
    1181 -  4  -  204 201 209 210
    1192 -  4  -  200 200 201 190
    0212 -  3  -  182 188 200
    2232 -  4  -  194 196 190 151
    4252 -  4  -  161 180 174 179
    6272 -  2  -  190 134 
    8293 -  4  -  120 126 135 140
    0313 -  2  -  149 132
    2333 -  4  -  168 151 160, 154
    4353 -  3  -  110 109 120
    6373 -  4  -  150 150 162 159

Output -
   idNum    Total   Average
    1234 - bpTotal, bpAverage
    5678 - bpTotal, bpAverage
    9101 - bpTotal, bpAverage
    1121 - bpTotal, bpAverage
    1131 - bpTotal, bpAverage
    1141 - bpTotal, bpAverage
    1151 - bpTotal, bpAverage
    1161 - bpTotal, bpAverage
    1171 - bpTotal, bpAverage
    1181 - bpTotal, bpAverage
    1192 - bpTotal, bpAverage
    0212 - bpTotal, bpAverage
    2232 - bpTotal, bpAverage
    4252 - bpTotal, bpAverage
    6272 - bpTotal, bpAverage
    8293 - bpTotal, bpAverage
    0313 - bpTotal, bpAverage
    2333 - bpTotal, bpAverage
    4353 - bpTotal, bpAverage
    6373 - bpTotal, bpAverage


Constraints-
    Only read data until the end of the file.

Operations -
    1. Read file for numPatients
        counter = numPatients
    2. While counter < numPatients
        read idNum, numBP, bpReadings (1, 2, 3, 4) 
    3. bpTotal = bpReadings (1 + 2 + 3 + 4)
    4. bpAverage = bpTotal / numBP
    5. Each time the end of line is read, counter++
    6. Repeat until counter > 20
    7. Display
       idNum    Total   Average
    1234 - bpTotal, bpAverage
    5678 - bpTotal, bpAverage
    9101 - bpTotal, bpAverage
    1121 - bpTotal, bpAverage
    1131 - bpTotal, bpAverage
    1141 - bpTotal, bpAverage
    1151 - bpTotal, bpAverage
    1161 - bpTotal, bpAverage
    1171 - bpTotal, bpAverage
    1181 - bpTotal, bpAverage
    1192 - bpTotal, bpAverage
    0212 - bpTotal, bpAverage
    2232 - bpTotal, bpAverage
    4252 - bpTotal, bpAverage
    6272 - bpTotal, bpAverage
    8293 - bpTotal, bpAverage
    0313 - bpTotal, bpAverage
    2333 - bpTotal, bpAverage
    4353 - bpTotal, bpAverage
    6373 - bpTotal, bpAverage


*/


    #include <iostream>
    #include <string>
    #include <iomanip>
    #include <conio.h>
    #include <fstream>

    using namespace std;

    string idNum;
    int counter, counter2, numBP, bpReading1, bpReading2, bpReading3, bpReading4, numPatients;
    int bpTotal = 0;
    int bpAverage = 0;

    int main()
    {

        cout << "ID\t BP Total\t BP Average";
            cout << endl;

            ifstream ifile("bpDATA.txt");


        counter = 0;
        while (counter <= numPatients)
        {
            ifile >> idNum >> numBP;
            cout << idNum << "\t" ;

            counter2 = 0;
                {
                    if (counter2 != numBP)
                    {



                    ifile >> bpReading1;
                    counter2++;
                    }
                else if (counter2 != numBP)
                {
                    ifile >> bpReading2;
                    counter2++;
                    }
                else if (counter2 != numBP)
                {
                    ifile >> bpReading3;
                    counter2++;

                }
                else (counter = numBP);
                ifile >> bpReading4;

                }





        bpTotal = bpReading1 + bpReading2 + bpReading3 + bpReading4;
        bpAverage = bpTotal / numBP;


        cout << bpTotal << "\t" << bpAverage;


        counter++;


        }

        _getch();
        return 0;
    }

Recommended Answers

All 4 Replies

Member Avatar for iamthwee

Great, really great... Is there a question?

Yes, I need it to output what it says under output in the specifiation, but I can't process the code properly. The while code isn't working and I'm not sure why. Could you elaborate on how to go about this problem?

Member Avatar for iamthwee

The while code isn't working and I'm not sure why

What exactly isn't working, YOU need to be more specific.

Could you elaborate on how to go about this problem?

YOU need to elaborate on exactly what the problem is and how you might go about fixing it?
Best to start of one bit at a time rather than just try and do the whole thing at once.

I advise you to format your code so it's easier to read, because it's hard to help someone when the code is hard to read.

The task is simple if you think about it in steps.

  1. Open the file.
  2. Read the number of patients first, the first line of the file. (You didn't do this)
    [Start Loop Here]
  3. Read the id of the patient. (good)
  4. Read the number of BP measurements. (good)
  5. Loop x amount of BPs and read the 1st, 2nd, 3rd, and 4th measurements.
  6. Add up the BPs and get an average for each patient.
  7. Repeat steps 3-6.
  8. Output the results.
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.