I have read a lot of topics with these subject many of theme where on these forum but what ever i try i cant get this working.

I have tried 4 or 5 function for converting string to float but i always get compiler error left from xxx must be class/struct ....

here is the function

void calculate(string& part_){
	
	float number_one=0;
	float number_two=0;

	for(int i=0; i<4; ++i){
		for (int j=0; j<part_.length(); ++j){
			if(part_[j]== '/'){
			
				number_one = part_.at(j-1); // need to be converted to float somehow
				number_two = part_[j+1]; // these too
				number_one /= number_two; 

				part_[j-1] = number_one;
				part_.erase(j, j+2);

				
			
			}
		}
	}
}

Edited 6 Years Ago by Jeronim: n/a

Can you give us an example of what a typical part_ would look like? Is it just like "400.0/20.0"?

Edited 6 Years Ago by jonsca: n/a

Can you give us an example of what a typical part_ would look like? Is it just like "400.0/20.0"?

part_=3+3/2*5

My first thought would have been to use a stringstream and substrings, but this is more complicated. You need to use string.find() and it's variants to get all the operations in your sentences and break them down using the rules of precedence.

Otherwise if you are just looking to do this for the 3/2 portion you could do something like this:

(after isolating from the + and * signs)
index = input.find("/");
float three;
	if (index !=string::npos)
	{
		ss<<input.substr(0,index);
		ss>>one;
		ss.clear();
		ss<<input.substr(index+1);
		ss>>two;
	
		three = one/two;
	}

Edited 6 Years Ago by jonsca: n/a

now i'm getting compiler error

error C2676: binary '>>' : 'std::string' does not define this operator or a conversion to a type acceptable to the predefined operator

Here are a couple of functions I designed to convert between numeric types and strings

/** Converts a number to a string
  * @param  number - The number to convert
  * @param  prec   - Floating point precision to use
  * @param  w      - Width of the string
  * @return A string representation of the number
  */
template <class T>
std::string num2str( T number, int prec=2, int w=0 ){
    std::ostringstream ss;
    ss.setf( std::ios::showpoint | std::ios::fixed );
    ss.precision( prec );
    ss.fill( ' ' );
    ss.width( w );
    ss << number;
    return ss.str();
}

/** Converts a string to a number
  * @param  str    - The string to convert
  * @param  number - The storage for the number
  */
template <class T>
void str2num( std::string str, T &number ){
    std::istringstream ss( str );
    ss >> number;
}

So to convert a string to a float:

float f;
string s="1234.5678";
str2num( s, f );  // Now f should equal 1234.5678

Since these are templated they should work for all standard numeric types.

Note the prec and w argument in num2str. The prec argument will control precision ( # of decimal places shown) for doubles and floats. The w argument pads the number out with spaces to a desired width.

This article has been dead for over six months. Start a new discussion instead.