简体   繁体   中英

How do I pull out the data file to find Average? Logic Help C++

What is happening is I have a .txt file in my project like this.

4567 4 180 140 170 150
4693 1 119
4690 5 200 120 135 136
4693 2 149 133
4783 3 133 123 140
4824 3 130 155 120
4833 2 119 186

1st column is Patient ID
2nd column is How Many Tests the patient had
3rd column and beyond is all the blood pressure readings.

How do I calculate the average for the data? The issue I came across was how do I pull those blood pressure readings out of the text file and add them all up to divide them by my variable howMany. Code is working fine I just need the average.Thanks

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main(){
//Initialize Required Variables For Program
int patientCount = 0;
string id;
int avg = 0;
int howMany = 0;
ifstream reader ("data.txt"); //Open The Data File To Be Read From

while( reader >> id && reader >> howMany ){ //as long as you can read another pacient data
    int sum = 0; //sum accumulates the pressure readings per pacient
    cout << "The Patient ID Is: " << id << endl;
    cout << "The Number Of Blood Pressure Record This Patient Has Is: " << howMany << endl;
    for(int K = 0; K < howMany; ++K){
        int number;
        reader >> number;
        sum += number;

    }
    //going to complete average here but I don't know how to pull out the data

}
system("pause");
return 0;
}

Just do the division:

while( reader >> id && reader >> howMany ){ //as long as you can read another pacient data
    int sum = 0; //sum accumulates the pressure readings per pacient
    double avg;

    cout << "The Patient ID Is: " << id << endl;
    cout << "The Number Of Blood Pressure Record This Patient Has Is: " << howMany << endl;
    for(int K = 0; K < howMany; ++K){
        int number;
        reader >> number;
        sum += number;
    }
    //going to complete average here but I don't know how to pull out the data
    avg = (double) sum / howMany; 
    cout << "The Average Blood Pressure For This Patient Is: " << avg << endl;
}

Your input loop is a bit strange. If a patient has an ID of 0 or has 0 blood pressure readings, then it stops reading input and exits normally.

Meanwhile, your program likely exits by throwing an exception (or infinitely looping) in the typical case. You might want to wrap your input loop with a try-catch block.

You're starting with the data arranged in lines, and the line breaks are significant.

Since it starts out as line-oriented data, I'd start by reading a line of data at a time. Then break each line up into the constituent pieces. One obvious way to do that would be to put the data from the line into a stringstream.

So, we could do something like this:

std::string line;

while (std::getline(infile, line)) {
    int id, num, total=0, reading;

    std::istringstream buffer(line);
    buffer >> id >> num;
    for (int i=0; i<num && buffer >> reading; i++)
        total += reading;
    int average = total / num;

    std::cout << "ID: " << id << ", average: " <<average << "\n";
}

This still has one minor problem:it doesn't really validate the input data. For example, the third row of input data you show in the question specifies that it contains 5 readings, but it actually only contains 4. The preceding code won't diagnose that, but doing so is probably a good idea (and your code will get out of sync--since it's missing a reading, it'll read the ID from the next line as a reading, and produce a bad result, then treat the number of readings on the next line as an ID, the reading after that as a number of readings, and...about now I hope you realize why I advise that line-oriented data be read as lines).

If you're sure your data will always be well formatted, you probably don't care--but the data you show in your question isn't, and you may want to diagnose that. One way to do so would be to read the data from the buffer into a vector<int> , and print out an error message if the number of items read differs from the number of items the line was supposed to contain.

while (std::getline(infile, line)) {
    int id, num, total=0, reading;

    std::istringstream buffer(line);
    buffer >> id >> num;
    std::vector<int> readings { std::istream_iterator<int>(buffer), 
                                std::istream_iterator<int>() };

    if (num != readings.size())
        throw std::runtime_error("Bad input data");

    std::cout << "ID: " << id << ", average: " << 
         std::accumulate(readings.begin(), readings.end(), 0.0) / num;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM