Volume part of calculator not working

Right now I am programming a calculator, and I am on to geometry. I have the user able to choose if they want area or volume. If the user chooses area, everything works fine. If the user chooses volume, the program will act as if it ended and follows through with my "Do you want to continue[Y/N]" loop.

Note: geometry code starts at line 101.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213`` `````` #include #include using namespace std; int main(int argc, char *argv[]) { char c; do{ string type; string oper; string shape; string gtype; //Basic math terms float a; float b; float answ; //Geometry float base; float height; float side; float area; float base1; float base2; float length; float width; //To compile with Terminal, use the "g++ calculator.cpp -o calculator" command cout << endl << "Basic math = bm, Geometry = g, Square root = s. Power = p. Type end for the program to end."; cin >> type; //End program code if(type=="end") { cout << "Bye!"; return 0; } //Specifys +,-,*, and / if(type=="bm") { cout << endl << "Addition, Subtraction, Multiplication, Division. Your choice? "; cin >> oper; if(oper=="addition") { cout << "Enter a number: "; cin >> a; cout << "A number to add to " << a << ": "; cin >> b; answ = a + b; cout << endl << "Your sum is " << answ << endl; } else if(oper=="subtraction") { cout << "Enter a number: "; cin >> a; cout << "A number to subtract from " << a << ": "; cin >> b; answ = a - b; cout << endl << "Your difference is " << answ << endl; } else if(oper=="multiplication") { cout << "Enter a number: "; cin >> a; cout << "A number to multiply " << a << " by: "; cin >> b; answ = a * b; cout << endl << "Your product is " << answ << endl; } else if(oper=="division") { cout << "Enter a number:"; cin >> a; cout << "Enter a number to divide " << a << " by: "; cin >> b; answ = a / b; cout<< endl << "Your quotient is " << answ << endl; } else { cout << "Invalid input" << endl; } //Ends basic math code } //Starts geometry if(type=="g") { cout << "Do you want to find the area or volume of a shape? "; cin >> gtype; if(gtype=="area"){ cout << "Do you want to find the area of a square, a rectangle, a trapezoid, a parallelogram, or a triangle?"; cin >> shape; if(shape=="square"){ cout << "Enter the length of one side: "; cin >> side; answ = side * side; cout << "The area of the square is: " << answ; } else if(shape=="rectangle"){ cout << "Enter the base: "; cin >> base; cout << "Enter the height: "; cin >> height; answ = base * height; cout << "The area of the rectangle is " << answ; } else if(shape=="trapezoid"){ cout << "Enter base one: "; cin >> base1; cout << "Enter base two: "; cin >> base2; cout << "Enter the height: "; cin >> height; answ = .5 * (base1+base2) * height; cout << "The area of the trapezoid is " << answ; } else if(shape=="parallelogram"){ cout << "Enter the base: "; cin >> base; cout << "Enter the height: "; cin >> height; answ = base * height; cout << "The area of your parallelogram is " << answ; } else if(shape=="triangle"){ cout << "Enter the base: "; cin >> base; cout << "Enter the height: "; cin >> height; answ = .5*(base*height); cout << "The area of your triangle is " << answ; } else { cout << "Invalid input" << endl; } if(gtype=="volume"){ cout << "Do you want to find the volume of a rectangular prism, or a cube? "; cin >> shape; if(shape=="rectangular prism"){ cout << "Enter the length: "; cin >> length; cout << "Enter the width: "; cin >> width; cout << "Enter the height: "; answ = length*width*height; cout << "The volume of the rectangular prism is " << answ << " ."; } //Ends geometry code } } } //Starts square root code if(type=="s"){ cout << "What number do you want to see the square root of? "; cin >> a; answ = sqrt(a); cout << "The square root of " << a << " is " << answ << " ."; } if(type=="p"){ cout << "Enter the base: "; cin >> a; cout <<"Enter the power: "; cin >> b; cout << a << " to the power of " << b << " is " << pow(a,b) << " ."; } cout<< endl <<"Do you want to continue (Y/N)?" << endl; cin>>c ; }while(c=='y'||c=='Y'); if(c == 'n'){ cout << "Program ended" << endl; } }``````
You don't close your if statement for the first gtype evaluation on line 109 before the statement on 168.

So the if statement on line 168 to see if the user inputted "volume" is inside the if statement for area. So it will never evaluate to true.
you have your braces messed up. your code should look like this comments where braces are changed

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212`` ``````#include #include #include using namespace std; int main(int argc, char *argv[]) { char c; do{ string type; string oper; string shape; string gtype; //Basic math terms float a; float b; float answ; //Geometry float base; float height; float side; float area; float base1; float base2; float length; float width; //To compile with Terminal, use the "g++ calculator.cpp -o calculator" command cout << endl << "Basic math = bm, Geometry = g, Square root = s. Power = p. Type end for the program to end."; cin >> type; //End program code if(type=="end") { cout << "Bye!"; return 0; } //Specifys +,-,*, and / if(type=="bm") { cout << endl << "Addition, Subtraction, Multiplication, Division. Your choice? "; cin >> oper; if(oper=="addition") { cout << "Enter a number: "; cin >> a; cout << "A number to add to " << a << ": "; cin >> b; answ = a + b; cout << endl << "Your sum is " << answ << endl; } else if(oper=="subtraction") { cout << "Enter a number: "; cin >> a; cout << "A number to subtract from " << a << ": "; cin >> b; answ = a - b; cout << endl << "Your difference is " << answ << endl; } else if(oper=="multiplication") { cout << "Enter a number: "; cin >> a; cout << "A number to multiply " << a << " by: "; cin >> b; answ = a * b; cout << endl << "Your product is " << answ << endl; } else if(oper=="division") { cout << "Enter a number:"; cin >> a; cout << "Enter a number to divide " << a << " by: "; cin >> b; answ = a / b; cout<< endl << "Your quotient is " << answ << endl; } else { cout << "Invalid input" << endl; } //Ends basic math code } //Starts geometry if(type=="g") { cout << "Do you want to find the area or volume of a shape? "; cin >> gtype; if(gtype=="area"){ cout << "Do you want to find the area of a square, a rectangle, a trapezoid, a parallelogram, or a triangle?"; cin >> shape; if(shape=="square"){ cout << "Enter the length of one side: "; cin >> side; answ = side * side; cout << "The area of the square is: " << answ; } else if(shape=="rectangle"){ cout << "Enter the base: "; cin >> base; cout << "Enter the height: "; cin >> height; answ = base * height; cout << "The area of the rectangle is " << answ; } else if(shape=="trapezoid"){ cout << "Enter base one: "; cin >> base1; cout << "Enter base two: "; cin >> base2; cout << "Enter the height: "; cin >> height; answ = .5 * (base1+base2) * height; cout << "The area of the trapezoid is " << answ; } else if(shape=="parallelogram"){ cout << "Enter the base: "; cin >> base; cout << "Enter the height: "; cin >> height; answ = base * height; cout << "The area of your parallelogram is " << answ; } else if(shape=="triangle"){ cout << "Enter the base: "; cin >> base; cout << "Enter the height: "; cin >> height; answ = .5*(base*height); cout << "The area of your triangle is " << answ; } }//CLOSES AREA <<<<<<<<< THIS FIXES PROBLEM CLOSES AREA IF else { cout << "Invalid input" << endl; } if(gtype=="volume"){ cout << "Do you want to find the volume of a rectangular prism, or a cube? "; cin >> shape; if(shape=="rectangular prism"){ cout << "Enter the length: "; cin >> length; cout << "Enter the width: "; cin >> width; cout << "Enter the height: "; answ = length*width*height; cout << "The volume of the rectangular prism is " << answ << " ."; } //Ends geometry code }//CLOSES VOLUME }//CLOSES GEOMETRY //} <<<<<> a; answ = sqrt(a); cout << "The square root of " << a << " is " << answ << " ."; } if(type=="p"){ cout << "Enter the base: "; cin >> a; cout <<"Enter the power: "; cin >> b; cout << a << " to the power of " << b << " is " << pow(a,b) << " ."; } cout<< endl <<"Do you want to continue (Y/N)?" << endl; cin>>c ; }while(c=='y'||c=='Y'); if(c == 'n'){ cout << "Program ended" << endl; } }``````
Thanks, that fixed the volume problem.

However, when I choose rectangular prism, the program will say "Invalid Input" but will come up with the

"Do you want to find the volume of a rectangular prism, or a cube? "

After choosing rectangular prism, the program says "Do you want to continue (Y/N)?" and then ends immediately.

@tag

That didn't seem to make a difference. The rectangular prism part is still broken.
Last edited on
Figured it out! The rectangular prism was broken because a string can only contain one word(no spaces)

Thanks
Topic archived. No new replies allowed.