Tried to type out a code for volume calculations

Hi, I am a student who just started with C++ coding. Trying it out on my free time. As I have just started recently, I am decent at the basics of C++ coding, but I do not know how to simplify codes. (I am using codeblocks)

My codes do work out, but some of them are repeating itself often.
Is there any way to shorten the code?

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
 #include <iostream>

using namespace std;


int main()
{
    //VOLUME CALCULATOR
    float Breadth;
    float Length;
    float Height;

    string NameofSolid;
    cout << "Please key in the name of solid.\n";
    cin >> NameofSolid;

    if(NameofSolid == "Cube" || NameofSolid == "cube"){
        cout << "Please key in ONE of the dimensions given/shown.\n";
        cin >> Breadth;
        while(Breadth <= 0){
        cout << "This value cannot be used for calculations.\n";
        cout << "Please key in a number greater than 0.\n";
        cin >> Breadth;
        };
        float LengthCube = Breadth;
        float HeightCube = Breadth;
        float volume = Breadth * LengthCube * HeightCube;
        cout << "The volume of solid is " << volume << ".\n";
    }else if(NameofSolid == "Cylinder" || NameofSolid == "cylinder"){
        string TypeofBase;
        cout << "The solid has a _______ base.\n";
        cin >> TypeofBase;
        cout << "Please key in the dimensions below:\n";
        cout << "Base Breadth/Radius:\n";
        cin >> Breadth;
        while(Breadth <= 0){
        cout << "This value cannot be used for calculations.\n";
        cout << "Please key in a number greater than 0.\n";
        cin >> Breadth;
        };
        cout << "Base Length/Radius:\n";
        cin >> Length;
        while(Length <= 0){
        cout << "This value cannot be used for calculations.\n";
        cout << "Please key in a number greater than 0.\n";
        cin >> Length;
        };
        cout << "Height of Solid:\n";
        cin >> Height;
        while(Height <= 0){
        cout << "This value cannot be used for calculations.\n";
        cout << "Please key in a number greater than 0.\n";
        cin >> Height;
        };
            if(TypeofBase == "Square" || TypeofBase == "square"){
                float SQBaseA = Breadth * Length;
                float SQBaseV = SQBaseA * Height;
                cout << "The volume of the cylinder is " << SQBaseV << ".\n";
            }else if(TypeofBase == "triangle" || TypeofBase == "Triangle"){
                float TriangularBaseA = Breadth * Length / 3;
                float TriangularCylinderV = TriangularBaseA * Height;
                cout << "The volume of solid is " << TriangularCylinderV << ".\n";
            }else if(TypeofBase == "Circle" || TypeofBase == "circle"){
                float CirBaseA = 3.1415 * Breadth * Length;
                float CirBaseV = CirBaseA * Height;
                cout << "The volume of the cylinder is " << CirBaseV << ".\n";
            }
    }else if(NameofSolid == "Sphere" || NameofSolid == "sphere"){
        float RadiusofSphere;
        RadiusofSphere = Breadth;
        cout << "Please key in the radius.\n";
        cin >> RadiusofSphere;
        while(RadiusofSphere <= 0){
        cout << "This value cannot be used for calculations.\n";
        cout << "Please key in a number greater than 0.\n";
        cin >> RadiusofSphere;
        }
        float VolumeofSphere = RadiusofSphere * RadiusofSphere * RadiusofSphere * 4 / 3 * 3.1415;
        cout << "The volume of sphere " << VolumeofSphere << ".\n";
    }else{
        cout << "You don't know your shapes, do you?" << endl;
        cout << "You have entered invalid shapes...\n";
    }



    return 0;
}
Basically, any time you're reaching for the ctrl-c / ctrl-v combination on your keyboard, you should be thinking about a function.

> if(NameofSolid == "Cube" || NameofSolid == "cube")
If you convert the string to lowercase to begin with, you reduce the number of conditionals to test.
That also protects you from typos like "CUbe" as well.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
        cout << "Base Breadth/Radius:\n";
        cin >> Breadth;
        while(Breadth <= 0){
        cout << "This value cannot be used for calculations.\n";
        cout << "Please key in a number greater than 0.\n";
        cin >> Breadth;
        };
        cout << "Base Length/Radius:\n";
        cin >> Length;
        while(Length <= 0){
        cout << "This value cannot be used for calculations.\n";
        cout << "Please key in a number greater than 0.\n";
        cin >> Length;
        };
        cout << "Height of Solid:\n";
        cin >> Height;
        while(Height <= 0){
        cout << "This value cannot be used for calculations.\n";
        cout << "Please key in a number greater than 0.\n";
        cin >> Height;


Perhaps
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int getPositiveNumber(string prompt) {
  cout << prompt;
  int result;
  cin >> result;
  while ( result < 0 ) {
    cout << "This value cannot be used for calculations.\n";
    cout << "Please key in a number greater than 0.\n";
    cin >> result;
  }
  return result;
}

// Then main becomes

        Breadth = getPositiveNumber("Base Breadth/Radius:\n");
        Length = getPositiveNumber("Base Length/Radius:\n");
        cin >> Height = getPositiveNumber("Height of Solid:\n");

Hi, thank you for taking your time off to help me with this.

I had tried to write a new code over the last few days. Please feel free to comment on them if you can.

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>

using namespace std;

//the pin is 752634
//can withdraw or deposit only
int bankbalance(){
    int pinNo;
    cout << "Please key in your 6 digits PIN number.\n";
    cin >> pinNo;
    int Ttries = 3; //total number of tries
    int Utries = 1; //counting with respect to the first user input
    while(pinNo != 752634 && Ttries - Utries != 0){
        cout << "You have enter an invalid PIN number.\n";
        cout << "Please try again.\n";
        Utries++;
        cin >> pinNo;
    }
    if(pinNo != 752634 && Ttries - Utries == 0){
        cout << "You have no more chances.\n";
        cout << "Your accound has been locked.\n";
        cout << "Please re-set your PIN before trying.\n";
    }

    return pinNo;
}

float amountleft(float moneytrans){ //moneytrans == money transaction
    float initialamt;
    initialamt = 90678; //amount of money one has in the account
    while(initialamt - moneytrans <= 0){
        cout << "ERROR\n";
        cout << "You cannot withdraw that amount.\n";
        cout << "\tThis is because your savings is less than amount to be withdrawn.\n";
        cout << "Please key in a new amount you wish to withdraw.\n";
        cin >> moneytrans;
        if(initialamt - moneytrans >= 0){
        cout << "Withdrawal successful." << endl;
        cout << "You have " << initialamt - moneytrans << endl;
        break;
    }
    float finalamt = initialamt - moneytrans;

return finalamt;
}

}
float amtincreased(float moneydep){
    float initialamt;
    initialamt = 90678;
    float finalamt = initialamt + moneydep;
    cout << "You have " << finalamt << " in your current account.\n";

    return finalamt;
}

int main()
{
    if(bankbalance() == 752634){
        cout << "You have key in the correct PIN numbers.\n";
    }else {
        return 0;
    }
    cout << "Please proceed with your transactions.\n";
    cout << "Please 1 of the following options:\n";
    cout << "1. Withdrawal\n \b2. Deposit\n";
    int Typeoftransaction;
    cin >> Typeoftransaction;
    if(Typeoftransaction == 1){
        cout << "Please key in the amount you wish to withdraw.\n";
        float amountwith;
        cin >> amountwith;
        amountleft(amountwith);
    }else if(Typeoftransaction == 2){
        cout << "Please key in the amount you wish to deposit.\n";
        float amountdep;
        cin >> amountdep;
        amtincreased(amountdep);
    }

    return 0;
}



As I am studying on my own, it is hard for me to determine which is the correct method. There are tons of advanced and complex answers online, which I cannot determine which is the right answer to my questions. I have entered them below for clarifications purposes.
I have the following questions:

1) The parentheses are meant for limits and boundary.
 
int bankbalance()

If I did not place any limits in it, it means that the code will just run on its own when called upon? When is it a good time to place limits and when is it not?

2) If I were to have a few functions, the name within each function can they be named the same? Why or why not?

3) How do choose between return functions and void functions? I know that return functions mean that you will get back a result, but you won't get one for void functions.

4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

int question4(){
    int qns4;
    cout << "Please key in a number.\n";
    cin >> qns4;

return qns4;
}

int main()
{

    cout << question4();
    return 0;
}



Is it possible to just get the value of qns4 instead of running the whole line of code? I am not so sure about the advanced codings just yet. But as I was trying to type out the new codes above, this qns kind of struck me.
Topic archived. No new replies allowed.