Feet and Inches to Inches

I just started college and I have a c++ homework assignment that finds the volume of a box in cubic inches. Example input and output:

Enter the height in feet and inches: 2 4
Enter the width in feet and inches: 3 0
Enter the depth in feet and inches: 0 22

Height: 2'4" = 28 inches
Width: 3'0" = 36 inches
Depth: 1'10" = 22 inches
Volume: 28 * 36 * 22 = 22176 Cubic inches

what I dont understand is how do make it so that when the user enters 2 4 the program knows that the first number is feet and the second number is inches. Can someone please either explain this or write a sample program using this type of input so I can try and understand how to do this?

PLEASE HELP!!! I HAVE TO HAVE THIS DONE BY TOMORROW :(
Just get the data from the user as 6 different input options.
Grab the height_feet, height_inches, width_feet, width_inches, depth_feet, depth_inches.
Get that input from the user (each one would be an int datatype).
When you have them perform the formula to decide how many inches are in the first...
Google "Formula to calculate inches in feet" or something.
For the last one volume just take those numbers and multiple them by each other...
give me a minute and I might try to write you something for it.
the problem is that I have to have the user enter the feet and inches in exactly the format listed above. 2 4 same line. I just have no idea how to allow an input of that format and tell the program that the number before the space is feet and the number after the space is inches. I believe I can do the rest of the code from conversion to output as long as I know how to do that.
Not sure how to help you there...best I can do is below (test).

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
#include <iostream>

int main() {
    // Declare the ones we need
    int height_feet;
    int height_inches;
    int width_feet;
    int width_inches;
    int depth_feet;
    int depth_inches;

    // get user input for each.
    std::cout<<"Please enter the information as it appears on the screen.."<<std::endl;

    // Get height
    std::cout<<"Enter amount of feet for the height? ";
    std::cin>>height_feet;
    std::cin.ignore();

    std::cout<<"Enter amount of inches for the height? ";
    std::cin>>height_inches;
    std::cin.ignore();

    // Get width
    std::cout<<"Enter amount of feet for the width? ";
    std::cin>>width_feet;
    std::cin.ignore();

    std::cout<<"Enter amount of inches for the width? ";
    std::cin>>width_inches;
    std::cin.ignore();

    // get depth
    std::cout<<"Enter amount of feet for the depth? ";
    std::cin>>depth_feet;
    std::cin.ignore();

    std::cout<<"Enter amount of inches for the depth? ";
    std::cin>>depth_inches;
    std::cin.ignore();

    // Perform calculations
    int height_calc;
    int height_convert;
    height_convert = height_feet*12;
    height_calc = height_convert+height_inches;
    int width_calc;
    int width_convert;
    width_convert = width_feet*12;
    width_calc = width_convert+width_inches;
    int depth_calc;
    int depth_convert;
    depth_convert = depth_feet*12;
    depth_calc = depth_convert+depth_inches;

    // get volume
    int volume = height_calc*width_calc*depth_calc;

    std::cout<<height_calc<<std::endl;
    std::cout<<width_calc<<std::endl;
    std::cout<<depth_calc<<std::endl;
    std::cout<<volume<<std::endl;
}

Very basic and to the point to give you an idea of how to do what you are wanting. You should be able to easily take that and go from there.You can format the output to present it as you wanted.
As far as I know if you want the user to enter height and feet together..in one line.meaning in one input, then you would have to do a bunch of stuff to the string to strip out the two numbers and separate them and tell which is which..some kind of string manipulation or regular expressions..unfortunately I don't yet know enough about C++ to go into how to do all of that because I do not know myself (atleast not yet) hopefully the above tested code will help you some.
right I understand all of this and I do appreciate this example though I still do not understand how to allow the user to input feet and inches on the same line in the format 2 4 with the number before the space being feet and the number after the space being inches... This is probably pretty basic stuff but im just not grasping how to let the program know that 2 4 means 2 feet 4 inches.
ok just read your reply and again thank you for your input I do believe that it has something to do with a float or remainder?... not so sure about that but still in the dark...:(
Unfortunately I can't help with that part. I don't know enough about strings yet to be able to do something like that.

I wish I could help further, good luck.
Thanks I'll keep trying...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
using namespace std;

main(){
       int height,width,depth,height_feet,height_inches;
       int width_feet,width_inches,depth_feet,depth_inches;
       cout<<"Enter the height in feet and inches: ";
       cin>>height_feet>>height_inches;
       height=height_feet*12+height_inches;
       cout<<"Enter the width in feet and inches: ";
       cin>>width_feet>>width_inches;
       width=width_feet*12+width_inches;
       cout<<"Enter the depth in feet and inches: ";
       cin>>depth_feet>>depth_inches;
       depth=depth_feet*12+depth_inches;
       
       cout<<"Height: "<<height_feet<<"'"<<height_inches<<"\" = "<<height<<" inches"<<endl;
       cout<<"Width: "<<width_feet<<"'"<<width_inches<<"\" = "<<width<<" inches"<<endl;
       cout<<"Depth: "<<depth_feet<<"'"<<depth_inches<<"\" = "<<depth<<" inches"<<endl;
       cout<<"Volume: "<<height<<" * "<<width<<" * "<<depth<<" = "<<height*width*height<<" Cubic inches";
       system("pause");
}


you can do that using 2 consecutive cin try this one
Last edited on
Oh cool..there you go. That'll work for what your wanting..although by what I read I recommend replacing the system("pause")
with
cin.get()
instead.
what is wrong with system("pause")??
http://www.gidnetwork.com/b-61.html
This should explain everything in detail.
It could be wrong but after reading this I tried to stopping using it.
not familiar with system pause however your lines of code make a ton of sense the only thing I don't understand is why you used int instead of unsigned int? Please explain.
ok her is what I finally ended up with and this is just the function of my assignment that deals with the volume of the cube.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//These are my variable declarations. I declared them as unsigned because this way it can be a negative number.
   unsigned int height, width, depth, volume;
   unsigned int feet_a, feet_b, feet_c;
   unsigned int inches_a, inches_b, inches_c;
   //This section asks the user for their input for the measurement of three sides of the cube.
   cout << "\nPlease enter the height of the box in feet and inches: ";
   cin >> feet_a >> inches_a;

   cout << "\nPlease enter the width of a the box in feet and inches: ";
   cin >> feet_b >> inches_b;

   cout << "\nPlease enter the depth of the box in feet and inches: ";
   cin >> feet_c >> inches_c;
   //I defined what each of my variables is exactly in this section. Also I explained the math for each variable here.
   height = feet_a * 12 + inches_a;
   width  = feet_b * 12 + inches_b;
   depth  = feet_c * 12 + inches_c;
   volume = height * width * depth;
   //Finally here is where I output the result of my code.  
   cout << "\n Height: " << feet_a<< "'" << inches_a <<"\" = " << height << "\n";
   cout << "\n Width : " << feet_b<< "'" << inches_b <<"\" = " << width << "\n";
   cout << "\n Depth : " << feet_c<< "'" << inches_c <<"\" = " << depth << "\n";
   cout << "\nThe Volume Of The Cube is : " <<height<<" * "<<width<<" * "<<depth<<" = " <<volume<<" cubic inches. \n";


Thank you for your help with this you were extremely helpful!!!!!
Topic archived. No new replies allowed.