无法使用 stringstream 读入 int 和 double
Unable to use stringstream to read in int and double
#include <iostream>
#include <sstream>//for istringstream
int main(){
std::istringstream iss;
std::string tempSTR;
int A;
double B;
std::cout<<"Please enter an int.\n";
std::cin>>tempSTR;
iss.str(tempSTR);
iss>>A;
std::cout<<"A = "<<A<<"\n";
std::cout<<"Please enter a double.\n";
std::cin>>tempSTR;
iss.str(tempSTR);
iss>>B;
std::cout<<"B = "<<B<<"\n";
std::cout<<"\nEND OF PROGRAM. GOODBYE!\n\n";
}//end of main
代码很简单,但就是行不通。我只需要使用字符串流读入一个 int,然后读入一个 double。读入 int 首先有效,但 double 只是输出为零。我做错了什么?
``
#include <iostream>
#include <sstream>//for istringstream
//Using stringstream to read in multiple arguments
int main(){
std::istringstream iss;
std::string tempSTR;
int A;
double B;
std::cout<<"Please enter an int.\n";
std::cin>>tempSTR;
iss.str(tempSTR);
iss>>A;
iss.clear();
std::cout<<"A = "<<A<<"\n";
std::cout<<"Please enter a double.\n";
std::cin>>tempSTR;
iss.str(tempSTR);
iss>>B;
iss.clear();
std::cout<<"B = "<<B<<"\n";
std::cout<<"\nEND OF PROGRAM. GOODBYE!\n\n";
}//end of main
``
//Question:Unable to use stringstream to read in int and double
//Answer:stream just needed to be cleared before using it to read in another argument.
#include <iostream>
#include <sstream>//for istringstream
int main(){
std::istringstream iss;
std::string tempSTR;
int A;
double B;
std::cout<<"Please enter an int.\n";
std::cin>>tempSTR;
iss.str(tempSTR);
iss>>A;
std::cout<<"A = "<<A<<"\n";
std::cout<<"Please enter a double.\n";
std::cin>>tempSTR;
iss.str(tempSTR);
iss>>B;
std::cout<<"B = "<<B<<"\n";
std::cout<<"\nEND OF PROGRAM. GOODBYE!\n\n";
}//end of main
代码很简单,但就是行不通。我只需要使用字符串流读入一个 int,然后读入一个 double。读入 int 首先有效,但 double 只是输出为零。我做错了什么?
``
#include <iostream>
#include <sstream>//for istringstream
//Using stringstream to read in multiple arguments
int main(){
std::istringstream iss;
std::string tempSTR;
int A;
double B;
std::cout<<"Please enter an int.\n";
std::cin>>tempSTR;
iss.str(tempSTR);
iss>>A;
iss.clear();
std::cout<<"A = "<<A<<"\n";
std::cout<<"Please enter a double.\n";
std::cin>>tempSTR;
iss.str(tempSTR);
iss>>B;
iss.clear();
std::cout<<"B = "<<B<<"\n";
std::cout<<"\nEND OF PROGRAM. GOODBYE!\n\n";
}//end of main
``
//Question:Unable to use stringstream to read in int and double
//Answer:stream just needed to be cleared before using it to read in another argument.