为什么 getline 只读取第一笔?
Why does getline reads only the first stroke?
我的程序应该读取命令并用一个数字来执行它们。它应该是 +, -, /, * 数字,但它只读取第一笔画。
ifstream fin("file.txt");
string line;
while(getline(fin,line))
{
if(line[0] == '+')
{
for(int i = 1; i < 10; i++)
{
n += line[i];
}
ss << n;
double p = 0.0;
ss >> p;
res += p;
}
n = "";
if(line[0] == '-')
{
for(int i = 1; i < 10; i++)
{
n += line[i];
}
ss << n;
double m = 0.0;
ss >> m;
res -= m;
}
n = "";
if(line[0] == '*')
{
for(int i = 1; i < 10; i++)
{
n += line[i];
}
ss << n;
double di = 0.0;
ss >> di;
res *= di;
}
n = "";
if(line[0] == '/')
{
for(int i = 1; i < 10; i++)
{
n += line[i];
}
ss << n;
double mu = 0.0;
ss >> mu;
res /= mu;
}
n = "";
}
文件示例:
+1
-2
*5
输出:
1
预期输出是-5,但它输出了1。
为什么会这样,我该怎么办?
ss 是字符串流,n 是字符串,res 是双精度数,strokes[] 是字符串。
你已经自己发现了。您必须清除 std::istringstream
或定义一个新的,即使名称相同。所以你可以写
ss.clear(); // Clear EOF bit
ss.str(std::string()); // Clear buffer
在下一次插入操作之前,或者,你只需每次定义一个新变量,写成:
std::stringstream ss{};
ss << n;
我还将您的代码修改为最小可重现示例:
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
int main() {
std::string line{};
std::stringstream ss{};
std::string n{};
double res{};
std::ifstream fin("file.txt");
while (std::getline(fin, line))
{
if (line[0] == '+')
{
for (int i = 1; i < line.length(); i++)
{
n += line[i];
}
ss << n;
double p = 0.0;
ss >> p;
res += p;
}
n = "";
if (line[0] == '-')
{
for (int i = 1; i < line.length(); i++)
{
n += line[i];
}
ss.clear(); // Clear EOF bit
ss.str(std::string()); // Clear buffer
ss << n;
double m = 0.0;
ss >> m;
res -= m;
}
n = "";
if (line[0] == '*')
{
for (int i = 1; i < line.length(); i++)
{
n += line[i];
}
ss.clear(); // Clear EOF bit
ss.str(std::string()); // Clear buffer
ss << n;
double di = 0.0;
ss >> di;
res *= di;
}
n = "";
if (line[0] == '/')
{
for (int i = 1; i < line.length(); i++)
{
n += line[i];
}
ss.clear(); // Clear EOF bit
ss.str(std::string()); // Clear buffer
ss << n;
double mu = 0.0;
ss >> mu;
res /= mu;
}
n = "";
}
std::cout << res;
return 0;
}
我还添加了一个更现代的 C++ 代码:
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
int main() {
if (std::ifstream ifs{ "file.txt" }; ifs) {
double result{};
for (std::string line{}; std::getline(ifs, line); ) {
char calculationOperator{};
double number{};
std::istringstream iss{ line };
if (iss >> calculationOperator >> number) {
switch (calculationOperator) {
case '+':
result += number;
break;
case '-':
result -= number;
break;
case '*':
result *= number;
break;
case '/':
result /= number;
break;
default:
std::cerr << "\n\nError: invalid mathematical operator '" << calculationOperator << "' found\n\n";
}
}
else
std::cerr << "\n\nError: Wronng input in line: " << line << "\n\n";
}
std::cout << "\n\nResult: " << result << "\n\n";
}
else
std::cerr << "\n\nError: Could not open 'file.txt'\n\n";
return 0;
}
我的程序应该读取命令并用一个数字来执行它们。它应该是 +, -, /, * 数字,但它只读取第一笔画。
ifstream fin("file.txt");
string line;
while(getline(fin,line))
{
if(line[0] == '+')
{
for(int i = 1; i < 10; i++)
{
n += line[i];
}
ss << n;
double p = 0.0;
ss >> p;
res += p;
}
n = "";
if(line[0] == '-')
{
for(int i = 1; i < 10; i++)
{
n += line[i];
}
ss << n;
double m = 0.0;
ss >> m;
res -= m;
}
n = "";
if(line[0] == '*')
{
for(int i = 1; i < 10; i++)
{
n += line[i];
}
ss << n;
double di = 0.0;
ss >> di;
res *= di;
}
n = "";
if(line[0] == '/')
{
for(int i = 1; i < 10; i++)
{
n += line[i];
}
ss << n;
double mu = 0.0;
ss >> mu;
res /= mu;
}
n = "";
}
文件示例:
+1
-2
*5
输出:
1
预期输出是-5,但它输出了1。 为什么会这样,我该怎么办? ss 是字符串流,n 是字符串,res 是双精度数,strokes[] 是字符串。
你已经自己发现了。您必须清除 std::istringstream
或定义一个新的,即使名称相同。所以你可以写
ss.clear(); // Clear EOF bit
ss.str(std::string()); // Clear buffer
在下一次插入操作之前,或者,你只需每次定义一个新变量,写成:
std::stringstream ss{};
ss << n;
我还将您的代码修改为最小可重现示例:
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
int main() {
std::string line{};
std::stringstream ss{};
std::string n{};
double res{};
std::ifstream fin("file.txt");
while (std::getline(fin, line))
{
if (line[0] == '+')
{
for (int i = 1; i < line.length(); i++)
{
n += line[i];
}
ss << n;
double p = 0.0;
ss >> p;
res += p;
}
n = "";
if (line[0] == '-')
{
for (int i = 1; i < line.length(); i++)
{
n += line[i];
}
ss.clear(); // Clear EOF bit
ss.str(std::string()); // Clear buffer
ss << n;
double m = 0.0;
ss >> m;
res -= m;
}
n = "";
if (line[0] == '*')
{
for (int i = 1; i < line.length(); i++)
{
n += line[i];
}
ss.clear(); // Clear EOF bit
ss.str(std::string()); // Clear buffer
ss << n;
double di = 0.0;
ss >> di;
res *= di;
}
n = "";
if (line[0] == '/')
{
for (int i = 1; i < line.length(); i++)
{
n += line[i];
}
ss.clear(); // Clear EOF bit
ss.str(std::string()); // Clear buffer
ss << n;
double mu = 0.0;
ss >> mu;
res /= mu;
}
n = "";
}
std::cout << res;
return 0;
}
我还添加了一个更现代的 C++ 代码:
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
int main() {
if (std::ifstream ifs{ "file.txt" }; ifs) {
double result{};
for (std::string line{}; std::getline(ifs, line); ) {
char calculationOperator{};
double number{};
std::istringstream iss{ line };
if (iss >> calculationOperator >> number) {
switch (calculationOperator) {
case '+':
result += number;
break;
case '-':
result -= number;
break;
case '*':
result *= number;
break;
case '/':
result /= number;
break;
default:
std::cerr << "\n\nError: invalid mathematical operator '" << calculationOperator << "' found\n\n";
}
}
else
std::cerr << "\n\nError: Wronng input in line: " << line << "\n\n";
}
std::cout << "\n\nResult: " << result << "\n\n";
}
else
std::cerr << "\n\nError: Could not open 'file.txt'\n\n";
return 0;
}