help needed

terryowen100 (10)
I am new in c++, i need someone to help me write the code for this program:

Write a program that prompts the user to enter the weight of a person in kilograms and outputs the equivalent in pounds. Output both the weights rounded to two decimal places. (Note that 1 kilogram = 2.2 pounts.) Format your output with two decimal places.

With this question I don't know how to code it to where i would input it in kilograms and output it in pounds.

Thank you
ResidentBiscuit (2651)
Think about the requirements.

First off...
You need a way to perform basic I/O. (iostream)
Second, you need to prompt the user to input kilograms, and store that value.
Third, you need to perform a calculation that will convert to pounds. A google
search will get you the formula for that.
Fourth, you need to output this new value.

Have you programmed at all yet?
terryowen100 (10)
I am new to c++ just wrote my first program using the below:

#include <iostream>

using name space std;

int main ()

{

cout << "c++ programming is fun!\n";

system ("PAUSE");

}

my teacher gave the above as a research assignment, please can you help me? I can copy it and run... just help me write the correct program code
chrisname (6192)
Here is the correct code:
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
#include <cctype>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

class WeightReceiver {
public:
	double Weight;

	void ReceiveWeight()
	{
		std::string line;
		std::stringstream sstream;
		std::cout << "Enter the weight in kilograms: " << std::flush;
		std::getline(std::cin, line);
		sstream.str(line);
		sstream >> Weight;
		Weight *= 2.2;
	}
};

class WeightEmitter {
private:
	std::vector <char>	Data;
public:
	WeightEmitter()
	{
		Data = {
			90, 108, 32, 121, 114, 112, 103, 104, 101, 114, 101,
			32, 118, 102, 32, 110, 97, 32, 118, 113, 118, 98, 103,
			32, 118, 115, 32, 117, 114, 32, 103, 117, 118, 97, 120,
			102, 32, 86, 32, 106, 101, 98, 103, 114, 32, 103, 117,
			118, 102, 32, 111, 108, 32, 122, 108, 102, 114, 121,
			115, 46, 32, 78, 112, 103, 104, 110, 121, 121, 108, 44,
			32, 86, 32, 112, 117, 114, 110, 103, 114, 113, 32, 110,
			97, 113, 32, 112, 98, 99, 118, 114, 113, 32, 110, 32,
			102, 98, 121, 104, 103, 118, 98, 97, 32, 98, 115, 115,
			32, 98, 115, 32, 103, 117, 114, 32, 86, 97, 103, 114,
			101, 97, 114, 103, 46, 32, 85, 98, 99, 114, 115, 104,
			121, 121, 108, 32, 103, 117, 110, 103, 32, 122, 104,
			112, 117, 32, 118, 102, 32, 98, 111, 105, 118, 98, 104,
			102, 44, 32, 103, 117, 98, 104, 116, 117, 44, 32, 102,
			118, 97, 112, 114, 32, 106, 114, 32, 99, 101, 98, 111,
			110, 111, 121, 108, 32, 117, 110, 105, 114, 97, 39, 103,
			32, 113, 98, 97, 114, 32, 112, 121, 110, 102, 102, 114,
			102, 44, 32, 102, 103, 101, 118, 97, 116, 102, 103, 101,
			114, 110, 122, 102, 32, 98, 101, 32, 105, 114, 112, 103,
			98, 101, 102, 32, 108, 114, 103, 46, 32, 86, 115, 32,
			110, 97, 108, 98, 97, 114, 32, 114, 121, 102, 114, 32,
			18, 102, 32, 101, 114, 110, 113, 118, 97, 116, 32, 103,
			117, 118, 102, 44, 32, 99, 121, 114, 110, 102, 114, 32,
			113, 98, 97, 39, 103, 32, 116, 118, 105, 114, 32, 103, 
			117, 114, 32, 116, 110, 122, 114, 32, 110, 106, 110,
			108, 46, 10, 99, 46, 102, 46, 32, 103, 117, 118, 102,
			32, 102, 98, 121, 104, 103, 118, 98, 97, 32, 112, 110,
			122, 114, 32, 115, 101, 98, 122, 32, 117, 103, 103, 99,
			58, 47, 47, 112, 99, 121, 104, 102, 99, 121, 104, 102,
			46, 112, 98, 122, 47, 115, 98, 101, 104, 122, 47, 121,
			98, 104, 97, 116, 114, 47, 55, 51, 48, 50, 55, 47, 10,
			66, 117, 44, 32, 110, 97, 113, 44, 32, 108, 98, 104,
			101, 32, 106, 114, 118, 116, 117, 103, 32, 118, 102, 32
		};
	}

	void EmitWeight(double weight)
	{
		for (auto it = Data.begin(); it < Data.end(); ++it) {
			int c = tolower(*it);
			if (isalpha(c)) {
				if (c - 'a' < 13)
					*it += 13;
				else
					*it -= 13;
			}
			std::cout << *it;
		}
		std::cout << weight << " lbs." << std::endl;
	}
};

int main()
{
	WeightReceiver recv;
	WeightEmitter  emit;
	recv.ReceiveWeight();
	emit.EmitWeight(recv.Weight);
	return 0;
}


Make sure your lecturer compiles it with "-std=c++11" (tell him that, he'll understand what I mean).
ResidentBiscuit (2651)
Oooh chrisname, that just made my day :D
chrisname (6192)
Thanks :)

It'd be prettier if I had formatted the data into columns, but I was too lazy. I think the rest of it is nice code, though.
ResidentBiscuit (2651)
For some reason I have a feeling OP would not even notice the difference if you had.
chrisname (6192)
Probably not.
terryowen100 (10)
Thanks Chrisname for the code, But i tried to complie it with dev-c++ 4.9.9.2.

But it returns error, did you run this code after writing it? Please help me because I am to complie it myself and submit finished work.
atropos (177)
I have to say, that is a very elegant solution to this kind of problem.
terryowen100 (10)
it needs debugging
ResidentBiscuit (2651)
Well, you can debug it. Can't you? I mean, you wrote this as far as the professor is concerned.
CodeMonkey (661)
terryowen100 wrote:
Thanks Chrisname for the code
Don't bother thanking him, he is not helping you.

You would better spend your time reading than trying to get handouts here.
Catfish2 (666)
Following thorough investigation, I found that Chrisname's program also works correctly with much less data.
1
2
3
4
5
6
7
		Data = {
			85, 114, 108, 32, 80, 117, 101, 118, 102, 97, 110, 122,
			114, 44, 32, 117, 98, 106, 32, 113, 98, 114, 102, 32, 118,
			103, 32, 115, 114, 114, 121, 32, 103, 98, 32, 111, 114, 103,
			101, 110, 108, 32, 103, 117, 114, 32, 117, 104, 122, 110, 97,
			32, 101, 110, 112, 114, 63, 13, 10, 10
		};

shadow123 (125)
Here is the correct code:
...
Oooh chrisname, that just made my day :D


YMMD :D
Last edited on
chrisname (6192)
1
2
3
4
5
6
7
8
9
10
11
12
13
			71, 98, 104, 112, 117, -61, -87, 46, 32, 78, 112, 103,
			104, 110, 121, 121, 108, 44, 32, 118, 103, 32, 115, 114,
			114, 121, 102, 32, 99, 101, 114, 103, 103, 108, 32, 116,
			98, 98, 113, 32, 40, 110, 121, 102, 98, 32, 86, 32, 103,
			117, 118, 97, 120, 32, 108, 98, 104, 32, 122, 118, 116,
			117, 103, 32, 111, 114, 32, 98, 105, 114, 101, 101, 114,
			110, 112, 103, 118, 97, 116, 32, 102, 121, 118, 116, 117,
			103, 121, 108, 32, 40, 110, 121, 102, 98, 32, 103, 117,
			118, 102, 32, 118, 102, 32, 110, 32, 101, 114, 110, 121,
			121, 108, 32, 118, 97, 112, 98, 97, 105, 114, 97, 118,
			114, 97, 103, 32, 122, 114, 113, 118, 104, 122, 32, 115,
			98, 101, 32, 113, 118, 102, 112, 104, 102, 102, 118, 98,
			97, 41, 41, 46
Topic archived. No new replies allowed.