0
我正在處理一個項目,該項目需要患者編號,患者血壓記錄的數量,顯示這些記錄,然後根據該患者的1個或更多血壓記錄來獲取平均血壓讀數。所有的數據都是從文件中讀取的。這是我到目前爲止的代碼,在C++中循環讀取字符串
#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;
}
我嘗試使用for循環和while循環起來,但得到一些,我解決不了,真是奇怪的錯誤。所以我決定用一個嵌套的while循環來完成它。不幸的是,代碼只是凍結執行。數據文件存在,其中包含正確的數據,但出於某種原因,我無法讓程序正常工作。如果有人能幫助我,我會很感激。
- 編輯 這裏是其中的一個文件看起來像
4567 4 180 140 170 150
5643 2 125 150
如果'reader'永遠不等於'「\ n」',該怎麼辦? –
「while(!EOF)」條件如何滿足? – Duck
這將成爲錯誤的文件結尾我認爲 – emufossum13