1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Struct, File Process, Switch case help me...

Discussion in 'C++' started by noratiqah, Apr 10, 2011.

  1. noratiqah

    noratiqah New Member

    Joined:
    Feb 23, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <cstdlib>
    #include <iomanip>
    #include <cmath>
    
    using namespace std;
    
    int main ()
    
    {
    
    
    const double pi = 3.14159;
    
    
        char TypeOfShape;
        char TheVariables;
        double r, l, h ,w, x;
        double TheArea;
        double TheVolume;
        double TheCircumference;
        double TheHypotenuse;
        double ThePerimeter;
    
    for (;;){
    do {
    cout<<"Welcome to the geometry calculator world.\n";
    cout<<"Please choose the shape of geometry by entering the number, press q to quit\n";
    cout<<"1 - Circle\n";
    cout<<"2 - Triangle\n";
    cout<<"3 - Square\n";
    cout<<"4 - Cylinder\n";
    cout<<"5 - Cone\n";
    cout<<"6 - Sphere\n";
    
    
    
        cout << fixed << showpoint;
        cout << setprecision (2);
    
        cout << " This program compute solution of area or circumference or volume"
             << " of the shape. " << endl;
        cout << " Enter the shape that you want to calculate :";
        cin >> TypeOfShape;
        cout << endl;
    
    } while ( TypeOfShape < '1' || TypeOfShape > '7' && TypeOfShape != 'q');
    if (TypeOfShape == 'q') break;
    
    
    
        switch (TypeOfShape)
        {
        case '1':
            cout << " Enter the radius : ";
            cin >> r;
            cout << endl;
    
            cout << " Enter the type of calculation of the shape:"
                 << " A or a (Area),"
                 << " \n C or c (Circumference): ";
            cin >> TheVariables;
            cout << endl;
    
            switch (TheVariables)
            {
            case 'A':
            case 'a':
    
            TheArea = pi*r*r;
            cout << "The Area is:" << TheArea<< endl;
            break;
    
            case 'C':
            case 'c':
    
            TheCircumference = 2*pi*r;
            cout << "The Circumference is:" << TheCircumference << endl;
            break;
            }
    
        break;
    
    
        case '2':
            cout << " Enter the height and length separated by spaces : ";
            cin >> h >> l;
            cout << endl;
    
            cout << " Enter the type of calculation of the shape:"
                 << " H or h (Hypotenuse),"
                 << " \n A or a (Area): ";
            cin >> TheVariables;
            cout << endl;
    
    
            switch (TheVariables)
            {
            case 'A':
            case 'a':
    
            TheArea = 1/2 *h*l;
            cout << "The Area is:" << TheArea<< endl;
            break;
    
            case 'H':
            case 'h':
    
            x = pow(h,2)+ pow(l,2);
            TheHypotenuse = sqrt(x);
            cout << "The Hypotenuse is:" << TheHypotenuse << endl;
            break;
            }
        break;
    
        case '3':
            cout << " Enter the width and lenght separated by spaces : ";
            cin >> w >> l;
            cout << endl;
    
            cout << " Enter the type of calculation of the shape:"
                 << " A or a (Area),"
                 << " \n P or p (Perimeter): ";
            cin >> TheVariables;
            cout << endl;
    
            switch (TheVariables)
            {
            case 'A':
            case 'a':
    
                TheArea = w*l;
                cout << "The Area is:" << TheArea<< endl;
                break;
    
            case 'P':
            case 'p':
                ThePerimeter = (2*w)+(2*l);
                cout << "The Perimeter is:" << ThePerimeter << endl;
                break;
            }
    
        case '4':
            cout << " Enter the radius and height separated by spaces : ";
            cin >> r >> h;
            cout << endl;
    
            cout << " Enter the type of calculation of the shape:"
                 << " A or a (Surface Area),"
                 << " \n V or v (Volume): ";
            cin >> TheVariables;
            cout << endl;
    
            switch (TheVariables)
            {
            case 'A':
            case 'a':
    
                TheArea = 2*pi*r*h;
                cout << "The SurfaceArea is:" << TheArea<< endl;
                break;
    
            case 'V':
            case 'v':
                TheVolume = pi*r*h;
                cout << "The Volume is:" << TheVolume << endl;
            break;
            }
        break;
    
        case '5':
            cout << " Enter the radius and height separated by spaces : ";
            cin >> r >> h;
            cout << endl;
    
            cout << " Enter the type of calculation of the shape:"
                 << " A or a (Surface Area),"
                 << " \n V or v (Volume): ";
            cin >> TheVariables;
            cout << endl;
    
            switch (TheVariables)
            {
            case 'A':
            case 'a':
    
                TheArea = pi*r*h;
                cout << "The Area is:" << TheArea<< endl;
                break;
            case 'v':
            case 'V':
                TheVolume = 1/3 *pi* pow(r,2)*h;
                cout << "The Volume is:" << TheVolume << endl;
            break;
            }
        break;
    
        case '6':
            cout << " Enter the radius : ";
            cin >> r ;
            cout << endl;
    
            cout << " Enter the type of calculation of the shape:"
                 << " A or a (Surface Area),"
                 << " \n V or v (Volume): ";
            cin >> TheVariables;
            cout << endl;
    
            switch (TheVariables)
            {
            case 'A':
            case 'a':
    
                TheArea = 4*pi*pow(r,2);
                cout << "The Area is:" << TheArea<< endl;
            break;
    
            case 'v':
            case 'V':
                TheVolume = 4/3 *pi* pow(r,3)*h;
                cout << "The Volume is:" << TheVolume << endl;
            break;
            }
    
        break;
    
        default:
            cout << "invalid" << endl;
        }
    return 0;
    }}
    
    
    struct Geometry
    {
     string Name;
     string TypeOfShape;
     string TypeOfCalculation;
    
    };
    
    
    {
    
    
     ofstream outGeometry("Geometry1.txt", ios::out );
    
     if (!outGeometry)
     {
     cerr << "File could not be opened" << endl;
     exit(1);
     }
    
         cout << "\n\t\t\t*~*~*~*~*~*~*~*~*~*~*~*~*";
         cout << "\n\t\t\t|\t\t\t|";
         cout << "\n\t\t\t| Geometry is fun!! ^.^ |";
         cout << "\n\t\t\t|\t\t\t|";
         cout << "\n\t\t\t*~*~*~*~*~*~*~*~*~*~*~*~*" << endl;
    
    
       struct Geometry Details;
    
     {
      cout<<"Welcome to the geometry calculator world.\n";
      cout<<"Please choose the shape of geometry by entering the number, for record just type the name of shape press q to quit\n";
      cout<<"1 - Circle\n";
      cout<<"2 - Triangle\n";
      cout<<"3 - Square\n";
      cout<<"4 - Cylinder\n";
      cout<<"5 - Cone\n";
      cout<<"6 - Sphere\n";
      cout << endl;
    
      cout << "Name: ";
      cin >> Details.Name ;
    
      cout << "Type of Shape: ";
      cin >> Details.TypeOfShape;
    
      cout << "What do you want to calculate:";
      cin >> Details.TypeOfCalculation;
    
    
    
      outGeometry << "Name: " << Details.Name << "\nType of Shape: " << Details.TypeOfShape << "\nWhat do you want to calculate: " << Details.TypeOfCalculation
       <<  endl;
     }
    
    }
    
    


    when im running this program there is a error and i dunno how to handle it ..
     
    Last edited by a moderator: Apr 10, 2011

Share This Page