JohnOblak.com
Web Development Laboratory

Back to HOME
 

 

Tired of Sudoku? C++ Programming is a great way to keep your
mind active and solve some calculations along the way. This
program determines the dimensions of a speaker enclosure based
on the volume the speaker needs. Plenty of STDs here (see code).

/* boxdimen3.cpp 
   This is a C++ program I completed on 27 April 2009.
   The pgm computes the dimensions of a speaker box based on the volume required by
   the speaker. The user inputs the volume in either cubic feet or cubic liters.
   This pgm is an expanded version of a BASIC pgm found in David B Weems book "Designing, 
   Building, and Testing Your Own Speaker System" 4th ed, 1997, McGraw-Hill, Page 104. The
   book had a printed copy of the C program, not a digital copy. The reader had to type in
   the 38-line program. I chose to learn C++ and create this 300-line program.
*/
#include  // needed for cout and cin
#include  // needed to be able to use strings
#include  // for pow(a, b) function (argument a raised to the power of argument b) 
void DisplayConversionChoices(); // prototype for this function

int main() {
    std::cout << "This program computes Speaker Box Dimensions. There are two sections: \n";
    std::cout << "    Section 1 provides various volume conversion calculations. \n";
    std::cout << "    Section 2 provides the dimensions for three speaker boxes (Normal, \n";
    std::cout << "        Fat, and Tall) based on the volume requirement you enter. \n";
    std::cout << "\n"; // prints a blank line

    float cu_ft_inp12; // four Calculation INPUT variables 
    float cu_in_inp3; //..
    float cu_lt_inp45; //..
    float cu_cm_inp6; //..

    std::string conversion_from; //  input of Cubic Feet, Cubic Liters, etc
    std::string conversion_to;  //  output of Cubic Feet, Cubic Liters, etc     

    std::cout << "..............1. VOLUME CONVERSION SECTION..............\n";    

    DisplayConversionChoices();

    char   conversion_type = 0; // set to "0" to start with
    char  i_am_done = 'N'; // value used to end the while section

          std::cout << "Choose a NUMBER (1 to 7), and press Enter:  ";
          std::cin >> conversion_type;
          std::cin.ignore(100, '\n'); // clears the input buffer of 100 chars
    while (i_am_done == 'N') {    
          switch (conversion_type) {
                 case '1': // conversion_type = 1
                      conversion_from = "Cubic Feet";
                      conversion_to = "Cubic Liters.";
                      std::cout << "You chose type 1. Convert " << conversion_from << " to " << conversion_to << " \n";
                      std::cout << "\n"; // prints a blank line                      
                      std::cout << "Enter value for " << conversion_from << " and press Enter.  ";
                      std::cin >> cu_ft_inp12;
                      std::cin.ignore(100, '\n'); // clears the input buffer of 100 chars
                      std::cout << "\n"; // prints a blank line                      
                      std::cout << "                 Here is the ANSWER: \n";                      
                      std::cout << "    " << cu_ft_inp12 << " " << conversion_from << " equals " 
                          << ((cu_ft_inp12 * 28.31687)) << " " << conversion_to << " \n"; 
                      DisplayConversionChoices();
                      std::cout << "Choose a NUMBER (1 to 7), and press Enter:  ";
                      std::cin >> conversion_type;
                      std::cin.ignore(100, '\n'); // clears the input buffer of 100 chars
                 break;
                 case '2': // conversion_type = 2
                      conversion_from = "Cubic Feet";
                      conversion_to = "Cubic Inches.";
                      std::cout << "You chose type 2. Convert " << conversion_from << " to " << conversion_to << " \n";
                      std::cout << "\n"; // prints a blank line                      
                      std::cout << "Enter value for " << conversion_from << " and press Enter.  ";
                      std::cin >> cu_ft_inp12;
                      std::cin.ignore(100, '\n'); // clears the input buffer of 100 chars                      
                      std::cout << "\n"; // prints a blank line                      
                      std::cout << "                 Here is the ANSWER: \n";                      
                      std::cout << "    " << cu_ft_inp12 << " " << conversion_from << " equals " 
                          << ((cu_ft_inp12 * 1728)) << " " << conversion_to << " \n"; 
                      DisplayConversionChoices();
                      std::cout << "Choose a NUMBER (1 to 7), and press Enter:  ";
                      std::cin >> conversion_type;
                      std::cin.ignore(100, '\n'); // clears the input buffer of 100 chars                      
                 break;
                 case '3': // conversion_type = 3
                      conversion_from = "Cubic Inches";
                      conversion_to = "Cubic Feet.";
                      std::cout << "You chose type 3. Convert " << conversion_from << " to " << conversion_to << " \n";
//                      std::cout << "If this is not correct, press CTRL/c to exit and start over.\n";
                      std::cout << "\n"; // prints a blank line                      
                      std::cout << "Enter value for " << conversion_from << " and press Enter.  ";
                      std::cin >> cu_in_inp3;
                      std::cin.ignore(100, '\n'); // clears the input buffer of 100 chars                      
                      std::cout << "\n"; // prints a blank line                      
                      std::cout << "                 Here is the ANSWER: \n";                      
                      std::cout << "    " << cu_in_inp3 << " " << conversion_from << " equals " 
                          << ((cu_in_inp3 / 1728)) << " " << conversion_to << " \n"; 
                      DisplayConversionChoices();
                      std::cout << "Choose a NUMBER (1 to 7), and press Enter:  ";
                      std::cin >> conversion_type;
                      std::cin.ignore(100, '\n'); // clears the input buffer of 100 chars                      
                 break;
                 case '4': // conversion_type = 4
                      conversion_from = "Cubic Liters";
                      conversion_to = "Cubic Feet";
                      std::cout << "You chose type 4. Convert " << conversion_from << " to " << conversion_to << " \n";
                      std::cout << "\n"; // prints a blank line                      
                      std::cout << "Enter value for " << conversion_from << " and press Enter.  ";
                      std::cin >> cu_lt_inp45;
                      std::cin.ignore(100, '\n'); // clears the input buffer of 100 chars                      
                      std::cout << "\n"; // prints a blank line                      
                      std::cout << "                 Here is the ANSWER: \n";                      
                      std::cout << "    " << cu_lt_inp45 << " " << conversion_from << " equals " 
                          << ((cu_lt_inp45 * 0.035314638)) << " " << conversion_to << " \n"; 
                      DisplayConversionChoices();
                      std::cout << "Choose a NUMBER (1 to 7), and press Enter:  ";
                      std::cin >> conversion_type;
                      std::cin.ignore(100, '\n'); // clears the input buffer of 100 chars                      
                 break;
                 case '5': // conversion_type = 5
                      conversion_from = "Cubic Liters";
                      conversion_to = "Cubic Centimeters.";
                      std::cout << "You chose type 5. Convert " << conversion_from << " to " << conversion_to << " \n";
//                      std::cout << "If this is not correct, press CTRL/c to exit and start over.\n";
                      std::cout << "\n"; // prints a blank line                      
                      std::cout << "Enter value for " << conversion_from << " and press Enter.  ";
                      std::cin >> cu_lt_inp45;
                      std::cin.ignore(100, '\n'); // clears the input buffer of 100 chars                      
                      std::cout << "\n"; // prints a blank line                      
                      std::cout << "                 Here is the ANSWER: \n";                      
                      std::cout << "    " << cu_lt_inp45 << " " << conversion_from << " equals " 
                          << ((cu_lt_inp45 * 1000)) << " " << conversion_to << " \n"; 
                      DisplayConversionChoices();
                      std::cout << "Choose a NUMBER (1 to 7), and press Enter:  ";
                      std::cin >> conversion_type;
                      std::cin.ignore(100, '\n'); // clears the input buffer of 100 chars                      

                 break;                                                  
                 case '6': // conversion_type = 6
                      conversion_from = "Cubic Centimeters";
                      conversion_to = "Cubic Liters.";
                      std::cout << "You chose type 6. Convert " << conversion_from << " to " << conversion_to << " \n";
                      std::cout << "\n"; // prints a blank line                      
                      std::cout << "Enter value for " << conversion_from << " and press Enter.  ";
                      std::cin >> cu_cm_inp6;
                      std::cin.ignore(100, '\n'); // clears the input buffer of 100 chars                      
                      std::cout << "\n"; // prints a blank line                      
                      std::cout << "                 Here is the ANSWER: \n";                      
                      std::cout << "    " << cu_cm_inp6 << " " << conversion_from << " equals " 
                          << ((cu_cm_inp6 / 1000)) << " " << conversion_to << " \n"; 
                      DisplayConversionChoices();
                      std::cout << "Choose a NUMBER (1 to 7), and press Enter:  ";
                      std::cin >> conversion_type;
                      std::cin.ignore(100, '\n'); // clears the input buffer of 100 chars                      
                 break;                                                                   
                 case '7': // conversion_type = 7
                      i_am_done = 'Y'; // user chose 7, no more conversions
                      std::cout << "You chose 7. DONE WITH CONVERSIONS [7]\n";
                      std::cout << "\n"; // prints a blank line                      
                      std::cout << "..............END OF VOLUME CONVERSION SECTION..............\n";
                      std::cout << "\n"; // prints a blank line                      
                      std::cout << "    Press Enter to CONTINUE TO THE NEXT SECTION."; // pauses pgm
                      std::cin.get(); // continues upon Enter input        
                      std::cout << "\n"; // prints a blank line                                 
                      std::cout << "\n"; // prints a blank line                                 
                      std::cout << "\n"; // prints a blank line                                 
                      std::cout << "\n"; // prints a blank line                                                                                                                         
                 break;                                                                        
           default: // conversion_type DOES NOT EQUAL 1 THRU 7
                      std::cout << "\n"; // prints a blank line                                 
                      std::cout << "INPUT ERROR: You did not enter a valid number (1-7).\n";
                      std::cout << "          You entered: " << conversion_type << " \n";                                       
                      std::cout << "\n"; // prints a blank line                      
                      std::cout << "    Press Enter and then enter a valid number.\n"; // pauses pgm
                      std::cin.get(); // continues upon Enter input                      
                      DisplayConversionChoices();
                      std::cout << "Choose a NUMBER (1 to 7), and press Enter:  ";
                      std::cin >> conversion_type;
                      std::cin.ignore(100, '\n'); // clears the input buffer of 100 chars                                            
                 break;
          } // end of switch section
    } // end of while section
    char  nbr_type; // C for cubic feet, L for cubic liter     NEEDED?????
    char  nbr_type_set = 'N'; // Y/N switch 
    char  vol_type = 'P'; // sets a value of "P"
    std::string print_type;
    std::string ruler_type;

    std::cout << "..............2. SPEAKER DIMENSION SECTION..............\n";        
    std::cout << "\n"; // prints a blank line
    while (nbr_type_set == 'N') {
          std::cout << "You can enter the volume in cubic feet (C) or cubic liters (L).\n";
          std::cout << "\n"; // prints a blank line
          std::cout << "Make your choice by typing 'C' or 'L', and press Enter:  ";
          std::cin >> nbr_type;
          std::cout << "\n"; // prints a blank line
          switch (nbr_type) {
                 case 'C':
                 case 'c':
                      nbr_type_set = 'Y';
                      vol_type = 'C';
                      print_type = "cubic feet";
                      ruler_type = " inches. \n";
                      std::cout << "You have chosen to enter " << print_type << " for the volume required.\n";
                      std::cout << "\n"; // prints a blank line                      
                 break;
                 case 'L':
                 case 'l':
                      nbr_type_set = 'Y';
                      vol_type = 'L';
                      print_type = "cubic liters";
                      ruler_type = " centimeters. \n";
                      std::cout << "You have chosen to enter " << print_type << " for the volume required.\n";                      
                      std::cout << "\n"; // prints a blank line
                 break;
           default:
                       std::cout << "You did not enter a 'C' (c) for cubic feet, or 'L' (l) for cubic liters.\n";
                 break;
          } // end of switch for to choose "C" or "L"
    } // end of while looking for either "C" or "L"
    std::cin.ignore(100, '\n'); // clears the input buffer of 100 chars                      
    float volume_entered; 
    float depth_multiplier, height_multiplier;

/*  the following two lines do not validate the input number.
  1. std::cout << "Type the volume (in " << print_type << ") and press Enter:  ";
  2. std::cin >> volume_entered; // input of volume number taken here
    The six code lines below validate the input number for the float variable.
    An input of a letter or other non-number character would cause problems.
*/
    std::cout << "Type the volume (in " << print_type << ") and press Enter:  ";
    while ((!(std::cin >> volume_entered)) ||
          (volume_entered <= 0)) {
      std::cin.clear();
      std::cin.ignore(100, '\n');
      std::cout << "Please enter a valid number using 0-9 and possibly a decimal point:  ";
} // end of while 
    std::cout << "\n"; // prints a blank line
    
    float cf_x1728eq_ci = 1728; // multiplier for cubic feet to cubic inches
    float culiter_x1000eq_cucm = 1000; // multiplier for cubic liters to cubic centimeters    
    float cubic_inORcm;

    if (vol_type == 'L')       {
          cubic_inORcm = volume_entered * culiter_x1000eq_cucm; // cu liter X 1000 = cu cm
    }  else  {
          cubic_inORcm = volume_entered * cf_x1728eq_ci; // cu ft X 1728 = cu in
    }
    std::cout << "Your input of " << volume_entered << " " << print_type << " was used to create the dimensions \n";    
    std::cout << "of three speaker boxes. Be ready to COPY DOWN the dimensions. \n";
    std::cin.ignore(100, '\n'); // clears the input buffer of 100 chars                          
    std::cout << "    Press Enter to see the dimensions.\n"; // pauses pgm
    std::cin.get(); // continues upon Enter input     
       
    std::cout << ".....NORMAL SPEAKER BOX............ \n";
    std::cout << "Box Ratio is D=0.62:W=1:H=1.61  (almost 3:5:8)\n";        
    std::cout << "This is the Golden Ratio used since the Egyptian Pyramids \n";            

    float nor_width, nor_depth, nor_height;
    nor_width = pow((cubic_inORcm), .3333); // this value was supplied by Weems
    nor_depth = nor_width * .62;
    nor_height = nor_width * 1.61; // more accurate than 1.62 in Weems book p.104
    std::cout << "   Width is " << nor_width << ruler_type;
    std::cout << "   Depth is " << nor_depth << ruler_type; 
    std::cout << "   Height is " << nor_height << ruler_type;
    std::cout << "\n"; // prints a blank line                    
    
    std::cout << ".....FAT SPEAKER BOX........Ratio is D=4:W=5:H=6......\n";          

    float fat_width, fat_depth, fat_height;
    fat_width = pow((cubic_inORcm), .335); // this value was supplied by Weems
    fat_depth = fat_width * .8;
    fat_height = fat_width * 1.2;
    std::cout << "   Width is " << fat_width << ruler_type;
    std::cout << "   Depth is " << fat_depth << ruler_type; 
    std::cout << "   Height is " << fat_height << ruler_type;        
    std::cout << "\n"; // prints a blank line         
                     
    std::cout << ".....TALL SPEAKER BOX.......Ratio is D=6:W=9:H=16......\n";

    float tall_width, tall_depth, tall_height;
    tall_width = pow((cubic_inORcm), .3267); // this value was supplied by Weems
    tall_depth = tall_width * .67;
    tall_height = tall_width * 1.8;
    std::cout << "   Width is " << tall_width << ruler_type;
    std::cout << "   Depth is " << tall_depth << ruler_type;
    std::cout << "   Height is " << tall_height << ruler_type;        

    std::cout << "******************************************************** \n"; 
    std::cout << "*                                                      * \n";     
    std::cout << "*       MAKE NOTE OF THE DIMENSIONS LISTED ABOVE       * \n"; 
    std::cout << "*                                                      * \n";     
    std::cout << "******************************************************** \n";           
    std::cout << "END OF THE PROGRAM. Press Enter to close. \n";    
    std::cin.get(); // continues upon Enter input        
    return 0; // the zero indicates no problems
} // end of main() function
void DisplayConversionChoices()  {
    std::cout << "\n"; // prints a blank line
    std::cout << "These six different conversions are available: \n";
    std::cout << "    1. Convert CUBIC FEET   to CUBIC LITERS [1]\n";
    std::cout << "    2. Convert CUBIC FEET   to CUBIC INCHES [2]\n";
    std::cout << "    3. Convert CUBIC INCHES to CUBIC FEET [3]\n";
    std::cout << "    4. Convert CUBIC LITERS to CUBIC FEET [4]\n";
    std::cout << "    5. Convert CUBIC LITERS to CUBIC CENTIMETERS [5]\n";
    std::cout << "    6. Convert CUBIC CENTIMETERS to CUBIC LITERS [6]\n";
    std::cout << "    7. DONE WITH CONVERSIONS [7]\n";                        
    std::cout << "\n"; // prints a blank line             
}

 
  Home   Webmaster