#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
#if 0//extract 1
string name;
int age;
float weight;
stringstream stream("mary 25 63.4");
stream>>name>>age>>weight;
cout<<"your information is "<<endl;
cout<<name<<" "<<age<<" "<<weight<<endl;
#elif 0//extract 2
string name, tmp;
int age;
float weight;
stringstream stream;
cout<<"input your name:"<<endl;
cin>>tmp;
stream<<tmp;
cout<<"input your age:"<<endl;
stream.clear(); /* if you want use the same one stream to convert,please invoke clear() before,or you will error
void clear (iostate state = goodbit);
Set error state flags
Sets a new value for the stream's internal error state flags.
The current value of the flags is overwritten: All bits are replaced by those in state; If state is goodbit (which is zero) all error flags are cleared.
The current state can be obtained with member function rdstate.*/
stream<<tmp;
stream>>age;
cout<<"input your weight:"<<endl;
stream.clear();
cin>>tmp;
stream<<tmp;
stream>>weight;
// stream>>name>>age>>weight;//error ---all input string will be chained to one string
cout<<"your information is "<<endl;
cout<<name<<" "<<age<<" "<<weight<<endl;
#elif 1 //insert
stringstream stream;
string name;
int age;
float weight;
cout<<"input your name:";
cin>>name;
cout<<"input your age:";
cin>>age;
cout<<"input your weight:";
cin>>weight;
stream<<"name:"<<name<<"\nage:"<<age<<"\nweight:"<<weight;
cout<<stream.str()<<endl;
#endif
return 0;
}
#include <sstream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
#if 0//extract 1
string name;
int age;
float weight;
stringstream stream("mary 25 63.4");
stream>>name>>age>>weight;
cout<<"your information is "<<endl;
cout<<name<<" "<<age<<" "<<weight<<endl;
#elif 0//extract 2
string name, tmp;
int age;
float weight;
stringstream stream;
cout<<"input your name:"<<endl;
cin>>tmp;
stream<<tmp;
stream>>name;
cout<<"input your age:"<<endl;
stream.clear(); /* if you want use the same one stream to convert,please invoke clear() before,or you will error
void clear (iostate state = goodbit);
Set error state flags
Sets a new value for the stream's internal error state flags.
The current value of the flags is overwritten: All bits are replaced by those in state; If state is goodbit (which is zero) all error flags are cleared.
In the case that no stream buffer is associated with the stream when this function is called, the badbit flag is automatically set
(no matter the value for that bit passed in argument state).
Note that changing the state may throw an exception, depending on the latest settings passed to member exceptions.The current state can be obtained with member function rdstate.*/
stream<<tmp;
stream>>age;
cout<<"input your weight:"<<endl;
stream.clear();
cin>>tmp;
stream<<tmp;
stream>>weight;
// stream>>name>>age>>weight;//error ---all input string will be chained to one string
cout<<"your information is "<<endl;
cout<<name<<" "<<age<<" "<<weight<<endl;
#elif 1 //insert
stringstream stream;
string name;
int age;
float weight;
cout<<"input your name:";
cin>>name;
cout<<"input your age:";
cin>>age;
cout<<"input your weight:";
cin>>weight;
stream<<"name:"<<name<<"\nage:"<<age<<"\nweight:"<<weight;
cout<<stream.str()<<endl;
#endif
return 0;
}