c++ shape calculation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zandiago
    New Member
    • Jul 2007
    • 19

    c++ shape calculation

    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>>heigth>>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) * heigth) * 2;
    	cout<<"The volume of the spear is "<<vol<<" "<<"cubic inches"<<endl;
    
    	
    
    
    	return 0;
    }
    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.
  • Studlyami
    Recognized Expert Contributor
    • Sep 2007
    • 464

    #2
    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.

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      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.

      Comment

      • zandiago
        New Member
        • Jul 2007
        • 19

        #4
        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.

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          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,

          [CODE=cpp]cin >> a;
          switch (a) {
          case 1:
          // Do stuff...
          break;
          case 2:
          // Do different stuff...
          break;
          case 3:
          // Do even more different stuff...
          break;
          }[/CODE]

          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.

          Comment

          • zandiago
            New Member
            • Jul 2007
            • 19

            #6
            Thanks, we haven't started the use of functions yet, so i'm not allowed to use it.

            Comment

            • zandiago
              New Member
              • Jul 2007
              • 19

              #7
              Originally posted by zandiago
              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?

              Comment

              Working...