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: -
#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);
-
}
-
1 1742
you need to have a break; after each operation in your switch statement, e.g. -
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;
-
}
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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....
|
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...
|
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...
|
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...
|
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...
|
by: Minh Khoa |
last post by:
Please give me more information about delegate and its usage?
Why do i use it and when?
|
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...
|
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){
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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
|
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...
|
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...
|
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,...
|
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...
| |