Converting string fractions to int
uhm, Im trying to convert a Fraction in the form of a string to int but it says:
'Addition' : cannot convert parameter 1 from 'std::string' to 'int'
1 2 3 4 5 6 7 8 9 10 11 12
|
case '+':
{ stringstream ss;
ss<<numerator;
ss>>n;
ss.str("");
ss<<denominator;
ss>>d;
ss.str("");
cout<<n+1<<"/"<<d<<endl;
Addition(numerator,denominator);
break;
}
|
What types do n and d have?
What does the function Addition
do? Can you post it?
Why not simply do
1 2 3 4 5
|
ostringstream num(numerator);
num >> n;
ostringstream den(denominator);
den >> d;
cout << n/d << endl; //since you wanted int
|
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#include <iostream>
#include <sstream>
#include <string>
int main()
{
std::string numerator = "10";
std::string denominator = "3";
std::istringstream num(numerator);
int n;
num >> n;
std::istringstream den(denominator);
int d;
den >> d;
std::cout << n/d << std::endl;
return 0;
}
|
3 |
Topic archived. No new replies allowed.