Conversion of std::string to int

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1260 Views - Last Post: 15 August 2011 - 09:49 AM Rate Topic: -----

#1 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Conversion of std::string to int

Posted 14 August 2011 - 03:05 AM

How can I convert an std::string to an int?

Here is my code . . .

int iChoice;
string sSafetyNetConversion;
getline(cin,sSafetyNetConversion);
if(sSafetyNetConversion=="1"||sSafetyNetConversion=="2")iChoice=sSafetyNetConversion;


Thank you for reading this
Is This A Good Question/Topic? 0
  • +

Replies To: Conversion of std::string to int

#2 xtreampb  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 337
  • Joined: 20-June 07

Re: Conversion of std::string to int

Posted 14 August 2011 - 03:09 AM


iChoce=atoi(sSafetyNetConverions);




www.cplusplus.com has a lot of great resources on the basic types and conversions

~Xtreampb~
Was This Post Helpful? 0
  • +
  • -

#3 MathiasVP  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 154
  • Joined: 08-August 10

Re: Conversion of std::string to int

Posted 14 August 2011 - 03:20 AM

If you're using C++, which I judge by the code you supplied, you should be using stringstreams

Like this:
#include <iostream>
#include <sstream>

int main(int argc, char* argv[])
{
	//Our string
	std::string s = "1234";
	//Create the stringstream object
	std::stringstream ss;
	//Set the string in the stringstream object
	ss.str(s);
	int n;
	//Convert the string from the stringstream to an integer
	ss >> n;
	std::cout << n << std::endl;
	std::cin.get();

	return 0;
}



You can read more on stringstreams here: http://www.cplusplus...m/stringstream/
Was This Post Helpful? 0
  • +
  • -

#4 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: Conversion of std::string to int

Posted 14 August 2011 - 03:24 AM

I had an error . . .
cannot convert 'std::string' to 'const char*' for argument '1' to 'int atoi(const char*)'|
||=== Build finished: 1 errors, 0 warnings ===|
Was This Post Helpful? 0
  • +
  • -

#5 MathiasVP  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 154
  • Joined: 08-August 10

Re: Conversion of std::string to int

Posted 14 August 2011 - 03:35 AM

If you're going with the C way you should do the following:

#include <iostream>

int main(int argc, char* argv[])
{
	//Our string
	std::string s = "1234";
	int n = atoi(s.c_str());
	std::cout << n/2<< std::endl;

	std::cin.get();
	return 0;
}



The string::c_str() method converts a string object to its equalavent c-string

Edit: Grammar fails!

This post has been edited by MathiasVP: 14 August 2011 - 03:37 AM

Was This Post Helpful? 0
  • +
  • -

#6 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: Conversion of std::string to int

Posted 14 August 2011 - 03:38 AM

@MathiasVP, That did not work for me. Maybe I made an error but a portion of my code looks like this:
string sSafetyNetConversion;
getline(cin,sSafetyNetConversion);
stringstream ss;
ss.str(sSafetyNetConversion);
if(sSafetyNetConversion=="1"||sSafetyNetConversion=="2")iChoice>>ss;

Was This Post Helpful? 0
  • +
  • -

#7 MathiasVP  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 154
  • Joined: 08-August 10

Re: Conversion of std::string to int

Posted 14 August 2011 - 03:41 AM

You've nearly got it right. But the error is here:

iChoice>>ss;



Should be:

ss >> iChoice



Try that ;)
Was This Post Helpful? 1
  • +
  • -

#8 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: Conversion of std::string to int

Posted 14 August 2011 - 03:45 AM

Heheh >.< Thanks . . . It works :) Now I can get input for an integer without the program spas'ing out whenever the user enters a symbol/letter :)
Was This Post Helpful? 0
  • +
  • -

#9 MathiasVP  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 154
  • Joined: 08-August 10

Re: Conversion of std::string to int

Posted 14 August 2011 - 03:48 AM

Glad you got it working :P

Happy coding!
Was This Post Helpful? 0
  • +
  • -

#10 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Conversion of std::string to int

Posted 14 August 2011 - 04:05 AM

Think of string streams like cout and cin. When you want to add to the stream (print to the screen), use <<. When you want to read from the stream (read from the screen) use >>.
Was This Post Helpful? 0
  • +
  • -

#11 xtreampb  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 337
  • Joined: 20-June 07

Re: Conversion of std::string to int

Posted 14 August 2011 - 04:48 AM

View PostPlasticineGuy, on 14 August 2011 - 07:05 AM, said:

Think of string streams like cout and cin. When you want to add to the stream (print to the screen), use <<. When you want to read from the stream (read from the screen) use >>.


and if you need a c style string because some functions and other libraries can only convert c style strings (such as wxWidgets) you can use ss.cstr() (assuming that ss is my stringstream type)
Was This Post Helpful? 0
  • +
  • -

#12 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,760
  • Joined: 16-October 07

Re: Conversion of std::string to int

Posted 14 August 2011 - 05:30 AM

I'd prefer the string stream. A simple wrapper can make it easy to use:
bool parse(const std::string &s, int &n) {
	std::stringstream ss(s);
	return (ss>>n);
}



If you want to get fancy, you can make that more general purpose:
template<typename T>
bool parse(const std::string &s, T &n) {
	std::stringstream ss(s);
	return (ss>>n);
}



In C you'd use sscanf in a similar way. Indeed, you probably see scanf used more than atoi.

For your code, I'd go something like:

int iChoice;
string sSafetyNetConversion;
getline(cin,sSafetyNetConversion);
if(parse(sSafetyNetConversion, iChoice)) {
	// now check the int
	if (iChoice==1||iChoice==2) { /* */ }
} else {
	/* bad data, you decide */
}



Or, perhaps better...
int parseOrDefault(const std::string &s, int n) {
	std::stringstream ss(s);
	ss>>n;
	return n;
}

string sSafetyNetConversion;
getline(cin, sSafetyNetConversion);
int iChoice = parseOrDefault(sSafetyNetConversion, -1);


Was This Post Helpful? 1
  • +
  • -

#13 Ricky65  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 115
  • Joined: 03-June 10

Re: Conversion of std::string to int

Posted 14 August 2011 - 09:39 AM

I'm not a fan of stringstreams as they can be unacceptably slow in some instances, although it wouldn't be a problem here.

If you have a C++0x compiler the useful stoi function is probably the easiest method. It's the C++ std::string equivalent of the C function atoi.

Your example would be as simple as:

int iChoice;
string sSafetyNetConversion;
getline(cin,sSafetyNetConversion);
if(sSafetyNetConversion=="1"||sSafetyNetConversion=="2")
iChoice = stoi(sSafetyNetConversion);


This post has been edited by Ricky65: 14 August 2011 - 09:53 AM

Was This Post Helpful? 1
  • +
  • -

#14 alke4  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 19
  • Joined: 26-May 10

Re: Conversion of std::string to int

Posted 14 August 2011 - 12:38 PM

Another way would be to use the Boost Library and simply use Lexical Cast to convert.
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <string>

using namespace std;

int main() {
  string s = "123456"
  int n = boost::lexical_cast<int> (s);

  cout << n << endl;

  cin.get();

  return 0;
}


You will have to install the Boost Libraries of course. Right here.

This post has been edited by alke4: 14 August 2011 - 12:41 PM

Was This Post Helpful? 1
  • +
  • -

#15 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: Conversion of std::string to int

Posted 15 August 2011 - 01:51 AM

Wow, where can I get a C++0x compiler? Does Code::Blocks use C++0x? I use CodeBlocks you see :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2