Need advice on some actual evaluations converting measurement systems


Now I have done the first two parts but I am stuck on the last 3 parts of the problem. Here is the description of this problem Among many measurement systems two of them seem to be the most widespread: metric and imperial. To make the story simpler we
assume that the first one uses "meter" as an only unit (expressed as a real number) while the second uses "foot" (always integer) and
"inch" (real number).
Your task is to write a simple "measurements converter". We want it to perform the following actions:
first, it should ask the user which system she/he uses to input data; we assume that 0 means "metric" and 1 means "imperial"
depending on a user's answer, the program should ask either for meters or feet and inches
next, the program should output the distance in proper (different) units: either in feet and inches or in meters
result outputted as metric should look like 123.4m
result printed as imperial should look like 12'3.5" Need help on those last 3 thank you.

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
  

#include "stdafx.h"
#include <iostream>
using namespace std;

int main(void)
{
	int sys;
	float m, ft, in;

	// Insert your code here 

	cout << "Enter 0 for metric or enter 1 for imperial." << endl;
	cin >> sys;

	if (sys == 0)
	{

		cout << "You have selected the metric system put meters: " << endl;
		cin >> m;

	}

	else if (sys == 1)
	{
		cout << "You have selected the imperial system " << endl;
		cout << "First put foot: " << endl;
		cin >> ft;
		cout << "Next put in integer: " << endl;
		cin >> in;
	}




	return 0;
}
Last edited on
Dividing total inches as integer (or just drop the fractional part) gets you the feet part. Modulo (or fmod) gets you the remaining inches.
Should I put them separately inside the else if loop for else if (sys==1) ? and how do I exactly output them combine them as strings ? Thus the output should be the following for any of them result outputted as metric should look like 123.4m
result printed as imperial should look like 12'3.5"
Last edited on
you can do it with a cout statement...

cout << "words " << totalfeet << "'" <<totalinches << "\"" << endl;

or
cout << "words " << decimalmeters << "m" << endl;

your code is a little weird, you say to input integers but the variables are floats.

I am following the program its based on a lab that's how they give it to me its already made. If you are familiar with the C++ institute program of course that's how they give it to me
Last edited on
Also how to you implement this * Dividing total inches as integer (or just drop the fractional part) gets you the feet part. Modulo (or fmod) gets you the remaining inches. Like do I just put it inside the loop ? I will try to figure it out
you can cast to interger

double d = 3.14;
(int)d is 3.0

mod is %

x = a %(int)d; the remainder when a is divided by d.

fmod is almost the same as % for floats. Read this one online.

13%12 = 1
13/12 = 1
1 foot, 1 inch...
What are you trying to say here ?

double d = 3.14;
(int)d is 3.0

mod is %

x = a %(int)d; the remainder when a is divided by d.

fmod is almost the same as % for floats. Read this one online.

13%12 = 1
13/12 = 1
1 foot, 1 inch...

All the variables are floats I have to do the second uses "foot" (always integer) and
"inch" (real number). What you put as a anwser does not make any sense and makes it more confusing then clearing it up.
cout << "words " << totalfeet << "'" <<totalinches << "\"" << endl;

or
cout << "words " << decimalmeters << "m" << endl;

How is this going to put out this

Example Input: 1 , 0 , 1
Example Output: 0.0254m

Example Input: 0 , 0.0254
Example Output: 0'1"

And keep it in mind I have to use this code template they give me

1
2
3
4
5
6
7
8
9
10

#include <iostream>
using namespace std;
int main(void) {
int sys;
float m, ft, in;
// Insert your code here
return 0;
}


That means I cant put other stuff
Last edited on
Can anybody help me here ?
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
#include <iostream>
#include <cmath>//std::floor, std::round
#include <iomanip>//std::fixed, std::setprecision
#include <utility>//std::move

auto constexpr METER_FEET = 3.2808;
auto constexpr FEET_INCH = 12;
auto constexpr INCH_METER = 0.0254;

struct Imperial;//forward declaration

struct Metric
{
    float m_meter;
    Metric (float meter) {m_meter = std::move(meter);}
    //can also use member initializer list or assignment instead of move semantics
    Metric(Imperial imp);
};
std::ostream& operator << (std::ostream& os, const Metric& m)//for printing
{
    os << std::fixed << std::setprecision(2) << m.m_meter << "m"; return os;
}
struct Imperial
{
    int m_foot;
    float m_inch;
    Imperial(int foot, float inch) {m_foot = std::move(foot); m_inch = std::move(inch);}
    Imperial(Metric m);
};
std::ostream& operator << (std::ostream& os, const Imperial& imp)
{
    os << imp.m_foot << "'" ;
    os << imp.m_inch << "''";
    return os;
}
Metric::Metric(Imperial imp)
{
        m_meter = (imp.m_foot * FEET_INCH + imp.m_inch) * (INCH_METER);
}
Imperial::Imperial(Metric m)
{
    float totalFeet = METER_FEET * m.m_meter;
    m_foot = std::floor (totalFeet);
    m_inch = std::round((totalFeet - m_foot) * FEET_INCH);
}
int main()
{
    Metric m{8.9};
    Imperial imp{std::move(m)};//moved objects in valid but unspecified state
    //http://stackoverflow.com/questions/7930105/does-moving-leave-the-object-in-a-usable-state
    std::cout << imp << "\n";
    std::cout << imp.m_inch << "\n";

    Imperial pmi{29, 2};
    Metric mm{std::move(pmi)};
    std::cout << mm << "\n";

}

http://coliru.stacked-crooked.com/a/1006e8cab17ab33f

Last edited on
Thanks
Thanks great
Topic archived. No new replies allowed.