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?
Given a string, you can convert it to an int using a stringstream.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <string>
#include <sstream>
usingnamespace std;
int main()
{
string intAsString = "42";
int asInt = 0;
stringstream ss;
ss<<intAsString;
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.
#include <string>
#include <iostream>
usingnamespace std;
int main()
{
string numerator, denominator;
cout<<"Enter fraction in the form 'int/int'"<<endl;
getline(cin, numerator, '/'); //read up to the '/' and store it in numerator. Discard the '/'
getline(cin, denominator); //read up to the newline and store it in denominator. Discard the '\n'
//if the user entered 47/1236, then
//numerator == "47"
//denominator == "1236"
//use a stringstream here to convert to ints!
return 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).
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?
1 2 3 4 5 6 7 8 9 10
float aFloat;
int anInt;
cout<<"Type in a float"<<endl;
cin>>aFloat;
cin.ignore(1000, '\n'); //remove newline from the stream
cout<<"Now type in an integer"<<endl;
cin>>anInt;
cin.ignore(1000, '\n'); //remove newline from the stream
stringstreams 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).
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
1 2 3 4 5 6
string num1;
string den1;
string num2;
string den2;
int n1=0;
int d1=0;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
case 1:{
cout<<"Addition"<<endl;
getline(cin,num1,'/');
getline(cin,den1); //<-maybe there's a problem in getting the den?
getline(cin,num2,'/');
getline(cin,den2);
stringstream ss;
ss<<num1;
ss>>n1;
ss.str("");
ss<<den1;
ss>>d1;
ss.str("");
cout<<n1+1<<"/"<<d1<<endl; //<-the output of d1 is zero even the input is 5 for example
break;
}
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.