编辑:C ++上的输入和输出文件

时间:2016-11-09 06:46:26

标签: c++ visual-studio-2015

所以,出于某种原因,当我从某个文本文件中读取时,我只是阅读并输出第一个和第三个文件。我不知道为什么它根本不读它,不管第二个人是否有负数。这是我正在使用的文本文件:

编辑:修正了读取的问题,但现在我想让它意识到如果它读取一个空文件,它会识别它是空的,不要终止但是会给出一个像“文件是空的”这样的消息。 / p>

Joseph Kradin III

5000 6 4

Joe Boo

15000 2 12

南希亚当斯

15000 2 12

这是我的代码的一部分,我的循环和fin的东西都在那里:

fin >> firstName;
        getline(fin, lastName);
        fin >> deposit >> year >> numCompound;

    do
    {

        fullName = firstName + " " + lastName;

        // if statements to determine the interest rate depending on the years

        if (year >= 5)
            rate = 0.045;
        else if ((year < 5) & (year >= 4))
            rate = 0.04;
        else if ((year < 4) & (year >= 3))
            rate = 0.035;
        else if ((year < 3) & (year >= 2))
            rate = 0.025;
        else if ((year < 2) & (year >= 1))
            rate = 0.02;
        else
            rate = 0.015;

        if (deposit < 0 || year < 0 || numCompound < 0)
        {
            cout << fullName << " You have entered a negative number" << endl;
            fout << fullName << " You have entered a negative number" << endl;

        }

        else if (deposit > 0 || year > 0 || numCompound > 0)
        {
            numname++;

            // finding the value for amount of money aquired after n years, including interest.
            moneyAquired = deposit * pow((1 + rate / numCompound), numCompound*year);

            // finding earned interest

            earnedInterest = moneyAquired - deposit;

            // For the Total Deposit entered

            totaldeposit += deposit;

            // For total earned interest

            totalinterest += earnedInterest;

            cout << fixed << setprecision(2) << showpoint << setw(20) << left << fullName << setw(15) << year << setw(14) << rate * 100 << "$" << setw(15) << deposit << "$" << setw(15) << earnedInterest << "$" << setw(20) << moneyAquired << endl;

            fout << fixed << setprecision(2) << showpoint << setw(20) << left << fullName << setw(15) << year << setw(14) << rate * 100 << "$" << setw(15) << deposit << "$" << setw(15) << earnedInterest << "$" << setw(20) << moneyAquired << endl;

        }

    } while (fin >> firstName >> lastName >> deposit >> year >> numCompound && !fin.eof());

    //end of do while loop
    fout.close();
    fin.close();

1 个答案:

答案 0 :(得分:0)

移动

while (fin >> firstName >> lastName >> deposit >> year >> numCompound && !fin.eof())

开头而不是执行并删除

fin >> firstName;
getline(fin, lastName);
fin >> deposit >> year >> numCompound;