need Help with saving a variable

Hello

I am currently learning programming. I am still at the beginners level, but I am having a problem trying to figure this out.

I created a basic calculator (using +, -, / and *) and I separated functions into another class to help me learn and understand hour class files work.

I will add my code first:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "stdafx.h"
#include "Calculations.h"
#include <iostream> 

using namespace std; 


void PerformCalculations(){

	int nNum1;
	int nNum2;
	char cOperator;
	bool CorrectOperand = false;

	do  {

		cout << "Please insert first number: ";
		cin >> nNum1;

		cout << "\n\nPlease insert Operator: ";
		cin >> cOperator;

		cout << "\n\nPlease insert second Number: ";
		cin >> nNum2;


		switch (cOperator)
		{
		case '+':
			cout << "Answer is: " << AddFunction(nNum1, nNum2) << endl;
			CorrectOperand = true;
			break;

		case '-':
			cout << "Answer is: " << SubtractFunction(nNum1, nNum2) << endl;
			CorrectOperand = true;
			break;

		case '*':
			cout << "Answer is: " << MultiplyFunction(nNum1, nNum2) << endl;
			CorrectOperand = true;
			break;

		case '/':
			cout << "Answer is: " << DivideFunction(nNum1, nNum2) << endl;
			CorrectOperand = true;
			break;

		default:
			cout << "Wrong operator Added...\n\n";
			CorrectOperand = false;

			if (cOperator != '/' || '*' || '-' || '+')

			{
				cout << "invalid...\n\n";

			}

		}
	} while (CorrectOperand != true);
	{

	}

}

int _tmain(int argc, _TCHAR* argv[])
{
	PerformCalculations();

	return 0;
}
 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

#include "stdafx.h"
#include "Calculations.h"
#include <iostream> 

using namespace 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.

thank you.
Last edited on
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.
Last edited on
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.
Last edited on
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; 
}


actually related to your calculations class?
Last edited on
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.
Last edited on
hello again,
so what i tried doing was this:


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?
Last edited on
here's an example. gotta go and eat and have beer now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include<iostream>
using namespace 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;
}

Hello and apologies for the late reply! I had a power cut, so I was not able to reply yesterday :(.

I am currently looking at encapsulation now and I am confused about something with the class.

When a variable is stored in private, it makes it hard for it to be used, so why is this referred to as good programming practice?

Also, when looking at classes, there is something called a get/set. What is this exactly?
Topic archived. No new replies allowed.