#include "stdafx.h"
#include "Calculations.h"
#include <iostream>
usingnamespace std;
Calculations::Calculations()
{
}
Calculations::~Calculations()
{
}
int AddFunction(int x, int y){
return x + y;
}
int SubtractFunction(int x, int y){
return x - y;
}
int MultiplyFunction(int x, int y){
return x * y;
}
int DivideFunction(int x, int y){
return x / y;
}
In my main cpp file, I created a do/while loop. What I want to do is, if a person chose the wrong Operator (so they chose any other character instead of +, -, * or /) I want it to show an error message saying "wrong operator" which it does.
What I want it to do next is, save whatever numbers were added into nNum1 and nNum2 but only allow me to change what's in the cOperator variable and output the result with the changed variable.
it's hard for me to explain it, but if you need any more info, please let me know.
hiya,
when you say "save" where is it exactly you would like to refer to these variables again?
a few other things:
1) if you initialise CorrectOperand equal to true, then you can remove lines 31, 36, 41 and 46.
2) This: if (cOperator != '/' || '*' || '-' || '+') is incorrect. although in fact you've already ascertained at this point the wrong character has been input.
3) Your divide method will give you incorrect answers. have a google of "integer division errors".
4) the braces on lines 62 and 64 aren't doing anything.
It's hard for me to explain, but I can try with an example?
So if assign nNum1 with the value of 1 and nNum2 with the value of 2 and I was meant to add "+" to my cOperator but I pressed "&" by mistake.
What I would like to do is allow the user to renter the correct operator into cOperator and then continue the calculation of nNum1 (1) and nNum2 (2) with the new cOperator.
i am sorry if it does not make sense... I'm finding it hard just to explain it myself lol.
well, you could ask the user to the operator first, then do all the logic to see if it's the right one, and then ask for the two numbers?
(i've added some more stuff to my original post as well)
edit:
how are these:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
int AddFunction(int x, int y){
return x + y;
}
int SubtractFunction(int x, int y){
return x - y;
}
int MultiplyFunction(int x, int y){
return x * y;
}
int DivideFunction(int x, int y){
return x / y;
}
Thanks again for the reply! I will give it a try now and post results :)
I made the changes.
What I forgot about the do/while was that everything inside it is set to a specific value and will only come out of it if it is true/false. Thank you for that :)
in terms of Integer division errors, what I looked into states that it will not give me a decimal number, but a whole number which will make it inaccurate.
so what i tried doing was this:
1 2 3 4
float DivideFunction(float x, float y){
return x / y;
}
In regards to my class, I will add the code below:
1 2 3 4 5 6 7 8 9 10 11 12
#pragma once
class Calculations
{
public:
Calculations();
~Calculations();
};
int AddFunction(int x, int y);
int SubtractFunction(int x, int y);
int MultiplyFunction(int x, int y);
float DivideFunction(float x, float y);
It's only the basic, but I started learning them recently and have not gone in depth.
I understand that a class normally contains a public and a private, but I do not have a good understanding of those yet.
you are still fine to pass in ints so you can keep the input params the same. do something like this:
1 2 3 4 5
double DivideFunction(int x, int y){
return x / static_cast<double>(y);
}
all that static cast is doing is saying to you compiler "yes, i know y is an integer, but i'd like it treated as a double for this calculation". Doing that will force 'real' division.
Regarding classes:
I understand that a class normally contains a public and a private
Yep you are 100% correct with this statement. it's useful for some stuff to be "hidden" from the "outside world". You might want to google "encapsulation" for this.
Your current code shows me that your calculation functions are not part of your class. Just because they are in the same file doesn't mean they're in the same class.
I'll add a class example if you want?
#include<iostream>
usingnamespace std;
// class declation. typically this would go in a .h file
class Calculations {
public:
Calculations();
~Calculations();
void PerformCalculations();
private:
// the outside world doesn't need to see your 'internal' calculation
// methods/
int AddFunction(int x, int y);
int SubtractFunction(int x, int y);
int MultiplyFunction(int x, int y);
double DivideFunction(int x, int y);
};
// class implementaion. typically this would go in a .cpp
// and #include your .h header
Calculations::Calculations()
{
}
Calculations::~Calculations()
{
}
// notice how we use 'Calculations::'
// this tells the compiler these methds belong to this class
int Calculations::AddFunction(int x, int y)
{
return x + y;
}
int Calculations::SubtractFunction(int x, int y)
{
return x - y;
}
int Calculations::MultiplyFunction(int x, int y)
{
return x * y;
}
double Calculations::DivideFunction(int x, int y)
{
return x / static_cast<double>(y);
}
void Calculations::PerformCalculations()
{
// Good to initialise variables
int nNum1(0);
int nNum2(0);
char cOperator('+');
bool correctOperator(true);
do {
cout << "Please insert first number: ";
cin >> nNum1;
cout << "\n\nPlease insert Operator: ";
cin >> cOperator;
cout << "\n\nPlease insert second Number: ";
cin >> nNum2;
correctOperator = true;
switch (cOperator)
{
case'+':
cout << "Answer is: " << AddFunction(nNum1, nNum2) << endl;
break;
case'-':
cout << "Answer is: " << SubtractFunction(nNum1, nNum2) << endl;
break;
case'*':
cout << "Answer is: " << MultiplyFunction(nNum1, nNum2) << endl;
break;
case'/':
cout << "Answer is: " << DivideFunction(nNum1, nNum2) << endl;
break;
default:
cout << "Wrong operator Added...\n\n";
correctOperator = false;
}
} while (correctOperator != true);
}
int main()
{
// Create your object
// (exactly the same as something like 'int x;'
Calculations myCalcs;
// call your method on your 'myCalcs' object
// note:i've declared the other methods as private so
// you cant 'see' them in main(), which a good thing.
myCalcs.PerformCalculations();
return 0;
}