我的问题 = 直到用户按下 "enter" 从 c++ 中的用户输入取平均值
My question = until user press "enter" take average from user inputs in c++
此代码计算从用户输入整数到用户输入“666”整数的平均值。但是,我想让它在用户按下回车键时停止。我怎样才能做到这一点?
#include <iostream>
using namespace std; //s
int main()
{
int total = 0, counter = 0, number, average;
do
{
cin >> number;
if(number == 666) //what should i do to make enter button instead of 666?
{
average = total / counter;
cout << average;
return 0;
}
total = total + number;
counter = counter + 1;
} while(1);
return 0;
}
遗憾的是,如果 <ENTER>
按钮已被按下,您无法轻松检查。 cin
读取格式化输入(在您的情况下为数字)并忽略其他所有内容(包括空格,如换行符)。解决您的问题的方法是读取整行并从中提取数字:
#include <iostream> // cin, cout
#include <sstream> // istringstream
#include <string> // getline
int main()
{
// Reading a line from the standard input to the variable 'line'.
std::string line;
std::getline(std::cin, line);
// The easiest way to get the numbers from 'line' is to wrap it in an
// 'istringstream'. Now, you can use 'iss' just like 'cin'.
std::istringstream iss{line};
double total = 0.0;
double counter = 0.0;
for (double number; iss >> number; ++counter) {
total += number;
}
std::cout << "Avarage: " << total / counter << '\n';
return 0;
}
我已经找到了解决方案:
请注意,我使用 Code::Blocks 编译器,因此我不得不进行调整。
设置>编译器>勾选“"have g++ follow the c++11 ISO C++ language standard [-std=c++11]"”框,然后单击确定。
解决方案如下:
#include <iostream>
#include <string>
using namespace std;
int main() {
float c = 0, sum = 0;
string input;
while (true) {
cout << "input number:";
getline(cin,input);
if (input == "") {
cout << "average:" << sum / c << endl;
break;
}
sum += stof(input);
c++;
}
return 0;
}
此代码计算从用户输入整数到用户输入“666”整数的平均值。但是,我想让它在用户按下回车键时停止。我怎样才能做到这一点?
#include <iostream>
using namespace std; //s
int main()
{
int total = 0, counter = 0, number, average;
do
{
cin >> number;
if(number == 666) //what should i do to make enter button instead of 666?
{
average = total / counter;
cout << average;
return 0;
}
total = total + number;
counter = counter + 1;
} while(1);
return 0;
}
遗憾的是,如果 <ENTER>
按钮已被按下,您无法轻松检查。 cin
读取格式化输入(在您的情况下为数字)并忽略其他所有内容(包括空格,如换行符)。解决您的问题的方法是读取整行并从中提取数字:
#include <iostream> // cin, cout
#include <sstream> // istringstream
#include <string> // getline
int main()
{
// Reading a line from the standard input to the variable 'line'.
std::string line;
std::getline(std::cin, line);
// The easiest way to get the numbers from 'line' is to wrap it in an
// 'istringstream'. Now, you can use 'iss' just like 'cin'.
std::istringstream iss{line};
double total = 0.0;
double counter = 0.0;
for (double number; iss >> number; ++counter) {
total += number;
}
std::cout << "Avarage: " << total / counter << '\n';
return 0;
}
我已经找到了解决方案: 请注意,我使用 Code::Blocks 编译器,因此我不得不进行调整。 设置>编译器>勾选“"have g++ follow the c++11 ISO C++ language standard [-std=c++11]"”框,然后单击确定。 解决方案如下:
#include <iostream>
#include <string>
using namespace std;
int main() {
float c = 0, sum = 0;
string input;
while (true) {
cout << "input number:";
getline(cin,input);
if (input == "") {
cout << "average:" << sum / c << endl;
break;
}
sum += stof(input);
c++;
}
return 0;
}