我在从 .txt 文件中添加数字时遇到了一些问题
I have got some problems with adding up numbers from a .txt file
我在学校布置了作业,我已经完成了,但结果并不是我想要的。
任务是根据以下规则从文本文件中获取总和:
第一列包含数字
第二个是0/1布尔值(分隔符是space)
只要 bool 在连续行中为真,程序就应该将数字相加
.txt 文件如下所示:
输入:
20 1
30 1
40 0
50 1
60 1
期望输出:
50
110
代码:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string line;
double number;
double sum;
int boolean;
sum = 0;
ifstream homework;
homework.open("homework.txt");
while (!homework.eof())
{
homework >> number >> boolean;
if (boolean == 1)
{
sum += number;
}
}
cout << sum << endl;
homework.close();
system("pause");
return 0;
}
我希望代码打印出 50 和 110(因为有一行带有 false 布尔值)但是代码打印出了 160(因此它总结了所有带有 true 布尔值的行)。
有什么想法吗?
问题是直到文件被完全迭代才输出,所以你看到的是总和,而不是每个 false
布尔值后的总和。如果每次遇到布尔值 false
时都需要小计,则需要在循环中添加一些内容:
while (homework >> number >> boolean)
{
if (boolean)
{
sum += number;
}
else
{
cout << sum << endl;
sum = 0;
}
}
顺便说一句。您的布尔变量 boolean
是如何声明的,您的代码甚至可以编译吗?我已经更改了样本中的支票(删除了 == 1
)。
您需要将行 cout << sum << endl;
放在 while 循环内的 else 中,并重置总和。
应该这样做:
while (!homework.eof())
{
homework >> number >> boolean;
if (boolean == 1)
{
sum += number;
}
else
{
cout << sum << endl;
sum = 0;
}
}
你有很多选择,一个可能是:
第一个:将布尔变量准确定义为(布尔变量)并将其与真假进行比较
示例:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string line;
bool boolean;
double number{ 0 };
double sum{ 0 };
ifstream homework;
homework.open("hw.txt");
while (!homework.eof())
{
homework >> number >> boolean;
if (boolean)
{
sum += number;
}
}
cout << sum << endl;
homework.close();
return 0;
}
要在发现 false 时停止 while,您可以使用 break
if (boolean)
{
sum += number;
}
else {
break;
}
我在学校布置了作业,我已经完成了,但结果并不是我想要的。
任务是根据以下规则从文本文件中获取总和:
第一列包含数字
第二个是0/1布尔值(分隔符是space)
只要 bool 在连续行中为真,程序就应该将数字相加
.txt 文件如下所示:
输入:
20 1
30 1
40 0
50 1
60 1
期望输出:
50
110
代码:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string line;
double number;
double sum;
int boolean;
sum = 0;
ifstream homework;
homework.open("homework.txt");
while (!homework.eof())
{
homework >> number >> boolean;
if (boolean == 1)
{
sum += number;
}
}
cout << sum << endl;
homework.close();
system("pause");
return 0;
}
我希望代码打印出 50 和 110(因为有一行带有 false 布尔值)但是代码打印出了 160(因此它总结了所有带有 true 布尔值的行)。
有什么想法吗?
问题是直到文件被完全迭代才输出,所以你看到的是总和,而不是每个 false
布尔值后的总和。如果每次遇到布尔值 false
时都需要小计,则需要在循环中添加一些内容:
while (homework >> number >> boolean)
{
if (boolean)
{
sum += number;
}
else
{
cout << sum << endl;
sum = 0;
}
}
顺便说一句。您的布尔变量 boolean
是如何声明的,您的代码甚至可以编译吗?我已经更改了样本中的支票(删除了 == 1
)。
您需要将行 cout << sum << endl;
放在 while 循环内的 else 中,并重置总和。
应该这样做:
while (!homework.eof())
{
homework >> number >> boolean;
if (boolean == 1)
{
sum += number;
}
else
{
cout << sum << endl;
sum = 0;
}
}
你有很多选择,一个可能是:
第一个:将布尔变量准确定义为(布尔变量)并将其与真假进行比较
示例:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string line;
bool boolean;
double number{ 0 };
double sum{ 0 };
ifstream homework;
homework.open("hw.txt");
while (!homework.eof())
{
homework >> number >> boolean;
if (boolean)
{
sum += number;
}
}
cout << sum << endl;
homework.close();
return 0;
}
要在发现 false 时停止 while,您可以使用 break
if (boolean)
{
sum += number;
}
else {
break;
}