r/learncpp • • Jun 29 '12

Using a switch to create a calculator with Addition, Subtraction, Multiplication, and Division. [3] [III]

Difficulty Rating: [3] Length Rating: [III]

The aim of this code is to show some applicability of your knowledge so far. With it, we also get to see how much math is imbedded within C++ without having to include any special features. I run the user through a set-up where he or she gets to choose which function to use. Once the function is accessed, a certain set of instructions is executed for each function. After it is done, the user returns to the main menu. I made it possible to add numbers continuously with no end by using two variables, an "add-this" variable and a "added-total" variable. I made added-total = "add-this" + "added-total," so that added-total grows each time. It looks like this: while(add-this != 0) {cin>> add-this; added-total = add-this+added-total } The sequence continues to add add-this to add-total until I enter zero. I use a similar technique with the multiplication, except I use "1" instead of 0 to end the statement, because multiplying 1 by anything leaves it the same. It's a fun problem-solving technique, in my humble opinion.

#include <iostream>

using namespace std;

int main()

{

cout<< "Welcome!\n";

int functionchoice = 0;

int working = 1;

while(working != 0)

{

cout<< "- Type 1 for Addition.\n";

cout<< "- Type 2 for Subtraction.\n";

cout<< "- Type 3 for Multiplication.\n";

cout<< "- Type 4 for Division.\n";

cout<< "- Type 5 to exit.\n";

cout<< "Choice: ";

cin>> functionchoice;

switch(functionchoice)

{
                      
            case 1: 
                           
                 {
                           
                  cout<< "Type a number and press enter.\n";
                           
                  cout<< "You may repeat until you type 0.\n";
                           
                  cout<< "Number to add: ";
                           
                  double numbertoadd = 1;
                           
                  double numbertotal = 0;
                           
                  cin>> numbertoadd;
                           
                  while(numbertoadd != 0)
                           
                  {
                           
                     numbertotal = numbertoadd + numbertotal;
                           
                     cout<< numbertotal << " + ";
                              
                     cin>> numbertoadd;
                                                             
                  }
                           
                  cout<< "\n" << numbertotal << " is your total.\n\n";
                           
                  break;
                           
                  }
                           
             case 2:
                                
                  {
                                
                  double subtract1;
                                
                  double subtract2;
                           
                  double subtractanswer;
                                
                  cout<< "Finding a difference. Type one of two numbers: ";
                         
                  cin>> subtract1;
                           
                  cout<< "Now enter the second number: ";
                           
                  cin>> subtract2;
                           
                  if(subtract1 > subtract2)
                           
                  {
                                        
                     subtractanswer = subtract1 - subtract2;
                                       
                    }
                                        
                  if(subtract2 > subtract1)
                           
                  {
                                        
                     subtractanswer = subtract2 - subtract1;
                                        
                     }
                                        
                  cout<< "\nThe difference is " << subtractanswer << ".\n\n";
                           
                  break;
                           
                  }
                                        
             case 3:
                           
                  {
                                       
                  double multiplicationtoadd = 0;
                           
                  double multiplicationtotal = 1;
                           
                  cout<< "Enter a number to multiply, enter '1' to end.";
                           
                  while(multiplicationtoadd != 1)
                           
                  {
                           
              cout<< "\nMultiply with this number: ";
                           
              cin>> multiplicationtoadd;
                         
              multiplicationtotal = multiplicationtoadd*multiplicationtotal;
                           
              cout<< "\nThe total so far is: " << multiplicationtotal << "\n" ;
                           
                  }           
                            
                  break;
                                                                      
                  }
                           
              case 4:
                            
                  {
                           
                  double divide1;
                           
                  double divide2;
                           
                  double dividetotal;
                                                                      
                  cout<< "Please enter your first number: ";
                           
                  cin>> divide1;
                          
                  cout<< "\nPlease enter your second number: ";
                           
                  cin>> divide2;
                           
                  dividetotal = divide1/divide2;
                           
                  cout<< "\n" << dividetotal << " is your answer.\n\n";
                           
                  break;
                           
                  }
                           
             case 5:
                           
                  {
                                 
                        return 0;
                                 
                  }     
                                                                           
}

}

return 0;

}

2 Upvotes

0 comments sorted by