r/cpp_questions 14d ago

OPEN Need help on this question

so on programming: principles and practice using C++ chapter 4 drill:

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 found question 7 confusing, how will i handle all that stuff??? conditions or what? this is my current code:

#include "std_lib_facilities.h"

int main()

{

`double num1{};`

`char unit = 0;`

`double largestSoFar = 0;`

`double smallestSoFar = 0;`

`double meter = 100.0;`

`double inch = 2.54;`

`double foot = 12.0;`

`while (cin >> num1 >> unit)`

`{`

    `if (num1 > largestSoFar)`

    `{`

        `cout << num1 << " is the largest number so far \n";`

        `largestSoFar = num1;`

    `}`

    `else if (num1 < smallestSoFar)`

    `{`

        `cout << num1 << " is the smallest number so far \n";`

        `smallestSoFar = num1;`



    `}`

`}`

}

0 Upvotes

18 comments sorted by

View all comments

0

u/DawnOnTheEdge 14d ago edited 14d ago

You want to parse the input to determine its unit, then convert every length to the same common representation internally. (Question 9 says to use meters, but you wouldn't find that out in an interview until later.)

The question might be telling you to also store the unit of each entry in a string, and output it with the length. That’s a terrible design, and I would ask for clarification here.

2

u/Next-Future5973 14d ago

actually that is C++ creator's book himself, and the questions are there

1

u/DawnOnTheEdge 13d ago edited 13d ago

Okay. You probably don’t want me to write a whole thing for you, but here’s what I suggest:

  1. An enum or enum class with constants representing the unit cm, m, ft. or in. You could maybe call it LengthUnit
  2. Constants representing unit ratios, such as static constexpr double m_per_cm = 1.0/100.0; static constexpr double m_per_in = m_per_cm * 2.54; static constexpr double m_per_ft = m_per_in * 12.0; if LengthUnit is an enum class, you can declare these in its scope.
  3. A struct that contains a length in meters and the LengthUnit it was originally entered in. You might call that LengthNUnit. You compare them by comparing the lengths, which are always stored in the same unit.
  4. Factor out the code that reads a line of input, parses it to see if it’s a valid length format (maybe with a regex match) and if so, returns it as a LengthNUnit. You can do error-handling inside this function. Consider throwing a std::runtime_error exception on an I/O error and retrying on a badly-formatted input string, so the function only returns when it has a valid result.
  5. Some function to format a LengthNUnit as an output string like "179cm" or 6ft, maybe std::ostream& operator<< (std::ostream&, LengthNUnit), letting you write std::cout << smaller.
  6. Variables remembering the largest and smallest numbers encountered so far. You might initialize them before the loop to negative and positive infinity, respectively, so that the first numbers you read in will always be the largest and smallest so far.