C++ 编辑:在C++;

C++ 编辑:在C++;,c++,visual-studio-2015,C++,Visual Studio 2015,因此,出于某种原因,当我从某个文本文件中读取时,我只读取并输出第一个和第三个文本文件。我不知道为什么它根本不读,不管第二个人是否有负数。以下是我正在使用的文本文件: 编辑:修复了读取的问题,但现在我想让它意识到,如果它读取一个空文件,它会识别它是空的,不要终止,而是给出一条类似“file was empty”的消息 约瑟夫·克拉丁三世 5000 6 4 乔布 15000 2 12 南希亚当斯 15000 2 12 这是我的代码的一部分,我的循环和fin都在这里: fin >> fir

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

编辑:修复了读取的问题,但现在我想让它意识到,如果它读取一个空文件,它会识别它是空的,不要终止,而是给出一条类似“file was empty”的消息

约瑟夫·克拉丁三世

5000 6 4

乔布

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();
fin>>名字;
getline(fin,lastName);
fin>>存款>>年份>>numCompound;
做
{
fullName=firstName+“”+lastName;
//根据年份确定利率的if报表
如果(年份>=5)
比率=0.045;
如果((年份<5)和(年份>=4))
比率=0.04;
如果((年份<4)和(年份>=3))
比率=0.035;
如果((年份<3)和(年份>=2))
比率=0.025;
否则,如果((年份<2)和(年份>=1))
比率=0.02;
其他的
比率=0.015;
如果(存款<0 | |年<0 | |金额<0)
{
不能移动

转到开头而不是执行并删除

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

是的,这里是你能想到的所有帖子,除了一个实际的问题。是的,我实际上没有看你的回复就这么做了哈哈。但是我还有另一个问题。Idk如何让程序意识到文件是空的?方向教授说我们不应该在阅读空文件时终止,而是给出这样的信息“文件是空的”。我们添加一个计数器,如果结尾没有增加,打印文件是空的。另外,不要重复代码:用
do{…}while(…);
替换
while{…}
fin >> firstName;
getline(fin, lastName);
fin >> deposit >> year >> numCompound;