Find which distance is bigger out of two inputs

Solved. Thanks everyone!
Last edited on
You haven't still bother to indent correctly your sourcecode at here. And you haven't made any commend to your pieces. That's really bad coding style! So you expect that someone will dig through your code?
What is indenting properly? I feel like it looks normal to me. Also, what do you mean any commend?

I know the problem exists in my double strToDouble(string s) function and deals with a period messing it up. I'm not sure what to do
ninjachachi

Your indenting is inconsistent, which makes it very difficult to follow the code's logical structure. Things in the same block of code should essentially be at the same indentation.

I haven't gone in depth through your strToDouble(string s) function, but I would encourage you to consider a different logical approach. As far as I can see, your string has to end in EITHER
" OR
m

So start by looking at the s.length()-1 element (assuming there are no blanks at the end - trim them off if so; in fact it might be a good idea to remove ANY spaces first).

You now know whether it is in metric or imperial measurement. Take the last element off the string.

If it is metric, then the rest of the string can be converted to a double (e.g. with a string conversion function, or use of stringstream). That is then done.

If it is imperial, then the truncated string will be of form
a'b
This string can be SPLIT and a and b can separately be converted to the equivalent doubles. If these are A and B then the total length in metres is ( A + B/12.0 ) * 0.305

There isn't any code here, but perhaps the simpler approach may lead to less coding errors.
The logic for strToDouble() is hard to follow, which makes it hard to debug. I'd recommend simplifying it by breaking it into smaller pieces with clear, specific purposes.

Does the assignment require strToDouble() identify the format AND convert meters to feet/inches AND split up feet inches? If not, write smaller helper functions to divvy up the tasks.

For example, strToDouble() could call other functions that
1) scan for 'm'
2) extract meters from string
3) convert meters to ft/in
4) extract ft/in from string

Each one of those functions will be easier to debug than all of them combined into a single for loop.

As a side note, what does the "magic" variable in Distance refer to? Is it the distance in inches? Is it the distance in meters? Does it change depending on circumstances?

I propose that it's easier to store the Distance value in meters regardless of the format provided. It'll require that you convert feet/in, but once it's converted comparisons become very simple.
Hello ninjachachi,

I have not tried this yet, but my thought is in the if statements starting at line 29.
Move line 33 to line 29 and make it just if and move line 29 to line 33 and make it an else if and I think it should work better.

Hope that helps,

Andy
ninjachachi

The following version of strToDouble() will convert your string into a double (the distance in metres). Note the relevant headers.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;


double strToDouble( string s )
{
   int pos;
   double feet, inch;
   string feetString, inchString;

   int last = s.size() - 1;       
   if ( s[last] == 'm' )          // metric measurement
   {
      s.erase(last);
      return atof( s.c_str() );
   }
   else if ( s[last] == '"' )     // imperial measurement
   {
      pos = s.find('\'');
      feetString = s.substr( 0, pos );   feet = atof( feetString.c_str() );
      inchString = s.substr( pos+1  );   inch = atof( inchString.c_str() );
      return ( feet + inch / 12.0 ) * 0.305;
   }
   else
   {
      cout << "I don't recognise the string" << endl;
      return 0.0;
   }
}
Everyone thanks for the help! I got it to work taking all of your advice, it was similar to lastchance's code!
Topic archived. No new replies allowed.