"hour" 计数器不会重置,程序再次运行 后

"hour" counter won't reset, after the program is run again

我不知道如何重置 "hour" 计数器。

程序 运行 在 1 日完美 运行,但是当用户输入另一组小时数时,小时数开始在 110-117 之间编号。

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

const float initialVolume = 130.00; 
const float decreaseRate = 0.13; 
int counter = 0;

int main()
{
int hours,i,j,k;
float remainingVolume, halfVolume, zeroVolume;

while (cout << "Enter hours to see how much caffeine "
     << "is left in your body, after you drank your coffee: " && cin >> hours)
{
cout << endl;
cout << fixed << showpoint << setprecision(4);

remainingVolume = initialVolume;

for (i = 0; i < hours; i++)
{
    counter++;
    remainingVolume = remainingVolume - decreaseRate * remainingVolume;
    cout << "Hour " << setw(5) << counter << setw(15) << remainingVolume << "mg"<< endl;
}

for (j = 0, halfVolume = 130.00; halfVolume > 65.0000; j++)
{
    counter++;
    halfVolume = halfVolume - decreaseRate * halfVolume;
}

for (k = 0, zeroVolume = 130.00; zeroVolume > 0.0001; k++)
{
    counter++;
    zeroVolume = zeroVolume - decreaseRate * zeroVolume;
}

cout << "\n" << endl;
cout << "It will take " << j << " hours to get caffeine levels to 65mg. \n" << endl;
cout << "It will take " << k << " hours to get caffeine levels to 0mg. \n\n" << endl;

}

return 0;
}

输出:

Enter hours to see how much caffeine is left in your body, after you drank your coffee: 4

Hour 1 113.1000mg
Hour 2 98.3970mg
Hour 3 85.6054mg
Hour 4 74.4767mg

Enter hours to see how much caffeine is left in your body, after you drank your coffee: 3

Hour 112 113.1000mg
Hour 113 98.3970mg
Hour 114 85.6054mg

您需要在完成迭代后重置计数器:

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

const float initialVolume = 130.00; 
const float decreaseRate = 0.13; 
int counter = 0;

int main()
{
int hours,i,j,k;
float remainingVolume, halfVolume, zeroVolume;

while (cout << "Enter hours to see how much caffeine "
     << "is left in your body, after you drank your coffee: " && cin >> hours)
{
cout << endl;
cout << fixed << showpoint << setprecision(4);

remainingVolume = initialVolume;

for (i = 0; i < hours; i++)
{
    counter++;
    remainingVolume = remainingVolume - decreaseRate * remainingVolume;
    cout << "Hour " << setw(5) << counter << setw(15) << remainingVolume << "mg"<< endl;
}

for (j = 0, halfVolume = 130.00; halfVolume > 65.0000; j++)
{
    counter++;
    halfVolume = halfVolume - decreaseRate * halfVolume;
}

for (k = 0, zeroVolume = 130.00; zeroVolume > 0.0001; k++)
{
    counter++;
    zeroVolume = zeroVolume - decreaseRate * zeroVolume;
}

cout << "\n" << endl;
cout << "It will take " << j << " hours to get caffeine levels to 65mg. \n" << endl;
cout << "It will take " << k << " hours to get caffeine levels to 0mg. \n\n" << endl;

counter = 0; // <--
}

return 0;
}

您永远不会重置您的 counter 值。您将其初始化为 0,然后在每个循环中增加它。每次他们输入新数字时(即在您的 while 循环中),您都需要将其重置为 0。

最好的解决方案是必须将计数器变量作为在循环中声明的局部变量:

while (cout << "Enter hours to see how much caffeine "
     << "is left in your body, after you drank your coffee: " && cin >> hours)
{
int counter=0;
cout << endl;
cout << fixed << showpoint << setprecision(4);

remainingVolume = initialVolume;

for (i = 0; i < hours; i++)
{
    counter++;
    remainingVolume = remainingVolume - decreaseRate * remainingVolume;
    cout << "Hour " << setw(5) << counter << setw(15) << remainingVolume << "mg"<< endl;
}

for (j = 0, halfVolume = 130.00; halfVolume > 65.0000; j++)
{
    counter++;
    halfVolume = halfVolume - decreaseRate * halfVolume;
}

for (k = 0, zeroVolume = 130.00; zeroVolume > 0.0001; k++)
{
    counter++;
    zeroVolume = zeroVolume - decreaseRate * zeroVolume;
}

cout << "\n" << endl;
cout << "It will take " << j << " hours to get caffeine levels to 65mg. \n" << endl;
cout << "It will take " << k << " hours to get caffeine levels to 0mg. \n\n" << endl;

}

return 0;
}