我认为您应该使用cin.getline?
我试图询问降雨的月份和数量,然后在他们输入之后,我想要第二个月的降雨和数量,然后再询问第三个月。在那之后,我简单地取这些平均值。
但是当我运行代码时,您只能输入第一个月的降雨量和降雨量,然后它只给出以下两个问题,而您却无法回答。
#include <iostream>
#include <iomanip>
int main()
{
double r1, r2, r3, rA;
int m1, m2, m3;
std::cout << "Average Rainfall Calculator\n";
std::cout << "Please enter your first month followed by the amount of rain in inches:\n";
std::cin >> m1;
std::cin >> r1;
std::cout << "\nPlease enter your second month followed by the amount of rain in inches:\n";
std::cin >> m2;
std::cin >> r2;
std::cout << "\nPlease enter your third month followed by the amount of rain in inches:\n";
std::cin >> m3;
std::cin >> r3;
rA = (r1 + r2 + r3) / 3;
std::cout << "The amount of rainfall for: " << m1 << ", " << m2 << ", and " << m3 << "is " << std::setprecision(2) << std::fixed << rA << "inches of rain.\n";
}
我认为您应该使用cin.getline?我想问问降雨的月份和数量,然后在他们输入后,我想要第二个月的降雨和数量,然后再次询问...
m1
,m2
和m3
是数字,但您可能输入了文本。试试: