473,387 Members | 1,529 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

More Function Malfunction

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
1 1742
horace1
1,510 Expert 1GB
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

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

Similar topics

0
by: Lisa D. Toshiba | last post by:
Wakey wakey! A good heart is better than all the heads in the world. Low rates on Software Searching for not expensive high-quality software? Our site might be just what you need....
28
by: Randy Starkey | last post by:
Hi, Does anyone know where I can get a script that show a little plus sign after a line of text, that when you click the plus sign, more text is revealed on that same page, like a continuing...
2
by: BrianP | last post by:
Hi, I have had to invent a work-around to get past what looks like a JavaScript bug, the malfunctioning Perl-like JavaScript array functions including SPLICE() and UNSHIFT(). I have boiled it...
116
by: Mike MacSween | last post by:
S**t for brains strikes again! Why did I do that? When I met the clients and at some point they vaguely asked whether eventually would it be possible to have some people who could read the data...
1
by: SteveC | last post by:
I have just uprgraded from Office 97 to Office 2003 and have a problem with a database I created. When I open the database, Access reports that database contains missing or broken reference to...
3
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
6
by: Teddy.Gammell | last post by:
Hi, I would like to write a function which does this: for each element in the list call f1() of each element. if f1() return false, break the loop else continue I try to use the...
40
by: gert | last post by:
#include <stdio.h> obj function hello(){ struct obj = { char *data = 'hello'} obj.add = obj_add(obj); return obj; } void function obj_add(obj){ obj function add(value){
151
by: istillshine | last post by:
There are many languages around: C++, JAVA, PASCAL, and so on. I tried to learn C++ and JAVA, but ended up criticizing them. Is it because C was my first programming language? I like C...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.