### String to variables

If I have a string such as "insert 56" how would I store insert into a separate string and 56 into an integer ?

Thanks.
Use a std::istringstream.
I'm not sure this is really a windows programming question, it might fit better in beginners section.

I'm also not sure how to use std::istringstream but I would do something like this with my beginner knowledge.

*Basically read the character from the string.
*Cast the character to an int
*Subtract 48 because that's where the ascii 0 starts.
*Multiply by 10^i for it's place in the number string
*then add the result to the total.

 ``1234567891011121314151617181920212223242526272829303132`` ``````#include #include int main(){ char number_string[]={"123456"}; char number_string_element; int total = 0; int Ten_Multiples; for (int i = 0; i < sizeof(number_string)-1; i++){ number_string_element = number_string[i];//read a digit from number string Ten_Multiples = 1;//reset for loop //Calculate significant digit for (int j = 1; j < (sizeof(number_string) - i - 1); j++){ Ten_Multiples = Ten_Multiples * 10; } total = total + ((int)number_string_element - 48) * Ten_Multiples; std::cout << "Total after Step " << i << ": " << total << "\n"; } std::cout << "\n\nFinal Total: " << total; total++; std::cout << "\n\nIncremented Total: " << total << "\n"; return 0; }``````
Last edited on
Garion, see http://en.cppreference.com/w/cpp/string/basic_string/stol

OP, as LB indicates, a solution using std::istringstream is viable:

 ``1234567891011121314151617`` ``````#include #include #include int main() { const char* text = "insert 56"; std::istringstream stream(text); std::string command; int value; stream >> command >> value; std::cout << "Command: \"" << command << "\"\n"; std::cout << "Value: \"" << value << "\"\n"; }`````` ```Command: "insert" Value: "56"```
Thanks guys,

What I actually found as a much simpler solution is to take in both arguments as strings

 ``123`` ``````string command, value cin >> command >> value ;``````

then used atoi to convert the value string to an integer.

atoi is the C equivalent of std::stoi referenced at the link I pointed Garion to, but yes, it should work.

Of course, if you can count on the input being correct, you could as well do:

 ``123`` `````` string command ; int value ; cin >> command >> value;``````

and forgo std::stoi or atoi altogether.
Joshua Schweigert wrote:
What I actually found as a much simpler solution is to take in both arguments as strings
You and I have very different ideas of "much simpler" ;)
Hmm, I tried to use a few STOI and STOL's but it doesn't seem to work on mingw.

I guess that's a bug.

The more i read about mingw the more convinced I become that I should switch to something else. It might be time to look for a new C++ compiler.

After lunch though :)
Last edited on
That's weird... I'm using mingw as well and it compiles using stoi. Gcc in Linux is my favorite so far for a compiler though.

Cire, I was going to use that... But I needed to call different functions based on the string input and not all of them required a value integer.

LB what would you consider simpler than what I did?
I think the GCC version for linux and the GCC version for windows (called mingw) is different.

I don't think Mingw has official development or versioning so it's hard to tell if you are using the latest version. Although it has been about a month since I updated my mingw so I could give that a shot I suppose.
Last edited on
I don't know where you get your information from. Try running g++ --version on your MinGW installation.
Already did that, still not working.

I did manage to find this on the GCC bug tracker.

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59076

More bickering about why it can't be fixed here also.

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52015

Also there seems to be an unofficial patch though I'm reluctant to download anything from a website called "Tehsausage" (spelling is correct)

http://tehsausage.com/mingw-to-string

For mingw I don't think there is any versioning numbers. You can't find a version number on the downloads from mingw.org. I can't find a version number anywhere on my install. Everything I've read on the mingw faq says mingw is based on specific versions of other linux programs but not that it is the same (nor would it make sense for it to be the same because they are different operating systems.)
Last edited on
If you run g++ --version and it displays an error or nothing at all then you dont have it installed correctly. I am able to run stoi fine on the latest mingw. And gcc and mingw arent that much different considering mingw is a port of gcc to windows.
I was also wondering,
How would I have the user input a function name and an argument ( insert 56 ) but also be able to input just print or a single function with no argument and be able to call the function ?

Such as

Input:
 insert 56

then run the insert command

Input:
 print

then run the print command

I just dont know how I can have input with one or the other. Because
`cin>>function>>argument`
will require an argument input so if I just wanted to run print I would have to input a second argument.
Any ideas?
Hmm. I don't have it installed incorrectly and I would point out that this isn't an issue that only I have had.

I'm not posting this to argue with Joshua Schweigert or anybody else but rather to provide information to anybody else who might view this thread. Although I would point out that it isn't exactly helpful to say "It works for me so it must work for everybody". At least provide information on which version you are using or where you downloaded your version if you are trying to help.

Here is what I've found by searching; if you want to use STOI, STOL, etc you must use the latest unreleased version of mingw version 4.8.1. At least I think it's unreleased as I can't find any version numbers of anything on mingw.org

There isn't even a mention of where to find latest builds or anything else for that matter.

I almost wonder if mingw.org is the official mingw site because it doesn't appear to ever get updated.

This site appears more helpful and I'll give this version a shot. Although I only see version 4.8.0 on there and not 4.8.1

http://mingw-w64.sourceforge.net/

Also Nuwen Mingw has precompiled binaries of version 4.8.2 and it works with STOI / STOL

http://nuwen.net/mingw.html
Last edited on
As I said in my previous post I installed the latest version from the download link on mingw.org. So whichever that version is thats what I'm running and stoi runs perfectly fine on it.
You never said where you got your version of mingw from. I see mingw version 4.7.2 download links off the mingw.org site after searching around the site for a while.

That site could be better organized to make information easier to find.