473,407 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

c++ shape calculation

Expand|Select|Wrap|Line Numbers
  1. #include <cstring>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <cmath>
  5. #include <fstream>
  6. #include <string>
  7. #include <ctime>
  8.  
  9. using namespace std;
  10.  
  11.  
  12. int main()
  13. {
  14.  
  15.     string Dumbell = '1';
  16.     string Axel = '2';
  17.     string Spear = '3';
  18.     double radius, height, length, width, ball, rod, wheel, cone, vol;
  19.     double pie = 3.14159;
  20.  
  21.  
  22.     cout<<"Which shape are you working with"<<endl;
  23.     cin>>'1' || '2' || '3';
  24.  
  25.     if (cin == 1)
  26.     {
  27.  
  28.     cout<<"Please enter the radius (of ball), height, length of object & radius of rod"<<endl;
  29.     cin>>radius>>heigth>>length>>rod;
  30.     cout<<"Dumbell: "<<endl;
  31.     cout<<"Radius of the ball: "<<radius<<endl;
  32.     cout<<"Radius of the rod: "<<rod<<endl;
  33.     cout<<"Length of rod: "<<heigth<<endl;
  34.     vol = (4/3) * 3.142 * radius * radius * radius:
  35.     cout<<"The volume of the dumbbell is "<<vol<<" "<<"cubic inches"<<endl;
  36.     }
  37.  
  38.     else if (cin == 2)
  39.     {
  40.     cout<<"Please enter the radius of the wheel, radius of the rod, length of the rod, width of the wheel"<<endl;
  41.     cin>>radius>>rod>>length>>width;
  42.     cout<<"Axle: "<<endl;
  43.     cout<<"Radius of the wheel: "<<radius<<endl;
  44.     cout<<"Radius of the rod: "<<rod<<endl;
  45.     cout<<"Length of the rod: "<<length<<endl;
  46.     cout<<"Width of the wheel: "<<width<<endl;
  47.     vol = ((pie * (radius *radius) * length) * 2)
  48.     cout<<"The volume of the axel is "<<vol<<" "<<"cubic inches"<<endl;
  49.     }
  50.  
  51.     else if (cin==3)
  52.     {
  53.     cout<<"Please enter the radius (of rod), the length of the rod & heigth of the cone"<<endl;
  54.     cin>>radius>>length>>heigth;
  55.     cout<<"Spear: "<<endl;
  56.     cout<<"Radius of the rod: "<<radius<<endl;
  57.     cout<<"Length of the rod: "<<length<<endl;
  58.     cout<<"Height of the cone: "<<heigth<<endl;
  59.     vol = (1/3 * pie * (radius * radius) * heigth) * 2;
  60.     cout<<"The volume of the spear is "<<vol<<" "<<"cubic inches"<<endl;
  61.  
  62.  
  63.  
  64.  
  65.     return 0;
  66. }
  67.  
The assignment is as follows:

Write a C++ program that calculates the volume of 3 different geometric shapes. The 3 objects are shown below. 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.
Pie = 3.141 59
r= radius
l= height (or length)
Vol. of a sphere = 4/3 pie r3
Area of a circle = pie *(r*r)
Vol. of a cylinder= pie *(r*r) *h
Vol. of a cone = 1/3 pie*r2h
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.
Sep 30 '07 #1
6 4480
Studlyami
464 Expert 256MB
what are you having troubles with? I don't feel like reading all of your code, then the entire assignment and figure out what isn't working right.
Sep 30 '07 #2
Ganon11
3,652 Expert 2GB
Your cin statement is fauly. You can't cin to either '1' or '2' or '3' - you need to cin to an int variable, and then check the value of that variable.
Sep 30 '07 #3
Expand|Select|Wrap|Line Numbers
  1. #include <cmath>
  2. #include <fstream>
  3. #include <string>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8.  
  9.  
  10. int main()
  11. {
  12.  
  13.     int a,b,c;
  14.     int ht = 0;//height
  15.     int radB = 0;//radius of sphere
  16.     int radR = 0;//radius of rod
  17.     int radC = 0;//radius cone
  18.     int wt = 0;//width
  19.     int lt = 0;//length
  20.     int wh = 0;//radius of wheel
  21.     const double pie = 3.14159;//pie
  22.     double volsp = (4.0/3) * (radB * radB * radB) * pie;//volume of a sphere
  23.     double volcy = pie * (radR * radR) * ht;//volume of cylinder/rod
  24.     double volcn = (1.0/3) * (radC * radC) * pie * ht;//volume of cone
  25.  
  26.     cout << "Do you want the volume of:\n";
  27.     cout << "1. Dumbbell \n";
  28.     cout << "2. Axle \n";
  29.     cout << "3. Spear \n";
  30.  
  31.     cin >> a;
  32.         switch (a)
  33.         {
  34.             case 1:
  35.             cout<<"Enter radius of Sphere ";
  36.             cin>>radB;
  37.             cout<<"Enter radius of rod ";
  38.             cin>>radR;
  39.             cout<<"Enter length of rod ";
  40.             cin>>lt;
  41.             float dumbbell = (volcy) + (volsp * 2);
  42.             cout<<"The volume of the dumbbell is "<<dumbbell<<" cubic inches"<<endl;
  43.         }
  44.  
  45.     cin >>b;
  46.         switch (b)
  47.         {
  48.             case 2:
  49.             cout<<"Enter radius of wheel ";
  50.             cin>>wh;
  51.             cout<<"Enter radius of rod ";
  52.             cin>>radR;
  53.             cout<<"Enter length of rod ";
  54.             cin>>lt;
  55.             cout<<"Enter width of wheel ";
  56.             cin>>wt;
  57.  
  58.             double axle = (volsp * 3);
  59.             cout<<"The volume of the axle is "<<axle<<" cubic inches"<<endl;
  60.         }
  61.  
  62.     cin >> c;
  63.         switch (c)
  64.         {
  65.             case 3:
  66.             cout<<"Enter radius of rod ";
  67.             cin>>radR;
  68.             cout<<"Enter length of rod ";
  69.             cin>>lt;
  70.             cout<<"Enter height of cone ";
  71.             cin>>ht;
  72.  
  73.             double spear = (volcn * 2) + volcn;
  74.             cout<<"The volume of the spear is "<<spear<<"cubic inches"<<endl;
  75.         }
  76.  
  77.     return 0;
  78. }
  79.  
  80.  
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.
Oct 1 '07 #4
Ganon11
3,652 Expert 2GB
Let's fix your switch statements before we move on:

Right now, you have 3 switch statements, depending on 3 different variables. Each of these switch statements has only 1 case statement inside. You've effectively ruined the usefulness of switch in your program this way!

You know that you can put all three case statements inside the same switch statements, right? For instance,

Expand|Select|Wrap|Line Numbers
  1. cin >> a;
  2. switch (a) {
  3.    case 1:
  4.       // Do stuff...
  5.       break;
  6.    case 2:
  7.       // Do different stuff...
  8.       break;
  9.    case 3:
  10.       // Do even more different stuff...
  11.       break;
  12. }
That way, you don't need b or c, and you'll save a lot of room.

Also, your program looks very messy with all of those variables - especially when you're only going to be using 1 set of them at a time. Why not make these into functions? You can have a dumbbell() function which will find the volume of a dumbbell, an axle() function that will find the volume of an axle, and a spear() function that will find the volume of a spear. You can declare your variables inside these functions, saving you a lot of memory space (and making your code look much nicer, too). Once you have this figured out, you just have to do the math with the variables entered by the user in order to find the volumes.
Oct 1 '07 #5
Thanks, we haven't started the use of functions yet, so i'm not allowed to use it.
Oct 1 '07 #6
Thanks, we haven't started the use of functions yet, so i'm not allowed to use it.
Also, though not related to this, after I've completed this class for example and i don't see the need to be no longer apart of the forum, can I have my profile and the threads that i started deleted?
Oct 2 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: minjie | last post by:
Hello, I have several reports that were written with ADO shape command (in C++) to access Microsoft Access database. Now we have migrated all the data from Access to DB2 UDB (version 8.1), and the...
3
by: PM | last post by:
Hi, Im a student currently designing a game for a project. As part of my game, I want to be able to drag a shape over a series of panels. The shape needs to be able to be dropped on any of the...
0
by: nets-rac | last post by:
Hi, I use c# to automate powerpoint. I created a new shape with PowerPoint.Shape shape = slide.Shapes.AddTextbox(MsoTextOrientation.msoTextOrientationHorizontal, left, top, width, height);...
0
by: saravanansvks | last post by:
Dear Friends, I have prepared a programme in Visual Basic for SHAPE OPERATOR.But my coding are changing the shape only.It does not changes the fill style of the shape tool .And it is not giving ant...
1
by: JWest46088 | last post by:
I am a little confused about what I need to do here. I created four separate classes (Circle, Rectangle, Square, and Triangle) that each calculate their own area and perimeter via user input. Now...
5
by: The alMIGHTY N | last post by:
Hi all, Let's say I have a simple math formula: sum (x * y / 1000) / (sum z / 1000) I have to do this across 50 items, each with an x, y and z value, when the page first loads AND when a...
5
by: Peter Webb | last post by:
I have created two sets of circles using addshape, innercircle(i) and outercircle(i), and I want to group them in pairs so innercircle(1) and outercircle(1) are grouped, etc. I am supposed to be...
4
by: Linda Liu[MSFT] | last post by:
Hi Moondaddy, I downloaded your sample project and run it and did see the problem on my side. There're three problems in the source code of your project. 1. You should move the following...
12
by: ab12 | last post by:
I'm trying to write a program in C that gets a shape outlined with asterisks from the user, and returns that shape filled with asterisks. It will also get the coordinates of a point inside the shape...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.