在C 中计算数字的问题

Issue with numbers calculating in C++ outfile

本文关键字:问题 数字 计算      更新时间:2023-10-16

我正在尝试创建一个文本文件输出,该文件输出显示了来自数据文件的区域投票的结果和细分。我有问题要计算适当的数字。投票文件(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。