BleepingComputer.com: String to Int

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

String to Int C++

#1 User is offline   sausage 

  • Forum Regular
  • PipPipPip
  • Find Topics
  • Group: Members
  • Posts: 317
  • Joined: 01-November 08
  • Gender:Male
  • Location:Louisville Colorado

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)

This post has been edited by sausage: 22 March 2009 - 04:39 PM

If I'm posting, I probably have something horribly wrong with my computer, there's no obvious explanation for it, that's just the way it is.

#2 User is offline   sausage 

  • Forum Regular
  • PipPipPip
  • Find Topics
  • Group: Members
  • Posts: 317
  • Joined: 01-November 08
  • Gender:Male
  • Location:Louisville Colorado

Posted 22 March 2009 - 04:40 PM

Accidental Double Post deleted

This post has been edited by sausage: 22 March 2009 - 04:40 PM

If I'm posting, I probably have something horribly wrong with my computer, there's no obvious explanation for it, that's just the way it is.

#3 User is offline   Billy O'Neal 

  • Bleepin Engineer GRADUATE
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Malware Response Instructor
  • Posts: 10,403
  • Joined: 17-January 08
  • Gender:Male
  • Location:Cleveland, Ohio

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

#4 User is offline   Billy O'Neal 

  • Bleepin Engineer GRADUATE
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Malware Response Instructor
  • Posts: 10,403
  • Joined: 17-January 08
  • Gender:Male
  • Location:Cleveland, Ohio

Posted 22 March 2009 - 05:59 PM

Quote

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

#5 User is offline   sausage 

  • Forum Regular
  • PipPipPip
  • Find Topics
  • Group: Members
  • Posts: 317
  • Joined: 01-November 08
  • Gender:Male
  • Location:Louisville Colorado

Posted 22 March 2009 - 06:04 PM

View PostBilly O, on Mar 22 2009, 04:59 PM, said:

Quote

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.......
If I'm posting, I probably have something horribly wrong with my computer, there's no obvious explanation for it, that's just the way it is.

#6 User is offline   Billy O'Neal 

  • Bleepin Engineer GRADUATE
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Malware Response Instructor
  • Posts: 10,403
  • Joined: 17-January 08
  • Gender:Male
  • Location:Cleveland, Ohio

Posted 22 March 2009 - 06:09 PM


#7 User is offline   sausage 

  • Forum Regular
  • PipPipPip
  • Find Topics
  • Group: Members
  • Posts: 317
  • Joined: 01-November 08
  • Gender:Male
  • Location:Louisville Colorado

Posted 22 March 2009 - 06:10 PM

its an array of characters... why?
If I'm posting, I probably have something horribly wrong with my computer, there's no obvious explanation for it, that's just the way it is.

#8 User is offline   Billy O'Neal 

  • Bleepin Engineer GRADUATE
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Malware Response Instructor
  • Posts: 10,403
  • Joined: 17-January 08
  • Gender:Male
  • Location:Cleveland, Ohio

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

#9 User is offline   sausage 

  • Forum Regular
  • PipPipPip
  • Find Topics
  • Group: Members
  • Posts: 317
  • Joined: 01-November 08
  • Gender:Male
  • Location:Louisville Colorado

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?
If I'm posting, I probably have something horribly wrong with my computer, there's no obvious explanation for it, that's just the way it is.

#10 User is offline   Billy O'Neal 

  • Bleepin Engineer GRADUATE
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Malware Response Instructor
  • Posts: 10,403
  • Joined: 17-January 08
  • Gender:Male
  • Location:Cleveland, Ohio

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

#11 User is offline   sausage 

  • Forum Regular
  • PipPipPip
  • Find Topics
  • Group: Members
  • Posts: 317
  • Joined: 01-November 08
  • Gender:Male
  • Location:Louisville Colorado

Posted 22 March 2009 - 10:34 PM

yeah that works, thanks :-)
If I'm posting, I probably have something horribly wrong with my computer, there's no obvious explanation for it, that's just the way it is.

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users