Convert any input of length measurement into meters C++
Given:
1 foot = 0.305 m
1 in = 0.0254m.
I am attempting to convert the input of length measurements into meters.
For example: 5'1'' = 5*0.305 + 1 * 0.0254 = 1.5504m
3.4m = 3.4m
5'1.23456" = 5*0.305 + 1.23456*0.0254 = 1.5563
But I have got quite a few errors. I don't know how to fix them but here is my logics on how to approach this puzzle.
I wrote some if statements to take care of:
1) If I input only, say 3.4, it will print the whole string and convert it into double 3.4.
2) If I input, say 5'1" without no "."
)If I input, say 5'1.23" with " . ",
But I failed. I need your help to point out the problems of my code. Thanks in advance!
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <sstream>
using namespace std;
double convert_String(string&);
int main()
{
string s;
getline(cin,s);
convert_String(s);
return 0;
}
double convert_String(string& s)
{
string s1,s2;
int y1, y2,y3;
double d1,d2,d3;
int l = s.length();
y1 = s.find("."); // find the position of the "." and then assign the position value to y1
y2 = s.find("'"); // find the position of the " ' " and then assign the position value to y
y3 = s.find('"'); // find the position of the " " " and then assign the position value to y2
if ((y1 < 0 && y2 < y1 && y3 == l) || (y2 < 0 && y3 < 0 && y1 == -1))
{
s1 = s.substr(0,y2-1);
d1 = stod(s1);
d1 = d1*0.305;
s2 = s.substr(y2+1,l-1);
d2 = stod(s2);
d2 = d2*0.0254;
}
else if(y2 == -1 && y3 == -1 && y1 >= 0) //there is no " ' " and " " "
{
d3 = stod(s);
}
cout << d1 << endl;
return d1 + d2;
}
Re: Convert any input of length measurement into meters C++
If no match is found, .find() returns value string::npos (not -1) which is what the return value needs to be tested against. Also the return type of .find() is size_t and not int. See http://www.cplusplus.com/reference/string/string/find/
Re: Convert any input of length measurement into meters C++
Quote:
Originally Posted by
2kaud
Do have any tips on how to convert size_t to double? Thank you.
Re: Convert any input of length measurement into meters C++
Why do you want to convert size_t to double? Only y1, y2, y3 (and l) need to be of type size_t. d1, d2 & d3 stay as type double. Consider
Code:
double d1 = 0.0, d2 = 0.0, d3 = 0.0;
size_t l = s.length();
size_t y1 = s.find(".");
size_t y2 = s.find("'");
size_t y3 = s.find('"');
...
else if (y2 == string::npos && y3 == string::npos && y1 >= 0)
d3 = stod(s);
Note that with stod(), if a conversion cannot be performed then an exception will be raised which should be handled within the code. See http://www.cplusplus.com/reference/string/stod/
Re: Convert any input of length measurement into meters C++
Quote:
Originally Posted by
2kaud
Why do you want to convert size_t to double? Only y1, y2, y3 (and l) need to be of type size_t. d1, d2 & d3 stay as type double. Consider
Code:
double d1 = 0.0, d2 = 0.0, d3 = 0.0;
size_t l = s.length();
size_t y1 = s.find(".");
size_t y2 = s.find("'");
size_t y3 = s.find('"');
...
else if (y2 == string::npos && y3 == string::npos && y1 >= 0)
d3 = stod(s);
Note that with stod(), if a conversion cannot be performed then an exception will be raised which should be handled within the code. See
http://www.cplusplus.com/reference/string/stod/
Thank you so much. I got my code to work now. I appreciate your help and your time.