简体   繁体   中英

infile not reading values from file c++

doing this for an assignment, I can't seem to get past this. I'm trying to read values from a text file. the data from the file is in the following format:

int string string double string int double int string bool.

eg of a line(a text file about insurance claims with multiple lines)

1 Accident 2014-01-26 112049.26 Male 46 112049.00 2013 Red 1

I have to sort the values of the claim amounts by the colours in the file and add them. then output the totals.

So far I can't get my program to read the values correctly from the file, not sure what I'm doing wrong though.

Any explanations and pointers in the right direction as to why that might be would be appreciated.

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

You monetary data is floating point (contains decimals), but you are using integers. The reading of the input data will stop at the decimal point (because decimal pointers are not integral format).

Declare your monetary variables as double :

double claimAmount;
double vehicleValue;

Note: when you are making monetary arithmetic, all your variables should be double .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM