Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

String to Int


  • Please log in to reply
10 replies to this topic

#1 sausage

sausage

  • Members
  • 389 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Colorado
  • Local time:04:49 AM

Posted 22 March 2009 - 04:39 PM

How do I get this function to work with a single number?

int GetIntVal(string strConvert, int location) { 
  int intReturn; 
 
  intReturn = atoi(strConvert.c_str()); 

  return(intReturn); 
}

I don't want it to read the whole string I just want it to read one part of the string

I am building an equation solver

string equation;
	 cout << "Enter your equation, one variable (x(lowercase)), no exponents: ";
	 cin >> equation;
	 for(int i = 0; i < equation.size(); ++i)
	 {
			 if(equation[i] == 'x')
			 {
						  int coef = GetIntVal(equation, i - 1);
						  cout << coef;
			 }
	 }

ANd I want it to read the coefficient right before the x but when I change the atoi function, it says c_str is not defined (I have no idea what the function does though)

Edited by sausage, 22 March 2009 - 04:39 PM.


BC AdBot (Login to Remove)

 


#2 sausage

sausage
  • Topic Starter

  • Members
  • 389 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Colorado
  • Local time:04:49 AM

Posted 22 March 2009 - 04:40 PM

Accidental Double Post deleted

Edited by sausage, 22 March 2009 - 04:40 PM.


#3 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:03:49 AM

Posted 22 March 2009 - 05:58 PM

Hello :thumbsup:

Here's one way to approach it:

Make a string with just the one number:
char temp[2];
copy the single number you want in there:
temp[0] = strConvert[location];
Null terminate:
temp[1] = NULL;
Call atoi:

return atoi(temp);

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#4 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:03:49 AM

Posted 22 March 2009 - 05:59 PM

I change the atoi function, it says c_str is not defined (I have no idea what the function does though)

Which function don't you understand, atoi or c_str?

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#5 sausage

sausage
  • Topic Starter

  • Members
  • 389 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Colorado
  • Local time:04:49 AM

Posted 22 March 2009 - 06:04 PM

I change the atoi function, it says c_str is not defined (I have no idea what the function does though)

Which function don't you understand, atoi or c_str?

Billy3


both.......

#6 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:03:49 AM

Posted 22 March 2009 - 06:09 PM

Alright... what is a string typically in C (not C++)?

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#7 sausage

sausage
  • Topic Starter

  • Members
  • 389 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Colorado
  • Local time:04:49 AM

Posted 22 March 2009 - 06:10 PM

its an array of characters... why?

#8 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:03:49 AM

Posted 22 March 2009 - 06:25 PM

Because that character array is what std::basic_string returns when you call c_str(). It's a way of getting a C-style string out of a std::basic_string object. (Note that std::string is std::basic_string<char>. std::wstring is std::basic_string<wchar_t> -- since it's a template class it works the same way for both plain characters and wide characters)

atoi turns a character array which is a string of numbers, and converts it to an integer representation. http://www.cplusplus.com/reference/clibrar...tdlib/atoi.html

If you can use the boost conversions library, you can accomplish the same thing without messing with c-style character arrays at all using lexical_cast. For example:

return boost::lexical_cast<unsigned int, std::string>(strConvert.substr(location,1));

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#9 sausage

sausage
  • Topic Starter

  • Members
  • 389 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Colorado
  • Local time:04:49 AM

Posted 22 March 2009 - 08:04 PM

Ok... the top part doesn't make much sense but I understand atoi now

but how would I right an atoi function using an exact position inside the string?

#10 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:03:49 AM

Posted 22 March 2009 - 08:49 PM

but how would I right an atoi function using an exact position inside the string?

Instead of pointing atoi at the existing string, which wont do what you want, create a new string containing the number you want.

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#11 sausage

sausage
  • Topic Starter

  • Members
  • 389 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Colorado
  • Local time:04:49 AM

Posted 22 March 2009 - 10:34 PM

yeah that works, thanks :-)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users