程序再次运行后,“小时”计数器不会重置

时间:2015-04-15 20:13:43

标签: c++ for-loop counter reset

我无法弄清楚如何重置“小时”计数器。

程序在第一次运行时运行完美,但是当用户输入另一组小时时,小时数开始在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;
}

输出:

  

输入几小时,看看你身后剩下多少咖啡因   喝了你的咖啡:4

     

第1小时113.1000mg
  第2小时98.3970mg
  第3小时85.6054mg
  第4小时74.4767mg

     

输入几小时,看看你身后剩下多少咖啡因   喝了你的咖啡:3

     

小时112 113.1000mg
  小时113 98.3970mg
  小时114 85.6054mg

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:2)

您永远不会重置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;
}