### getting integer from a string

how do i extract the string to an integer? ask the user to input fraction but in a form of a string, and i want to get the numerator and denominator in int type so as to perform arithmetic to another fraction.

for example, 13/27

how do i get the 13 as a numerator and 27 as a denominator?
... What is the point of asking for it as a string? Wouldn't it be much easier to ask for them as ints?
Given a string, you can convert it to an int using a stringstream.

 ``12345678910111213141516171819`` ``````#include #include using namespace std; int main() { string intAsString = "42"; int asInt = 0; stringstream ss; ss<>asInt; //convert string into int and store it in "asInt" ss.str(""); //clear the stringstream ss.clear(); //clear error flags because "42" is not a valid stream (?) //here asInt == 42 return 0; }``````

If the user enters a fraction like you say above, you could use `getline` with '/' as the delimiter to extract the numerator, and getline again to extract the denominator.

 ``123456789101112131415161718192021`` ``````#include #include using namespace std; int main() { string numerator, denominator; cout<<"Enter fraction in the form 'int/int'"<
Last edited on
@nano, it's our assigment and it said that the user must enter a string, i wish it's integer :(
@shackstar, can you explain to me the sstream im not familiar with it, thanks! :)
why do i need to initialize the
 ``12`` ``````string intAsString = "42"; int asInt = 0;``````
You don't need to initialize it as "42". That was just an example. You could pass it in numerator as a string (and convert it to an int as in the first example), and then pass it in denominator as a string (and convert it to an int).

Here is a reference for the stringstream object:
http://www.cplusplus.com/reference/iostream/stringstream/

 can you explain to me the sstream im not familiar with it

Do you know how when you do `cin>>` and expect an `int` (or some other data type), it converts the user input to that data type?

 ``12345678910`` ``````float aFloat; int anInt; cout<<"Type in a float"<>aFloat; cin.ignore(1000, '\n'); //remove newline from the stream cout<<"Now type in an integer"<>anInt; cin.ignore(1000, '\n'); //remove newline from the stream ``````

`stringstream`s allow you to use that functionality but without the stream being tied into standard in, or a file stream, etc. Essentially, you pass in the stream as a string to the stringstream using the `<<` operator, and then the stringstream object allows you to play with the stream as if it were `cin` or an `ifstream`, for example. When you're done with using a particular string as the stream, remember to clear the stringstream using the call `.str("")` (see the first example).
Last edited on
there's something wrong with this one, when i check if i really did convert the string to int by using arithmetic to the converted strings the denominator seems not working, it remain as zero

 ``123456`` ``````string num1; string den1; string num2; string den2; int n1=0; int d1=0;``````

 ``1234567891011121314151617`` ``````case 1:{ cout<<"Addition"<>n1; ss.str(""); ss<>d1; ss.str(""); cout<
Last edited on
Apparently, you have to call `ss.clear();` after `ss.str("");`. That's strange because that has worked for me in the past. `ss.clear();` clears the error flags, so maybe just passing it a string without an end of file character causes the stringstream to be in a bad state? I will change my examples above.
Last edited on
 ``12345678910111213141516171819`` ``````case 1:{ cout<<"Addition"<>n1; ss.str(""); ss.clear(); ss<>n1; ss.str(""); ss.clear(); cout<

still doesn't work
Last edited on
It worked for me after I changed the second `ss>>n1;` to `ss>>d1;` ;)
oh yes! it worked! maybe it's a sign that i must go and eat, anyways, thanks a lot! *thumbsup
Topic archived. No new replies allowed.