在 c++ 中循环字符串读取器
Looping a string reader in c++
我正在做一个项目,该项目获取患者 ID、患者血压记录的数量,显示这些记录,然后根据该患者的 1 条或多条血压记录获取平均血压读数。所有数据都是从文件中读取的。这是我到目前为止得到的代码,
#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"
始终为 true,因为 reader
是ifstream
,而 "" 是字符串文本。
即使两者都转换为void*
(我怀疑,因为它显然是编译的),将reader
转换为void*
的结果也永远不会是文字的地址。
你的逻辑也有点缺陷——如果循环工作,avg
将是读取的最后一个值的两倍。
修复它留作练习。
相关文章:
- 如何使用Luacneneneba API正确读取字符串和表参数
- 如何从二进制文件中读取字符串
- 如何在 c++ 中使用 getline 从文件中读取字符串?
- 读取字符串文本输入以创建 2D 矢量
- 罗马数字到阿拉伯语 (vinculum) - 读取字符串中的字符
- C++:从文件中读取字符串和整数,并获得最大数字
- C++,从文件中读取字符串,分段错误
- 如果语句错误地读取字符串,则始终在第一个条件返回
- 如何从内存中读取字符串?
- 如何创建一个C++程序来读取字符串数组中的信息,然后将其排序到类中?
- 我需要C++读取字符串数组中每个单词的第一个字母
- 读取字符串C++的字符时出错
- 读取字符串直到标志 (C++)
- 读取字符串字符时出错 - 对于数组中的字符串值
- C++输入文件流无法从文件中读取字符串
- 读取字符串字符时出错
- 如何读取<字符串,矢量<对<int,字符串的映射>>>
- 如何使此函数使用 getline 读取字符串并使用 int 执行相同的行为?
- C++ 使用 Strtok 读取字符串字符时出错
- 使用 getline 读取字符串,但每行中都有换行符