在C 中计算数字的问题
Issue with numbers calculating in C++ outfile
我正在尝试创建一个文本文件输出,该文件输出显示了来自数据文件的区域投票的结果和细分。我有问题要计算适当的数字。投票文件(fotes.dat)每个行只包含两个字符"区和Y/N"。我不确定为什么只有少数条目(少于10)时,我会不断提出像4317448和2600960这样的数字。
#include <string>
string using namespace std
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
ifstream infile;
ofstream outfile;
char vote;
int district;
int overallTotal;
int yesTotal;
int noTotal;
int d1Yesvotes;
int d1Novotes;
int d2Yesvotes;
int d2Novotes;
int d3Yesvotes;
int d3Novotes;
//open file
infile.open("votes.dat");
infile>>district>>vote;
//if statement for 'yes' and 'no' votes, containing nested if statements
if (district=='1')
{
if(vote=='Y')
{
yesTotal+1;
d1Yesvotes+1;
overallTotal+1; }
else if(vote=='N')
{
noTotal+1;
d1Novotes+1;
overallTotal+1; }
}
else
if (district=='2')
{
if(vote=='Y')
{yesTotal+1;
d2Yesvotes+1;
overallTotal+1;}
else if(vote=='N')
{ noTotal+1;
d2Novotes+1;
overallTotal+1;}
}
else
if (district=='3')
{
if(vote=='Y')
{yesTotal+1;
d3Yesvotes+1;
overallTotal+1;}
else if(vote=='N')
{ noTotal+1;
d3Novotes+1;
overallTotal+1;}
}
outfile.open("votingresults.txt");
outfile<<endl<<"Number of Overall votes: "<<overallTotal;
outfile<<endl;
outfile<<endl<<"Number of Yes votes: "<<yesTotal;
outfile<<endl<<"Number of No votes: "<<noTotal;
outfile<<endl;
outfile<<endl<<"Number of District 1 Yes Votes: "<<d1Yesvotes;
outfile<<endl<<"Number of District 1 No Votes: "<<d1Yesvotes;
outfile<<endl;
outfile<<endl<<"Number of District 2 Yes Votes: "<<d1Yesvotes;
outfile<<endl<<"Number of District 2 No Votes: "<<d1Yesvotes;
outfile<<endl;
outfile<<endl<<"Number of District 3 Yes Votes: "<<d1Yesvotes;
outfile<<endl<<"Number of District 3 No Votes: "<<d1Yesvotes;
//close files
infile.close();
outfile.close();
return 0;
}
您的代码有一些错误。 - 它仅读取代码的1行,然后退出 - 您没有正确递增变量
yesTotal+1;
将有效地计算Yestotal 1,但不会在任何地方存储IY。您需要做以下
之一yesTotal=yesTotal+1;
yesTotal++; // This does the same thing
这是您的代码的固定版本
#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
ifstream infile;
ofstream outfile;
char vote;
int district;
int overallTotal=0;
int yesTotal=0;
int noTotal=0;
int d1Yesvotes=0;
int d1Novotes=0;
int d2Yesvotes=0;
int d2Novotes=0;
int d3Yesvotes=0;
int d3Novotes=0;
//open file
infile.open("votes.dat");
//if statement for 'yes' and 'no' votes, containing nested if statements
string line;
if (!infile.is_open()) {
cout << "Error opening file" << endl;
return 0;
}
while (getline(infile, line)) {
cout << line << endl;
if (line.size() < 2)
continue;
district=line[0];
vote=line[1];
if (district=='1')
{
if(vote=='Y')
{
yesTotal++;
d1Yesvotes++;
overallTotal++;
}
else if(vote=='N')
{
noTotal++;
d1Novotes++;
overallTotal++;
}
}
else if (district=='2')
{
if(vote=='Y')
{
yesTotal++;
d2Yesvotes++;
overallTotal++;
}
else if(vote=='N')
{
noTotal++;
d2Novotes++;
overallTotal++;
}
}
else if (district=='3')
{
if(vote=='Y') {
yesTotal++;
d3Yesvotes++;
overallTotal++;
}
else if(vote=='N')
{
noTotal++;
d3Novotes++;
overallTotal++;
}
}
}
outfile.open("votingresults.txt");
outfile<<endl<<"Number of Overall votes: "<<overallTotal;
outfile<<endl;
outfile<<endl<<"Number of Yes votes: "<<yesTotal;
outfile<<endl<<"Number of No votes: "<<noTotal;
outfile<<endl;
outfile<<endl<<"Number of District 1 Yes Votes: "<<d1Yesvotes;
outfile<<endl<<"Number of District 1 No Votes: "<<d1Yesvotes;
outfile<<endl;
outfile<<endl<<"Number of District 2 Yes Votes: "<<d2Yesvotes;
outfile<<endl<<"Number of District 2 No Votes: "<<d2Yesvotes;
outfile<<endl;
outfile<<endl<<"Number of District 3 Yes Votes: "<<d3Yesvotes;
outfile<<endl<<"Number of District 3 No Votes: "<<d3Yesvotes;
//close files
infile.close();
outfile.close();
return 0;
}
我还更改了输出变量以使用正确的变量,因为您的示例是为所有3个地区编写D1YESVOTES和D1NOVOTES。
相关文章:
- C++问题:用户认为数字1-100,程序提出问题不超过6次即可得到答案。无法正确
- 输出错误,问题是找到总和5000位数字cpp
- 使用 BMI 计算器对C++中的数字进行四舍五入的问题
- 遇到此问题时遇到困难:允许用户输入数组的值并使用 for,而循环也输出输入的最大数字
- 我的问题是关于C++中数字和序列的重复以及如何打印它们?
- 如何通过使用 2 位或更多数字的 XOR 运算符来执行此操作C++问题
- 在 cpp 中使用较大数字时遇到大数字的问题<iomanip>
- 无法理解代码背后的逻辑,这是在两个给定数字之间生成素数的优化问题
- 数组C++问题中最大和第二大的数字
- 使用用户定义的函数查找数字的幂时出现问题
- C++ - 我在输出时遇到问题,因为我正在收到数字和字母
- C++ 制作一个随机数字/字母生成器,代码中有问题
- 有问题将数字转换为罗马数字
- 使用C ,将转换为char的问题始终具有3位数字
- 优化递归问题以计算超级数字
- 快速读取带有数字 c++ 的二进制文件时出现问题
- 代码问题.使用矢量删除相同的数字,除了它的最后一个入口
- 编程:原理与实践 使用C++ 第 4 章演练步骤 6:关于数字范围的一般问题
- 在C++中使用限制和非常大的数字时出现问题
- 函数以相反的顺序输出输入问题,并改进算法以解释相等的数字