converting string to int

Hallo,

I am new in C++. I want to convert string to ing. I got string bpm60 or bpm120. I want to read only 60 and 120 as int.

I though of using this:

as an example:
bpm.erase(remove(bpm.begin());

Or crating a function to filter or remove "bpm" from my string and keeping the int number only. I need the values to use it later in my code?
Or crating a function to filter or remove "bpm" from my string and keeping the int number only
This is the best approach for you.
try a for loop, reading each char and checking if its between 0x30(ascii 0) and 0x39 (ascii 9)
if it is then keep it, if not, ignore it...

just pay attention to whether its ascii or unicode
Last edited on
Hallo,

#include <iostream> // std::cout
#include <string> // std::string, std::stoi

float duration;

int main ()
{
std::string str_dec = "60bpm";

std::string::size_type sz; // alias of size_t

float i_dec = std::stoi (str_dec,&sz);

std::cout << i_dec << "\n";

duration = 1/i_dec;

printf ("%1.6f \n", duration);

//return 0;
}

I have this code it is working but if I change the string from 60pbm to bpm60 does not work. My input string is bpm60? Can any one advice how to get it or what is the be way to have the value of the int (60) only if I have pbm60???
Last edited on
Are you trying to make your code work for both 60bpm and bpm60 or just bpm60?

In either case you can probably use string::find() to locate where "bpm" is located in the string. If it is located starting at the first element you can use the substr() function to retrieve the numeric part of the string to be converted. If it is not at the start of the string then you should be able to use either a stringstream to process the string or stox().

Topic archived. No new replies allowed.