在C ++ outfile中计算数字的问题

时间:2017-09-20 16:30:09

标签: c++ file-io

我正在尝试创建一个文本文件输出,显示区域投票的结果和细分来自数据文件。我有问题得到正确的数字来计算。投票文件(votes.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 个答案:

答案 0 :(得分: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。