如何运行多行cin?
How to run multiple lines of cin?
我认为你应该使用 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";
}
m1
、m2
和 m3
是数字,但您可能输入了文本。尝试:
#include <iostream>
#include <iomanip>
#include <string>
int main()
{
double r1, r2, r3, rA;
std::string 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?
我试图询问月份和降雨量,然后在他们输入我想要第二个月和降雨量之后,然后再次询问第三个月。之后我只取这些的平均值。
但是当我运行我的代码你只能输入第一个月份和降雨量,然后它只是计算出以下两个问题而不让你回答
#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";
}
m1
、m2
和 m3
是数字,但您可能输入了文本。尝试:
#include <iostream>
#include <iomanip>
#include <string>
int main()
{
double r1, r2, r3, rA;
std::string 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";
}