infile没有从文件c ++中读取值

时间:2015-03-21 14:55:20

标签: c++ file-io multiple-columns

执行此任务, 我似乎无法克服这个问题。 我试图从文本文件中读取值。 文件中的数据采用以下格式:

int string string double string int double int string bool。

e.g。一行(关于多行保险索赔的文本文件)

1事故2014-01-26 112049.26男46 112049.00 2013红1

我必须按照文件中的颜色对索赔金额的值进行排序并添加它们。然后输出总数。

到目前为止,我无法让我的程序从文件中正确读取值, 不知道我做错了什么。

对于为什么会这样做的正确方向的任何解释和指示都将受到赞赏。

#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
#include<cmath>

using namespace std;
int main()
{

    double claimTotalBlue=0;
    double claimTotalRed=0;
    double claimTotalWhite=0;
    int blue=0;
    int red=0;
    int white=0;

    double averagered;
    double averageblue;
    double averagewhite;
    int menuchoice;

    cout<<"1. Blue cars"<<endl;
    cout<<"2. White cars"<<endl;
    cout<<"3. Red cars"<<endl;
    cout<<"4. Quit"<<endl;
    cout<<"Enter your choice (1-4): ";

    cin>>menuchoice;
    cout<<endl;
    cout<<setfill('-');



    ifstream inFile;
    inFile.open("claims.dat");

    while (inFile)
    { 
            int policyNumber, age, vehicleValue, yearOfManufacture, claimAmount;
        string type;
        string date;
        string gender;
        string colour;
        bool immobilizer;

           inFile>>policyNumber>>type>>date>>claimAmount>>gender>>age>>vehicleValue>>yearOfManufacture>>colour>>immobilizer;         

        if(colour==string("Blue"))
        {
            blue=blue++;
            claimTotalBlue=claimTotalBlue+claimAmount;

        }
        if(colour==string("White"))
        {
            white=white++;
            claimTotalWhite=claimTotalWhite+claimAmount;

        }
        if(colour==string("Red"))
        {
            red=red++;
            claimTotalRed=claimTotalRed+claimAmount;

        }
    };
    if(menuchoice==1)
    {
        averageblue=(claimTotalBlue/blue);

        cout<<"Received "<<blue<<" claims in respect of blue cars"<<endl;
        cout<<"The total value of claims in respect of blue cars is R "<<claimTotalBlue<<endl;
        cout<<"The average value of claims in respect of blue cars is R "<<averageblue<<endl;
    }       
    if(menuchoice==2)
    {
        averagewhite=(claimTotalWhite/white);

        cout<<"Received "<<white<<" claims in respect of white cars"<<endl;
        cout<<"The total value of claims in respect of white cars is R "<<claimTotalWhite<<endl;
        cout<<"The average value of claims in respect of white cars is R "<<averagewhite<<endl;
    }       
    if(menuchoice==3)
    {
        averagered=(claimTotalRed/red);

        cout<<"Received "<<red<<" claims in respect of red cars"<<endl;
        cout<<"The total value of claims in respect of red cars is R "<<claimTotalRed<<endl;
        cout<<"The average value of claims in respect of red cars is R "<<averagered<<endl;
    }       
    if(menuchoice==4)
    {

        return 0;
    };      


    return 0;
}

1 个答案:

答案 0 :(得分:0)

您的货币数据是浮点数(包含小数),但您使用的是整数。输入数据的读取将停止在小数点(因为十进制指针不是整数格式)。

将货币变量声明为double

double claimAmount;
double vehicleValue;

注意:当您进行货币算术时,所有变量都应为double