正在计算while循环中的值
Calculating values from a while loop?
我需要使用输入while循环的数据来计算和和和最大值。我很困惑如何将我存储为"weathervalue"的每个"天"的用户输入作为"weathervalue",并在循环之外的公式中使用它。这是我的密码。
#include <iostream>
using namespace std;
int main ()
{
double weatherdays;
double weathervalue;
double counter;
double day;
cout << "Enter the number of days for which you have weather data - ";
cin >> weatherdays;
counter = 1;
day = 1;
while (counter <= weatherdays)
{
counter++;
cout << "What is the rainfall for day " << day << endl;
cin >> weathervalue;
cout << "What is the max temp for day " << day << endl;
cin >> weathervalue;
day = day + 1;
}
weathervalue = //formula for sum of rainfall values entered in while loop
cout << "The total rainfall is " << weathervalue << endl;
weathervalue = //formula for max temp values entered in while loop
cout << "The max temperature is " << weathervalue << endl;
您不需要数组来完成此任务。表示当前状态的两个变量可以:
#include <iostream>
using namespace std;
int main ()
{
int weatherdays;
int day = 0;
double rainfall;
double temperature;
double sumRainfall = 0.0;
double maxTemperature = 0.0;
cout << "Enter the number of days for which you have weather data - ";
cin >> weatherdays;
while (weatherdays--)
{
++day;
cout << "What is the rainfall for day " << day << endl;
cin >> rainfall;
sumRainfall += rainfall;
cout << "What is the max temp for day " << day << endl;
cin >> temperature;
if(temperature > maxTemperature)
{
maxTemperature = temperature;
}
}
cout << "The total rainfall is " << sumRainfall << endl;
cout << "The max temperature is " << maxTemperature << endl;
return 0;
}
您也可以使用<algorithm>
中的std
算法,但那只是小菜一碟!
我想您在问如何存储所有输入的值?
使用集合,例如vector
。
cin >> wearhervalue;
vals.push_back (weathervalue);
相关文章:
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- while循环中while循环的时间复杂度是多少
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 擦除while循环中迭代的元素
- 为什么我不能在 while 循环中创建线程?
- 退出简单while循环时出现问题
- 如果条件为TRUE(最佳方式?),则在do while循环中后置增量
- 即使没有满足他们的条件,我也无法通过一些 do-while 循环
- 嵌套While循环不起作用(C++问题)
- 如何通过替换顺序代码的while循环来添加OpenMP for循环
- 使用 GMP 的 while 循环出现问题
- 程序以使用 C++ 中的 while 循环查找一组数字的最小值
- 为什么在 while 循环中返回表达式不起作用
- 如何使用 do while 循环确定最高值和最低值
- 如何读取单个字符并在输入两个字符序列时输出? 使用 while 循环和C++
- 用于在 C++ 中使用 while 循环查找下一个素数的简单函数
- OpenCV QT,显示视频的帧(不使用while循环)
- 输入循环 while 函数
- c++中的循环(while)
- 为什么这段代码没有循环(while)