help with a fill in the code exercise

I've been trying to figure this "finish the code" section on this online book i am doing. and for some reason i am at a lost. the program allows you to convert units given from a user to another unit also given from the user with a value applied. so for example a user will say he wants to convert from cm to inches and gives a value of say 10 and the program will output 10 cm = 3.93701 in.

the code they give to complete is as follows:

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
 #include <iostream>
#include <string>
using namespace std;

int main()
{
   bool done = false;
   string unit1 = "";
   string unit2 = "";
   double factor1 = 0; // conversion factor from first unit to cm
   double factor2 = 0; // conversion factor from cm to second unit
   
   while (!done)
   {
      bool again = false; // true to repeat the same conversion
      cout << "From unit (in, cm, m, again, quit): " << endl;
      string command;
      cin >> command;
      if (command == "in")
      {
         factor1 = 2.54; 
         unit1 = command;
      }
      else if (command == "cm")
      {
         . . .
      }
      else if (command == "m")
      {
         . . .
      }
      else if (command == "again")
      {
         again = true;
      }
      else if (command == "quit")
      {
         done = true;
      }
      else
      {
         cout << "Sorry, unknown unit." << endl;
      }
      
      if (factor1 != 0 && !again && !done)
      {
         cout << "To unit: " << endl;
         cin >> unit2;
         if (unit2 == "in")
         {
            factor2 = 1.0 / 2.54;
         }
         else if (unit2 == "m")
         {
            . . .
         }
         else if (. . .)
         {
         }
         else
         {
            cout << "Sorry, unknown unit." << endl;
         }
      }
      
      if (. . .) 
      {
         // Read value to be converted
         double value;
         cin >> value;
         // Convert and print result
         cout << value << " " << unit1 << " = " 
            << value * factor1 * factor2 << " " << unit2 << endl;
      }
   }

   return 0;
}



what i filled into the code is as follows:
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <string>
using namespace std;

int main()
{
    bool done = false;
    string unit1 = "";
    string unit2 = "";
    double factor1 = 0; // conversion factor from first unit to cm
    double factor2 = 0; // conversion factor from cm to second unit

    while (!done)
    {
        bool again = false; // true to repeat the same conversion
        cout << "From unit (in, cm, m, again, quit): " << endl;
        string command;
        cin >> command;
        cout << command;
        if (command == "in")
        {
            factor1 = 2.54;
            unit1 = command;
        }
        else if (command == "cm")
        {
            factor1 = 1;
            unit1 = command;
        }
        else if (command == "m")
        {
            factor1 = 100;
            unit1 = command;

        }
        else if (command == "again")
        {
            again = true;
        }
        else if (command == "quit")
        {
            done = true;
        }
        else
        {
            cout << "Sorry, unknown unit." << endl;
        }

        if (factor1 != 0 && !again && !done)
        {
            cout << "To unit: " << endl;
            cin >> unit2;
            cout << unit2;
            if (unit2 == "in")
            {
                factor2 = 1.0 / 2.54;
            }
            else if (unit2 == "m")
            {
                factor2 = .01;
            }
            else if (unit2 == "cm")
            {
                factor2 = 1;
            }
            else
            {
                cout << "Sorry, unknown unit." << endl;
            }
        }
        int i = 1;

        if (i > 0)
        {
            i--;
            // Read value to be converted
            double value;
            cin >> value;
            // Convert and print result
            cout << value << " " << unit1 << " = "
                << value * factor1 * factor2 << " " << unit2 << endl;
        }
        else
        {
            done = true;
        }
    }

    return 0;
} 
Last edited on
Do you have a question? It isn't clear what you're asking.
What's the issue?

Line 53 outputs what the user just inputted, so it appears twice without reason.

Moreover, you should output a statement telling the user to input a value before line 78, otherwise the user is just staring at the console and has to figure it out for themselves.
Be sure to change only what is what's marked with ". . . " in the original. So you should delete lines 71 and 83-86.

The if at your line 73 should check whether you have two valid units. You can do that by checking whether both factor1 and factor2 are non-zero.

Hmm. Actually the code has bugs that require changes other than what's in ". . ." If you go through the loop a second time and enter an invalid unit, factor1 will still contain the non-zero value from the first time through the loop.

To fix this, factor1 and factor2 should be set to zero inside the while loop:
1
2
3
4
5
    while (!done)
    {
        double factor1 = 0; // conversion factor from first unit to cm
        double factor2 = 0; // conversion factor from cm to second unit
        bool again = false; // true to repeat the same conversion 
Topic archived. No new replies allowed.