C++ 在c+中循环字符串读取器+;

C++ 在c+中循环字符串读取器+;,c++,while-loop,do-while,C++,While Loop,Do While,我正在进行一个项目,该项目需要获取患者id,患者血压记录的数量,显示这些数据,然后根据该患者的一个或多个血压记录获取平均血压读数。所有数据都是从文件中读取的。这是我到目前为止得到的密码 #include <iostream> #include <string> #include <conio.h> #include <fstream> using namespace std; int main() { //Initialize Requ

我正在进行一个项目,该项目需要获取患者id,患者血压记录的数量,显示这些数据,然后根据该患者的一个或多个血压记录获取平均血压读数。所有数据都是从文件中读取的。这是我到目前为止得到的密码

#include <iostream>
#include <string>
#include <conio.h>
#include <fstream>
using namespace std;

int main()
{

    //Initialize Required Variables For Program
    int patientCount = 0;
    string id;
    string rHowMany; //String To Read From File
    int howMany = 0;
    int howManyCount = 0;
    int avg = 0;
    int avg2;
    string line;
    int numbre_lines = 0;
    ifstream reader ("data.txt"); //Open The Data File To Be Read From

    do
    {
        reader >> id;
        reader >> howMany;
        cout << "The Patient ID Is: " << id << endl;
        cout << "The Number Of Blood Pressure Record This Patient Has Is: " <<
                     howMany << endl;
        do
        {
            reader >> avg;
            avg = avg + avg;
        }
        while (reader != "\n");
        cin.clear();
        avg = avg / howMany;
        cout << "The Patient Average BP Reading Is: " << avg;
    }
    while (!EOF);
    return 0;
}
始终为true,因为
读取器
ifstream
,“\n”是字符串文字。
即使两者都转换为
void*
(我怀疑它显然是编译的),将
reader
转换为
void*
的结果也永远不会是文本的地址

您的逻辑也有一点缺陷-
avg
将是上一次读取的值的两倍(如果循环工作)


修复它只是一个练习。

如果
读卡器
永远不等于
“\n”
?当(!EOF)时
如何条件是否满足?在文件末尾它将变为false我想Sam,我不确定您的意思,“\n”是否等同于一行的结尾?您似乎假设所有血压值都在一行上。你不相信“有多少人”吗?
4567 4 180 140 170 150
5643 2 125 150
reader != "\n"