簡體   English   中英

Infile無法從文件C ++讀取值

[英]infile not reading values from file c++

這樣做是為了完成任務,我似乎無法超越。 我正在嘗試從文本文件讀取值。 文件中的數據采用以下格式:

int字符串字符串double字符串int double int字符串布爾值。

例如一行(關於多行保險索賠的文本文件)

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;
}

您的貨幣數據是浮點數(包含小數),但是您使用的是整數。 輸入數據的讀取將停止在小數點(因為小數點指針不是整數格式)。

將您的貨幣變量聲明為double

double claimAmount;
double vehicleValue;

注意:進行貨幣算術時,所有變量都應為double

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM