C++ &引用;小时“;反赢';重新运行程序后,t重置

C++ &引用;小时“;反赢';重新运行程序后,t重置,c++,for-loop,counter,reset,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

我不知道如何重置“小时”计数器

程序在第一次运行时运行正常,但当用户输入另一组小时数时,小时数开始在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;
}
#包括
#包括
#包括
使用名称空间std;
常量浮动初始体积=130.00;
常量浮动递减率=0.13;
int计数器=0;
int main()
{
整数小时,i,j,k;
浮动剩余体积、半体积、零体积;
while(cout小时)
{

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

#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;
}
#包括
#包括
#包括
使用名称空间std;
常量浮动初始体积=130.00;
常量浮动递减率=0.13;
int计数器=0;
int main()
{
整数小时,i,j,k;
浮动剩余体积、半体积、零体积;
while(cout小时)
{

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

#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;
}
#包括
#包括
#包括
使用名称空间std;
常量浮动初始体积=130.00;
常量浮动递减率=0.13;
int计数器=0;
int main()
{
整数小时,i,j,k;
浮动剩余体积、半体积、零体积;
while(cout小时)
{

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

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

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;
}
while(cout小时)
{
int计数器=0;

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

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

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;
}
while(cout小时)
{
int计数器=0;

cout或者更好,使用counter作为本地变量或者更好,使用counter作为本地变量当然这很简单,我想不出来。谢谢!!你应该在调试器中逐步检查应用程序并监视值。学习如何正确调试是你能给自己的最有用的技能之一。我正在尝试o找出是否应该在程序的末尾为计数器添加另一个循环。我没有想到要将计数器放在程序的开头。我感谢你的帮助!!没问题。很高兴我能提供帮助。当然,这很简单,我想不出来。谢谢你!!你应该在调试中逐步完成应用程序ger和监视值。学习如何正确调试是你能给自己的最有用的技能之一。我正试图弄清楚是否应该在程序末尾为计数器添加另一个循环。我没有想到要将计数器放在程序的开头。我感谢你的帮助!!没问题。很高兴我能提供帮助。