CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2016
    Posts
    32

    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;
    }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,923

    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/
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Nov 2016
    Posts
    32

    Re: Convert any input of length measurement into meters C++

    Quote Originally Posted by 2kaud View Post
    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/
    Do have any tips on how to convert size_t to double? Thank you.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,923

    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/
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Nov 2016
    Posts
    32

    Re: Convert any input of length measurement into meters C++

    Quote Originally Posted by 2kaud View Post
    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured