Hey, I have a volume/surface area calculating program, but the problem is that my program always runs through the first possible choice instead of choosing between two. It will always do the cube/rectangle function instead of going to the square pyramid function.

Here's my code.

#include <iostream>
#include <windows.h>
using namespace std;
char choice[25];
char choice2[25];

int main(){
    system("Color 06");
    cout<<"                 **************************************\n"
          "                 *    Volume/Surface Area Calculator  *\n"
          "                 **************************************\n"
          "                 *                                    *\n"
          "                 *             1: Volume              *\n"
          "                 *             2: Surface Area        *\n"
          "                 *             3: Quit                *\n"
          "                 **************************************\n\n";
          
          cin.getline(choice,25);
          
          if (choice == "1" || "V" || "Volume")
          {
                     system("cls");
    cout<<"                 **************************************\n"
          "                 *    Volume/Surface Area Calculator  *\n"
          "                 **************************************\n"
          "                 *                                    *\n"
          "                 *             1: Cube/Rectangle      *\n"
          "                 *             2: Square Pyramid      *\n"
          "                 *             3: Sphere              *\n"
          "                 **************************************\n\n";
          cin.getline(choice2,25);
          }
          if (choice2 == "1" || "C" || "R" || "rectangle" || "cube")
          {
                     system("cls");
                     int length;
                     int width;
                     int height;
                     int volume;
    cout<<"                 **************************************\n"
          "                 *    Volume/Surface Area Calculator  *\n"
          "                 **************************************\n"
          "                 Enter your the length: ";
          cin>>length;
          cout<<endl<<"                 Enter your the width: ";
          cin>>width; 
          cout<<endl<<"                 Enter your the height: ";
          cin>>height;
          volume=length*width*height;
          cout<<endl<<"                 Volume of your object is: "<<volume;
                                        }
                                        
                                          if (choice2 == "2" || "S" || "P" || "pyramid" || "square")
          {
                     system("cls");
                     int base;
                     int height;
                     int volume;
                     
    cout<<"                 **************************************\n"
          "                 *    Volume/Surface Area Calculator  *\n"
          "                 **************************************\n"
          "                 Enter your the base length: ";
          cin>>base;
          cout<<endl<<"                 Enter your the height: ";
          cin>>height;
          volume=base*base*height*1/3;
          cout<<endl<<"                 Volume of your object is: "<<volume;
                                        }
                                        
          
          Sleep(3000);
          return 0;
          }

Thanks.

Recommended Answers

All 3 Replies

cause this always true

(choice == "1" || "V" || "Volume")

to make multi check

(choice == "1" || choice =="V" || choice =="Volume")

50 posts in C++ forum and you wrote if (choice == "1" || "V" || "Volume") and you expected your program to work?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.