Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 135,935 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,674 people online right now. Registration is fast and FREE... Join Now!




c++ calculation of shapes

2 Pages V  1 2 >  
Closed TopicStart new topic

c++ calculation of shapes

zandiago
28 Sep, 2007 - 09:51 AM
Post #1

D.I.C Head
**

Joined: 13 Jul, 2007
Posts: 190


My Contributions
CODE
#include <cstring>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
#include <ctime>

using namespace std;


int main()
{
    
    string Dumbell = '1';
    string Axel = '2';
    string Spear = '3';
    double radius, height, length, width, ball, rod, wheel, cone, vol;
    double pie = 3.14159;


    cout<<"Which shape are you working with"<<endl;
    cin>>'1' || '2' || '3';

    if (cin == 1)
    {

    cout<<"Please enter the radius (of ball), height, length of object & radius of rod"<<endl;
    cin>>radius>>height>>length>>rod;
    cout<<"Dumbell: "<<endl;
    cout<<"Radius of the ball: "<<radius<<endl;
    cout<<"Radius of the rod: "<<rod<<endl;
    cout<<"Length of rod: "<<heigth<<endl;
    vol = (4/3) * 3.142 * radius * radius * radius:
    cout<<"The volume of the dumbbell is "<<vol<<" "<<"cubic inches"<<endl;
    }

    else if (cin == 2)
    {
    cout<<"Please enter the radius of the wheel, radius of the rod, length of the rod, width of the wheel"<<endl;
    cin>>radius>>rod>>length>>width;
    cout<<"Axle: "<<endl;
    cout<<"Radius of the wheel: "<<radius<<endl;
    cout<<"Radius of the rod: "<<rod<<endl;
    cout<<"Length of the rod: "<<length<<endl;
    cout<<"Width of the wheel: "<<width<<endl;
    vol = ((pie * (radius *radius) * length) * 2)
    cout<<"The volume of the axel is "<<vol<<" "<<"cubic inches"<<endl;
    }

    else if (cin==3)
    {
    cout<<"Please enter the radius (of rod), the length of the rod & heigth of the cone"<<endl;
    cin>>radius>>length>>heigth;
    cout<<"Spear: "<<endl;
    cout<<"Radius of the rod: "<<radius<<endl;
    cout<<"Length of the rod: "<<length<<endl;
    cout<<"Height of the cone: "<<heigth<<endl;
    vol = (1/3 * pie * (radius * radius) * height) * 2;
    cout<<"The volume of the spear is "<<vol<<" "<<"cubic inches"<<endl;

    


    return 0;
}

I know it's a crazy one..but here is the assignment:
Write a C++ program that calculates the volume of 3 different geometric shapes. Your
program should give the user a menu like the following and repeat until the user wants to stop.
Volume Calculation Program
Select the Number of Your Chosen Object
1. Dumbbell
2. Axle
3. Spear
When the user selects one of the 3 objects, the program should then request the appropriate measurements (in inches) and then calculate and display the volume of that object. The result should be printed showing cubic-inch units with 3 decimals of precision.
Axle: 2 cylinders and a rod
Dumbbell: two spheres and a rod
Spears: two cones and a rod



Program requirements and Assumptions
Formulas and Abbr.
II P1 = 3.141 59
r radius
= height (or length)
Vol. of a sphere = 4I3flr3
Area of a circle = flr2
Vol. of a cylinder fl9b
Vol. of a cone = 1/3fJr2h
1. The rod-end fits against the dumbbell ball snugly; there is no “air-gap’. You may assume that there is no loss of volume of the ball when the rod is fitted against it.
2. The radius of the dumbbell and axle center rods cannot be larger than half the radius of the ball or wheel. Do not make calculations if the rod radius is more than half of the ball or wheel radius. Give the user an error message instead. For example, if the user enters a radius of 5 inches for the ball or wheel, the rod radius must be no larger than 2.5 inches.
3. The spear’s cone base has exactly the same radius as the connecting rod.
4. Output is to both the monitor and the printer.




User is offlineProfile CardPM
+Quote Post

Martyr2
RE: C++ Calculation Of Shapes
28 Sep, 2007 - 10:14 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,173



Thanked: 208 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Do you do a single assignment on your own? We have helped you with like 3 already and you seem to come up with a new one every day. You are also making silly mistakes in it. Look at where you specify the length of a rod in your first group of statements, you output the height and then you even spell height wrong.

Please attempt to do a compile and weed out the simple errors and give it a whirl yourself before asking us to debug your assignments. We are not all human compilers here (well I am, but that is besides the point).

You don't even specify a question. Please read the forum rules about posting.

Thanks! smile.gif
User is offlineProfile CardPM
+Quote Post

RAVE986
RE: C++ Calculation Of Shapes
30 Sep, 2007 - 08:59 AM
Post #3

New D.I.C Head
*

Joined: 14 Jul, 2007
Posts: 1


My Contributions
QUOTE(zandiago @ 28 Sep, 2007 - 10:51 AM) *

CODE
#include <cstring>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
#include <ctime>

using namespace std;


int main()
{
    
    string Dumbell = '1';
    string Axel = '2';
    string Spear = '3';
    double radius, height, length, width, ball, rod, wheel, cone, vol;
    double pie = 3.14159;


    cout<<"Which shape are you working with"<<endl;
    cin>>'1' || '2' || '3';

    if (cin == 1)
    {

    cout<<"Please enter the radius (of ball), height, length of object & radius of rod"<<endl;
    cin>>radius>>height>>length>>rod;
    cout<<"Dumbell: "<<endl;
    cout<<"Radius of the ball: "<<radius<<endl;
    cout<<"Radius of the rod: "<<rod<<endl;
    cout<<"Length of rod: "<<heigth<<endl;
    vol = (4/3) * 3.142 * radius * radius * radius:
    cout<<"The volume of the dumbbell is "<<vol<<" "<<"cubic inches"<<endl;
    }

    else if (cin == 2)
    {
    cout<<"Please enter the radius of the wheel, radius of the rod, length of the rod, width of the wheel"<<endl;
    cin>>radius>>rod>>length>>width;
    cout<<"Axle: "<<endl;
    cout<<"Radius of the wheel: "<<radius<<endl;
    cout<<"Radius of the rod: "<<rod<<endl;
    cout<<"Length of the rod: "<<length<<endl;
    cout<<"Width of the wheel: "<<width<<endl;
    vol = ((pie * (radius *radius) * length) * 2)
    cout<<"The volume of the axel is "<<vol<<" "<<"cubic inches"<<endl;
    }

    else if (cin==3)
    {
    cout<<"Please enter the radius (of rod), the length of the rod & heigth of the cone"<<endl;
    cin>>radius>>length>>heigth;
    cout<<"Spear: "<<endl;
    cout<<"Radius of the rod: "<<radius<<endl;
    cout<<"Length of the rod: "<<length<<endl;
    cout<<"Height of the cone: "<<heigth<<endl;
    vol = (1/3 * pie * (radius * radius) * height) * 2;
    cout<<"The volume of the spear is "<<vol<<" "<<"cubic inches"<<endl;

    


    return 0;
}

I know it's a crazy one..but here is the assignment:
Write a C++ program that calculates the volume of 3 different geometric shapes. Your
program should give the user a menu like the following and repeat until the user wants to stop.
Volume Calculation Program
Select the Number of Your Chosen Object
1. Dumbbell
2. Axle
3. Spear
When the user selects one of the 3 objects, the program should then request the appropriate measurements (in inches) and then calculate and display the volume of that object. The result should be printed showing cubic-inch units with 3 decimals of precision.
Axle: 2 cylinders and a rod
Dumbbell: two spheres and a rod
Spears: two cones and a rod



Program requirements and Assumptions
Formulas and Abbr.
II P1 = 3.141 59
r radius
= height (or length)
Vol. of a sphere = 4I3flr3
Area of a circle = flr2
Vol. of a cylinder fl9b
Vol. of a cone = 1/3fJr2h
1. The rod-end fits against the dumbbell ball snugly; there is no “air-gap’. You may assume that there is no loss of volume of the ball when the rod is fitted against it.
2. The radius of the dumbbell and axle center rods cannot be larger than half the radius of the ball or wheel. Do not make calculations if the rod radius is more than half of the ball or wheel radius. Give the user an error message instead. For example, if the user enters a radius of 5 inches for the ball or wheel, the rod radius must be no larger than 2.5 inches.
3. The spear’s cone base has exactly the same radius as the connecting rod.
4. Output is to both the monitor and the printer.



dude if and else r not cicluses, you need to do it using "do while" and "switch case" statements for now it is not correct...
TRY HARDER

User is offlineProfile CardPM
+Quote Post

zandiago
RE: C++ Calculation Of Shapes
1 Oct, 2007 - 12:16 PM
Post #4

D.I.C Head
**

Joined: 13 Jul, 2007
Posts: 190


My Contributions
CODE
#include <cmath>
#include <fstream>
#include <string>
#include <iostream>

using namespace std;



int main()
{

    int a,b,c;
    int ht = 0;//height
    int radB = 0;//radius of sphere
    int radR = 0;//radius of rod
    int radC = 0;//radius cone
    int wt = 0;//width
    int lt = 0;//length
    int wh = 0;//radius of wheel
    const double pie = 3.14159;//pie
    double volsp = (4.0/3) * (radB * radB * radB) * pie;//volume of a sphere
    double volcy = pie * (radR * radR) * ht;//volume of cylinder/rod
    double volcn = (1.0/3) * (radC * radC) * pie * ht;//volume of cone

    cout << "Do you want the volume of:\n";
    cout << "1. Dumbbell \n";
    cout << "2. Axle \n";
    cout << "3. Spear \n";

    cin >> a;
        switch (a)
        {
            case 1:
            cout<<"Enter radius of Sphere ";
            cin>>radB;
            cout<<"Enter radius of rod ";
            cin>>radR;
            cout<<"Enter length of rod ";
            cin>>lt;
            float dumbbell = (volcy) + (volsp * 2);
            cout<<"The volume of the dumbbell is "<<dumbbell<<" cubic inches"<<endl;
        }

    cin >>b;
        switch (b)
        {
            case 2:
            cout<<"Enter radius of wheel ";
            cin>>wh;
            cout<<"Enter radius of rod ";
            cin>>radR;
            cout<<"Enter length of rod ";
            cin>>lt;
            cout<<"Enter width of wheel ";
            cin>>wt;

            double axle = (volsp * 3);
            cout<<"The volume of the axle is "<<axle<<" cubic inches"<<endl;
        }

    cin >> c;
        switch (c)
        {
            case 3:
            cout<<"Enter radius of rod ";
            cin>>radR;
            cout<<"Enter length of rod ";
            cin>>lt;
            cout<<"Enter height of cone ";
            cin>>ht;

            double spear = (volcn * 2) + volcn;
            cout<<"The volume of the spear is "<<spear<<"cubic inches"<<endl;
        }
                
    return 0;
}


Ok, so i made a bit of revision to my previous post. A few things:
1. I tried finding the volume of the Dumbbell(option 1) and it shows the result as being zero.
2. How to modify the program so that after the user calcualtes the volume of one object the program automatically goes back to the menu?
3. I'm kinna lost as how to calcualte the volume of the axle and spear.

Thx much for all the assistance.
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: C++ Calculation Of Shapes
1 Oct, 2007 - 12:29 PM
Post #5

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 1 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
QUOTE

1. I tried finding the volume of the Dumbbell(option 1) and it shows the result as being zero.

of course it does. you add together two zeros to get your result.

QUOTE

2. How to modify the program so that after the user calcualtes the volume of one object the program automatically goes back to the menu?

put it in a do-while loop

QUOTE
3. I'm kinna lost as how to calcualte the volume of the axle and spear.

why are you lost? what does a spear look-like? using only the simple shapes for which you've been given volumetric equations, how could you build a spear?

-jjh
User is offlineProfile CardPM
+Quote Post

zandiago
RE: C++ Calculation Of Shapes
1 Oct, 2007 - 05:08 PM
Post #6

D.I.C Head
**

Joined: 13 Jul, 2007
Posts: 190


My Contributions
with regards to the results being zero, if i never set those variables to '0'...the compiler error would say that the variables haven't been initialized.
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: C++ Calculation Of Shapes
1 Oct, 2007 - 05:30 PM
Post #7

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 1 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
yes, but you haven't done anything to make them NOT zero. you need to perform the calculations and assign the result to your volume variable, after you have recieved the input from the user and stored the input in the appropriate parameters. just because you have written an equation in the source code doesn't mean that the equation is going to update every time you change a variable. you need a function to do that, or you just need to rewrite the equation where appropriate.

also, you switch-case control structure is a little off. you only have one case in each of three switch statements. the form for a switch-case control is:

CODE
switch (variable) {
    case value_1: {
        //do stuff if variable=value_1
    }
    case value_2: {
        //do stuff if variable=value_2
    }
    ...
    default: {
        //do stuff if variable is not equal to any of the specified case values
    }
}


-jjh
User is offlineProfile CardPM
+Quote Post

zandiago
RE: C++ Calculation Of Shapes
1 Oct, 2007 - 07:13 PM
Post #8

D.I.C Head
**

Joined: 13 Jul, 2007
Posts: 190


My Contributions
Ok kool- i see it now...almost there, I did try calculating the volume for the dumbbell (option 1) using the following:

CODE

#include <cmath>
#include <fstream>
#include <string>
#include <iostream>

using namespace std;



int main()
{

    int a,b,c;
    int ht = 0;//height
    int radB = 0;//radius of sphere
    int radR = 0;//radius of rod
    int radC = 0;//radius cone
    int wt = 0;//width
    int lt = 0;//length
    int wh = 0;//radius of wheel
    const double pie = 3.14159;//pie
    double volsp = (4.0/3) * (radB * radB * radB) * pie;//volume of a sphere
    double volcy = pie * (radR * radR) * ht;//volume of cylinder/rod
    double volcn = (1.0/3) * (radC * radC) * pie * ht;//volume of cone

    cout << "Do you want the volume of:\n";
    cout << "1. Dumbbell \n";
    cout << "2. Axle \n";
    cout << "3. Spear \n";

    cin >> a;
        switch (a)
        {
            case 1:
            cout<<"Enter radius of Sphere ";
            cin>>radB;
            cout<<"Enter radius of rod ";
            cin>>radR;
            cout<<"Enter length of rod ";
            cin>>lt;
            double volsp = (4.0/3) * (radB * radB * radB) * pie;//volume of a sphere
            double volcy = pie * (radR * radR) * ht;//volume of cylinder/rod
            double volcn = (1.0/3) * (radC * radC) * pie * ht;//volume of cone
            float dumbbell = (volcy) + (volsp * 2);
            cout<<"The volume of the dumbbell is "<<dumbbell<<" cubic inches"<<endl;
        }

A dumbbell consists of two spheres and a cylinder. What is it that i'm overlooking. If i use the following info for dumbbell: radius of ball = 17. radius of the rod = 7. length of rod = 13. My professor said the answer should be 43160.211, however i get 41159...Thanx.

This post has been edited by zandiago: 1 Oct, 2007 - 07:18 PM
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: C++ Calculation Of Shapes
1 Oct, 2007 - 07:49 PM
Post #9

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 1 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
you're using the int datatype to store your values, which can only handle integer numbers. anything else gets truncated to an int. you should use the double datatype for all of your size measurements, otherwise you'll have this truncation problem.

-jjh
User is offlineProfile CardPM
+Quote Post

zandiago
RE: C++ Calculation Of Shapes
2 Oct, 2007 - 06:06 PM
Post #10

D.I.C Head
**

Joined: 13 Jul, 2007
Posts: 190


My Contributions
CODE
#include <cmath>
#include <fstream>
#include <string>
#include <iostream>

using namespace std;



int main()
{

    int a,b,c;
    double ht = 0;//height
    double radB = 0;//radius of sphere
    double radR = 0;//radius of rod
    double radC = 0;//radius cone
    double wt = 0;//width
    double lt = 0;//length
    double wh = 0;//radius of wheel
    const double pie = 3.14159;//pie
    double volsp = (4.0/3) * (radB * radB * radB) * pie;//volume of a sphere
    double volcy = pie * (radR * radR) * ht;//volume of cylinder/rod
    double volcn = (1.0/3) * (radC * radC) * pie * ht;//volume of cone
    char again = 'y';

    cout << "Do you want the volume of:\n";
    cout << "1. Dumbbell \n";
    cout << "2. Axle \n";
    cout << "3. Spear \n";

    while (again == 'y')
    {
    cin >> a;
        switch (a)
        {
            case 1:
            cout<<"Enter radius of Sphere ";
            cin>>radB;
            cout<<"Enter radius of rod ";
            cin>>radR;
            cout<<"Enter length of rod ";
            cin>>lt;
            double volsp = (4.0/3) * (radB * radB * radB) * pie;//volume of a sphere
            double volcy = pie * (radR * radR) * ht;//volume of cylinder/rod
            double volcn = (1.0/3) * (radC * radC) * pie * ht;//volume of cone
            double dumbbell = (volcy) + (volsp * 2);
            cout<<"The volume of the dumbbell is "<<dumbbell<<" cubic inches"<<endl;
            break;
        }

    cin>>b;
        case 2:
        {            
            cout<<"Enter radius of wheel ";
            cin>>wh;
            cout<<"Enter radius of rod ";
            cin>>radR;
            cout<<"Enter length of rod ";
            cin>>lt;
            cout<<"Enter width of wheel ";
            cin>>wt;
            double volsp = (4.0/3) * (radB * radB * radB) * pie;//volume of a sphere
            double volcy = pie * (radR * radR) * ht;//volume of cylinder/rod
            double volcn = (1.0/3) * (radC * radC) * pie * ht;//volume of cone
            double axle = (volsp * 3);
            cout<<"The volume of the axle is "<<axle<<" cubic inches"<<endl;
            break;
        }

    cin >> c;
        case 3:
        {        
            cout<<"Enter radius of rod ";
            cin>>radR;
            cout<<"Enter length of rod ";
            cin>>lt;
            cout<<"Enter height of cone ";
            cin>>ht;
            double volsp = (4.0/3) * (radB * radB * radB) * pie;//volume of a sphere
            double volcy = pie * (radR * radR) * ht;//volume of cylinder/rod
            double volcn = (1.0/3) * (radC * radC) * pie * ht;//volume of cone
            double spear = (volcn * 3);
            cout<<"The volume of the spear is "<<spear<<"cubic inches"<<endl;
            break;

        }
           cout << "\n Do you want to try again? (y/n): ";
           cin >> again;
}
                
    return 0;
}

I still didn't get the right answer after changing the data types and it's now saying that the case statements are illegal...thx 4 all the assistance.
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: C++ Calculation Of Shapes
2 Oct, 2007 - 06:20 PM
Post #11

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 1 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
that's because they are...right now, because of the way you have placed the curly braces, you have a switch statement with a single case, and then 2 more isolated case statements - and isolated case statements are not allowed.

look at my previous post with the simplified switch-case structures. your case statements need to be nested within the switch control block. and you don't want to have a separate cin statement to get a different variable for each case. the entire point of the switch-case control structure is to do something different, depending on the value of a single variable.

you want the test variable in your switch statement to be the variable that you accept the user input into. then your case labels must correspond to each of the potential inputs:

CODE
cin >> a;
switch (a) {
    case 1: {
        //dumbell input, calculations, and display
    }
    case 2: {
        //axle input, calculations, and display
    }
    ...
    case 3: {
        //spear input, calculations, and display
    }
}


oh, and with respect to the data type problem - make sure that all of your literals are the proper form as well. 3 is an int, 3.0 is a double. you'll want to use the double form for the literals you have in your equations.

hope that helps.

-jjh

This post has been edited by jjhaag: 2 Oct, 2007 - 06:21 PM
User is offlineProfile CardPM
+Quote Post

zandiago
RE: C++ Calculation Of Shapes
2 Oct, 2007 - 06:33 PM
Post #12

D.I.C Head
**

Joined: 13 Jul, 2007
Posts: 190


My Contributions
Thx...the with that, was to figure out where to place your 'cin' statement in relation to the braces.
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Closed TopicStart new topic
Time is now: 12/1/08 08:29AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets