I take a text from the user, for example, the user types:
5 inch 10 foot
How do I separate 5 and 10 from the text? I want to do a calculation on 5 and another calculation on 10
I take a text from the user, for example, the user types:
5 inch 10 foot
How do I separate 5 and 10 from the text? I want to do a calculation on 5 and another calculation on 10
use stringstream...take the integers and push it in vector:- I'm taking words also but not doing anything with it.
vector<int> parseInts(string str) {
stringstream ss(str);
int num;
char words;
vector<int>v;
while(ss>>num){
v.push_back(num);
ss>>words;
}
return v;
}
int main() {
string str;
cin >> str;
vector<int> integers = parseInts(str);
int inch=integers[0];
int foot=integers[1];
//do what you need to do here
return 0;
}
You didn't specify which modules are in your text and what type of conversions you want to perform. Based on your comment, I suppose you have 'foot' and 'inch' in your code and you want to convert them to meter and cm. If you have any other modules or conversions, this code needs a little modification:
// input: 3 foot 5 inch
// output: 0.9144 meter 12.7 cm
#include <iostream>
#include <string>
using namespace std;
int main()
{
string text = "";
getline(std::cin, text);
for (int i = 0, SIZE = text.size(); i < SIZE; i++) {
if (isdigit(text[i])) {
int number = (int)text[i] - 48; // Convert char to int
i++;
for (; i < SIZE && isdigit(text[i]); i++) {
// Iterate string to extract this number with all its digits:
number = number * 10 + (int)text[i] - 48;
}
for (; i < SIZE - 3; i++) {
if (tolower(text[i]) == 'f' || tolower(text[i] == 'i')) { // Search for the beginning of foot or inch
const string module = text.substr(i, 4); // Copy 4 characters of text (from position I)
if (module.compare("foot") == 0) {
const float meter = (float)(number * 0.3048);
cout << meter << " meter" << "\t";
break;
}
else if (module.compare("inch") == 0) {
const float cm = (float)(number * 2.54);
cout << cm << " cm" << "\t";
break;
}
}
}
}
}
}
Here is my proposition, using a regex
and storing all the extracted numbers in a vector
.
I first read a whole sentence from the console and apply a std::regex
on each word of the sentence and store the eventual number from each word in a vector.
#include <iostream>
#include <string>
#include <regex>
#include <sstream>
int main()
{
std::vector<int> numbers;
// Read a string from the console
if (std::string line; std::getline(std::cin, line))
{
// Put the complete line into a std::istringstream
std::istringstream iss{line};
std::string word;
while(iss >> word)
{
int x;
/* to the conversion may not work */
try
{
x = std::stoi(std::regex_replace(word, std::regex{ R"([^\d])" }, ""));
}
catch(std::invalid_argument& e)
{
//in case the conversion does not work, we continue as it is not a number
continue;
}
//std::cout << x<<" ";
numbers.push_back(x);
}
}
// display all the extracted numbers
for (int a: numbers)
{
std::cout << a << std::endl;
}
return 0;
}
Here is an example of execution:
5 inch 10 foot and 7y or u8 and U78
5
10
7
8
78