Help Needed with Drill from Book

As the title says, I need some help with a Drill from the book I'm learning C++ from, Bjarne Stroustrup's Programming: Principles and Practice Using C++ Second Edition.

These are the Drill questions (I'm on numbers 7 and 8 right now):
1. Write a program that consists of a while-loop that (each time around the loop) reads in two ints and then prints them. Exit the program when a terminating '|' is entered.

2. Change the program to write out the smaller value is: followed by the smaller of the numbers and the larger value is: followed by the larger value.

3. Augment the program so that it writes the line the numbers are equal (only) if they are equal.

4. Change the program so that it uses doubles instead of ints.

5. Change the program so that it writes out the numbers are almost equal after writing out which is the larger and the smaller if the two numbers differ by less than 1.0/100.

6. Now change the body of the loop so that it reads just one double each time around. Define two variables to keep track of which is the smallest and which is the largest value you have seen so far. Each time through the loop write out the value entered. If it’s the smallest so far, write the smallest so far after the number. If it is the largest so far, write the largest so far after the number.

7. Add a unit to each double entered; that is, enter values such as 10cm, 2.5in, 5ft, or 3.33m. Accept the four units: cm, m, in, ft. Assume conversion factors 1m == 100cm, 1in == 2.54cm, 1ft == 12in. Read the unit indicator into a string. You may consider 12 m (with a space between the number and the unit) equivalent to 12m (without a space).

8. Reject values without units or with “illegal” representations of units, such as y, yard, meter, km, and gallons.

9. Keep track of the sum of values entered (as well as the smallest and the largest) and the number of values entered. When the loop ends, print the smallest, the largest, the number of values, and the sum of values. Note that to keep the sum, you have to decide on a unit to use for that sum; use meters.

10. Keep all the values entered (converted into meters) in a vector. At the end, write out those values.

11. Before writing out the values from the vector, sort them (that’ll make them come out in increasing order).


I need help with figuring out the logic first, and then the syntax, for doing numbers 7 and 8, for right now. I'll ask for help on the others as I need to later.

Just in case, I'll also post the code I've written for this Drill so far:
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
#include <iostream>
#include <vector>
#include <cmath>
#include <limits>
#include <algorithm>

using namespace std;

void keep_window_open();

int main()
{
    double larger;
    double smaller;
    double number = 0;
    vector<double> user_input;
    string distance_input;
    vector<string> distance;
    cout << "Keep entering numbers; to exit the program, just enter something that isn't a number.\n";
    while (cin >> number)
    {
        user_input.push_back(number);
        for (unsigned i = 0; i < user_input.size(); i++)
        {
            smaller = std::numeric_limits<double>::min();
            larger = std::numeric_limits<double>::max();
            if (std::abs(user_input[i]) < smaller)
            {
                sort(user_input.begin(), user_input.end());
                smaller = std::abs(user_input[i]);
                cout << smaller << ": the smallest so far\n";
            }
            else if (std::abs(user_input[i]) > smaller)
            {
                sort(user_input.begin(), user_input.end());
                larger = std::abs(user_input[i]);
                cout << larger << ": the largest so far\n";
            }
        }
    }
    int number_of_elements = std::abs(user_input.size());
    cout << "The smallest value is: " << smaller << "\n";
    cout << "The largest value is: " << larger << "\n";
    cout << "The number of items is: " << number_of_elements << "\n";
    keep_window_open();
    cin.ignore();
}

void keep_window_open()
{
    cin.clear();
    cout << "Please enter a character to exit: ";
    char ch;
    cin >> ch;
    cin.ignore();
    return;
}


If you guys would please run this code and help me fix the runtime logic errors popping up, that'd be much appreciated (yes, I've run it myself and seen what's happening -- I get some numbers in Scientific Notation for some of the numbers I enter, and there are repeated lines and numbers in the output as well).

Any help would be much appreciated. Thanks in advance.
Last edited on
What have you written so far?

Make sure your code is [code]between code tags[/code] so that it has syntax highlighting and line numbers, as well as proper indentation.
Topic archived. No new replies allowed.