Confusion on how to write the code

ask the user which item they have, and how many cubic meters they
have. Display the total mass of the item. You should allow for non-whole numbers.

How do I start this out. I joined a c++ class with not much knowledge and I am very confused on how to do this. Id prefer basic code, so its easier to understand.

thank you, BuddhaTwo.
Hey BuddhaTwo, did you even try solving it? We'll be glad to help, but you have to show us that you want to learn - not to get us to do your homework.
Post code that you came up with, describe your problem(what you don't understand/what doesn't work in your code) - and we'll help you.

Cheers!
Last edited on
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
#include <iostream>
using namespace std;

/*
A cubic meter of feathers has a mass of 55g
cubic meter of steel has mass of 1200 kg
cubic meter of lead has mass of 3400kg

ask user which item they have (A,B,C)
How much cubic meters they have
display total mass of the item
allow for non-whole numbers
*/
int main()
{
	int a, b, c;

	//user asked what item and picks
cout << "Please enter the object you have" << endl;
cout << "A: Feathers" << endl;
cout << "B: Lead" << endl;
cout << "C: Steel" << endl;
	


	system ("pause");
		return 0;
}



I don't understand how I am suppose to add in the non-whole numbers and how i'm suppose to get the total mass. This is as far as I could get code wise.
Last edited on
Before we get into your problems, I would suggest you please use the [code] [/ code] tags when posting your code. It makes it easier for everyone else to read it and you're more likely to get the required help that way.

Now, basically what you want to do is store two inputs from the user in variables. Those could be named anything you want (item and meters would be easy to undertand). Remember to use the appropriate type of variable depending on the input you need to store (int for whole numbers, char from character, etc). You can then use if/else statements to check the value of those variables and set the proper mass per cubic meter ratio.

Once you've got that you can simple find the total by multiplying the mass per cubic meter with the number of cubic meters.
I don't understand how I am suppose to add in the non-whole numbers

Well, a non-whole number can't be an integer - because integers aren't a whole number. So instead of using ints to store the numbers, you should be using a different type that can store a number that isn't a whole number.

If you don't understand how to read the user's input, I suggest you look at this tutorial:

http://www.cplusplus.com/doc/tutorial/basic_io/

and how i'm suppose to get the total mass.

Well, let's say you were doing this in your head, rather than writing a program. How would you get the total mass, if you knew the amount of a substance someone had?

In general with programming, when you have a problem to solve, the first thing to do is ignore the actual programming, and work out the logical steps involved in doing it. Then, once you've understood what those steps are, then you can start working out how to write those steps as code.
Thank you all (:
Topic archived. No new replies allowed.