By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 838 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

More Function Malfunction

P: 16
Newb C++ problem, arithmetic functions are returning 0 values each time. Not sure where the problem is, but I've run checks and it seems to be going fine until the very end where the arithmetic functions are:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. const int SIZE = 41;
  7. char TestOp, UseOp, TestNum[SIZE];
  8. double UseNum, result, value;
  9. int i, j;
  10.  
  11. void introduction();
  12. bool IsValidOp(char TestOp, int i);
  13. bool IsValidNum(char TestNum[], int j);
  14. double Addition(double UseNum, double result);
  15. double Subtraction(double UseNum, double result);
  16. double Division(double UseNum, double result);
  17. double Multiplication(double UseNum, double result);
  18.  
  19. void main()
  20.      {
  21.      introduction();
  22.      result = 0;
  23.           while (1) 
  24.     {                            cout << "Enter desired operation: ";
  25.     cin >> TestOp;
  26.  
  27.     if (IsValidOp(TestOp, i))    
  28.          UseOp = TestOp;
  29.     else
  30.          {
  31.          cout << "Invalid operator. Try again." << endl;
  32.          continue;
  33.          } 
  34.  
  35.     switch(UseOp)
  36.          {
  37.          case 'C':
  38.          case 'c': 
  39.                           cout << "Result has been reset to 0." << endl;
  40.               continue;
  41.  
  42.          case 'X': 
  43.          case 'x': break;
  44.          }
  45.  
  46.     cout << "Enter a number: ";
  47.     cin >> TestNum;
  48.  
  49.     if (IsValidNum(TestNum, j))
  50.          UseNum = atof(TestNum);
  51.     else {
  52.           cout << "Invalid number. Try again." << endl;
  53.           continue;
  54.            }
  55.  
  56.     switch (UseOp)
  57.          {
  58.          case '+': result = Addition(UseNum, result);
  59.                      case '-': result = Subtraction(UseNum, result);
  60.          case '*': result = Multiplication(UseNum, result);
  61.                      case '/': 
  62.         if (UseOp=='/' && UseNum==0)
  63.         {
  64.                    cout << "Error: Cannot divide by zero. Try again." << endl;
  65.         continue;
  66.         }
  67.         else{
  68.                result = Division(UseNum, result);
  69.               }
  70.  
  71.                 cout << "Result: " << result << endl;
  72.         }
  73.     }
  74. }
  75.  
  76. //Function for introduction to the program
  77. void introduction()
  78. {
  79. cout << "-------------------------------------------------------------------------------" << endl;
  80. cout << "This program will act as a basic four-function calculator. The valid operators\nare + for addition, - for subtraction, * for multiplication, and / for division.Typing a C or c as an operator will clear the result and restart the calculator.Typing an X or x as an operator will shut down the program and end the\ncalculator. All numbers will be displayed as whole numbers." << endl;
  81. cout << "-------------------------------------------------------------------------------" << endl;
  82.     }
  83.  
  84. //Function to read the operator, validate it, and store it
  85. bool IsValidOp(char TestOp, int i)
  86.     {
  87.     char ValidOp[9] = "+-*/cCxX";
  88.     for (i=0; i<9; i++)
  89.         {
  90.         if (TestOp==ValidOp[i])
  91.             return true;
  92.         }
  93.     return false;
  94.     }
  95.  
  96. //Function to validate the numbers
  97. bool IsValidNum(char TestNum[], int j)
  98.     {
  99.     for (j=0; j<41; j++)
  100.         {
  101.         if (isdigit(TestNum[j]) || TestNum[j]=='.')
  102.             {
  103.             return true;
  104.             break;
  105.             }
  106.         }
  107.     return false;
  108.     }
  109.  
  110. //Operational functions
  111. double Addition(double UseNum, double result)
  112.     {
  113.     return result + UseNum;
  114.     }
  115.  
  116. double Subtraction(double UseNum, double result)
  117.     {
  118.     return result - UseNum;
  119.     }
  120.  
  121. double Multiplication(double UseNum, double result)
  122.     {
  123.     return result * UseNum;
  124.     }
  125.  
  126. double Division(double UseNum, double result)
  127.     {
  128.     return result = (result / UseNum); 
  129.     }
  130.  
Mar 11 '07 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 1,510
you need to have a break; after each operation in your switch statement, e.g.
Expand|Select|Wrap|Line Numbers
  1.     switch (UseOp)
  2.          {
  3.          case '+': result = Addition(UseNum, result);
  4.                          cout << "Result: " << result << endl;break;
  5.                      case '-': result = Subtraction(UseNum, result);cout << "Result: " << result << endl;break;
  6.          case '*': result = Multiplication(UseNum, result);cout << "Result: " << result << endl;break;
  7.                      case '/': 
  8.         if (UseOp=='/' && UseNum==0)
  9.         {
  10.                    cout << "Error: Cannot divide by zero. Try again." << endl;
  11.         continue;
  12.         }
  13.         else{
  14.                result = Division(UseNum, result);
  15.               }
  16.  
  17.                 cout << "Result: " << result << endl;
  18.         }
  19.     }
  20.  
Mar 11 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.