Steps for using Stringstream:
Include the Stringstream library.
#include<sstream>
Use getline to get the string from the user
std::getline(std::cin, stringVariable);
Convert the string variable to a float variable.
std::stringstream(stringVariable) >> numericVariable;
We can see the steps applied in this example:
#include <iostream>
#include <string>
#include <sstream>
int main ()
{
std::string stringLength;
float inches = 0;
float yardage = 0;
std::cout << "Enter number of inches: ";
std::getline (std::cin,stringLength);
std::stringstream(stringLength) >> inches;
std::cout<<"You entered "<<inches<<"\n";
yardage = inches/36;
std::cout << "Yardage is " << yardage;
return 0;
}
sstream的一些技巧
最新推荐文章于 2024-04-20 12:18:01 发布