More Function Malfunction

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Randeh
    New Member
    • Feb 2007
    • 16

    #1

    More Function Malfunction

    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:

    Code:
    #include <iostream>
    #include <iomanip>
    #include <cstring>
    using namespace std;
    
    const int SIZE = 41;
    char TestOp, UseOp, TestNum[SIZE];
    double UseNum, result, value;
    int i, j;
    
    void introduction();
    bool IsValidOp(char TestOp, int i);
    bool IsValidNum(char TestNum[], int j);
    double Addition(double UseNum, double result);
    double Subtraction(double UseNum, double result);
    double Division(double UseNum, double result);
    double Multiplication(double UseNum, double result);
    
    void main()
         {
         introduction();
         result = 0;
              while (1) 
    	{							cout << "Enter desired operation: ";
    	cin >> TestOp;
    
    	if (IsValidOp(TestOp, i))	
    	     UseOp = TestOp;
    	else
    	     {
    	     cout << "Invalid operator. Try again." << endl;
    	     continue;
    	     } 
    
    	switch(UseOp)
    	     {
    	     case 'C':
    	     case 'c': 
                              cout << "Result has been reset to 0." << endl;
    	          continue;
    		
    	     case 'X': 
    	     case 'x': break;
    	     }
    
    	cout << "Enter a number: ";
    	cin >> TestNum;
    		
    	if (IsValidNum(TestNum, j))
    	     UseNum = atof(TestNum);
    	else {
    	      cout << "Invalid number. Try again." << endl;
    	      continue;
    	       }
    		
    	switch (UseOp)
    	     {
    	     case '+': result = Addition(UseNum, result);
                         case '-': result = Subtraction(UseNum, result);
    	     case '*': result = Multiplication(UseNum, result);
                         case '/': 
    		if (UseOp=='/' && UseNum==0)
    		{
    	               cout << "Error: Cannot divide by zero. Try again." << endl;
    		continue;
    		}
    		else{
    		       result = Division(UseNum, result);
    		      }
    	
                    cout << "Result: " << result << endl;
    		}
    	}
    }
    
    //Function for introduction to the program
    void introduction()
    {
    cout << "-------------------------------------------------------------------------------" << endl;
    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;
    cout << "-------------------------------------------------------------------------------" << endl;
    	}
    
    //Function to read the operator, validate it, and store it
    bool IsValidOp(char TestOp, int i)
    	{
    	char ValidOp[9] = "+-*/cCxX";
    	for (i=0; i<9; i++)
    		{
    		if (TestOp==ValidOp[i])
    			return true;
    		}
    	return false;
    	}
    
    //Function to validate the numbers
    bool IsValidNum(char TestNum[], int j)
    	{
    	for (j=0; j<41; j++)
    		{
    		if (isdigit(TestNum[j]) || TestNum[j]=='.')
    			{
    			return true;
    			break;
    			}
    		}
    	return false;
    	}
    
    //Operational functions
    double Addition(double UseNum, double result)
    	{
    	return result + UseNum;
    	}
    
    double Subtraction(double UseNum, double result)
    	{
    	return result - UseNum;
    	}
    
    double Multiplication(double UseNum, double result)
    	{
    	return result * UseNum;
    	}
    
    double Division(double UseNum, double result)
    	{
    	return result = (result / UseNum); 
    	}
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    you need to have a break; after each operation in your switch statement, e.g.
    Code:
    	switch (UseOp)
    	     {
    	     case '+': result = Addition(UseNum, result);
                             cout << "Result: " << result << endl;break;
                         case '-': result = Subtraction(UseNum, result);cout << "Result: " << result << endl;break;
    	     case '*': result = Multiplication(UseNum, result);cout << "Result: " << result << endl;break;
                         case '/': 
    		if (UseOp=='/' && UseNum==0)
    		{
    	               cout << "Error: Cannot divide by zero. Try again." << endl;
    		continue;
    		}
    		else{
    		       result = Division(UseNum, result);
    		      }
    	
                    cout << "Result: " << result << endl;
    		}
    	}

    Comment

    Working...