0

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

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • It depends on the actual requirements. One possible way is to parse one char at a time and testing for digits. Robust but can be longer to code. Another way is to split the line in words from a stringstream, and try to convert the words to integers. Depends on... – Serge Ballesta Mar 16 '21 at 07:08
  • Does this answer your question? [Extract all numbers from stringstream](https://stackoverflow.com/questions/61144980/extract-all-numbers-from-stringstream) – Pat. ANDRIA Mar 16 '21 at 07:16
  • @SergeBallesta In fact, I want to write a program that looks like the example below. input : 3 foot 5 inch ouput: 1 meter 4.1400 cm –  Mar 16 '21 at 07:17
  • You need to parse the input in some way and depending on how much variation you have in the input format you need a more or less complex way to parse it. If the inut is always exactly in the form of your exampl, using a `stringstream` like the answer below suggests is a good option. – super Mar 16 '21 at 07:30
  • @super I have problem with below code. int main() { string str; cin >> str; vector integers = parseInts(str); int inch=integers[0]; int foot=integers[1]; float cm = ((foot)*12)+ inch)*2.54; float meter = cm/100; return 0; } –  Mar 16 '21 at 07:39

3 Answers3

0

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;
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Shadow
  • 37
  • 9
  • it doesn't really help....I want to write a program that looks like the example below. input: 3 foot 5 inch output: 1 meter 4.1400 cm –  Mar 16 '21 at 07:32
  • ok. well, it will be helpful to someone else. i compiled the code, it worked fine. you just needed to implement after you get the values. Did the code not work? – Shadow Mar 16 '21 at 12:30
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;
                     }

                 }
             }
        
         }
     }
 }
ArashV
  • 74
  • 1
  • 10
0

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
Pat. ANDRIA
  • 2,330
  • 1
  • 13
  • 27