Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Remove the break statement in the else clause. That's causing it to exit the loop.</p> <p>You could really improve this code by reading the conversion factors into an internal data structure, perhaps a dictionary keyed by the "from" conversion unit with the value be a dictionary of possible output units and their conversion factor -- or a custom key/value pair if you only have a single possible output unit. This would turn your inner loop into a two-stage look up (much faster) and save you the trouble of having to re-read the conversion file each time. As @Ben points out, the error message is in the wrong place, too. It needs to be outside the loop/lookup, and only executed if no match was found.</p> <p>Sample code -- note there is no input validation in this code:</p> <pre><code>var conversions = new Dictionary&lt;string,Dictionary&lt;string,double&gt;&gt;(); var convert = new StreamReader("../../convert.txt"); while ((var line = convert.ReadLine()) != null) { string components = line.Split(','); Dictionary&lt;string,double&gt; unitConversions; if (conversions.ContainsKey( components[0] )) { unitConversions = conversions[components[0]]; } else { unitConversions = new Dictionary&lt;string,double&gt;(); conversions.Add( components[0], unitConversions ); } unitConversions.Add( components[1], Convert.ToDouble( components[2] ) ); } while (true) { //ask for the conversion information Console.WriteLine("Enter the conversion in the form (Amount, Convert from, Convert to)"); var inputMeasurement = Console.ReadLine(); var inputMeasurementArray = inputMeasurement.Split(','); bool converted = false; Dictionary&lt;string,double&gt; unitConversion; if (conversions.TryGetValue( inputMeasurementArray[1], out unitConversion )) { double conversionFactor; if (unitConversion.TryGetValue( inputMeasurementArray[2], out conversionFactor )) { converted = true; conversion = Convert.ToDouble( inputMeasurementArray[0] ) * conversionFactor; Console.WriteLine("{0} {1} is {2} {3} \n", inputMeasurementArray[0], inputMeasurementArray[1], conversion, inputMeasurementArray[2]); } } if (!converted) { Console.WriteLine("Please enter two valid conversion types\n"); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload