Making a calculator.

Hi, I've been making a calculator that is able to process decimals. I'm a noob, but would like some help on this matter:

These are the errors:

1>3.cpp(30): error C2065: 'dValue1' : undeclared identifier
1>3.cpp(30): error C2065: 'chOperator' : undeclared identifier
1>3.cpp(30): error C2065: 'dValue2' : undeclared identifier
1>3.cpp(30): error C2065: 'dResult' : undeclared identifier
1>3.cpp(37): error C2065: 'chOperator' : undeclared identifier
1>3.cpp(39): error C2065: 'chOperator' : undeclared identifier
1>3.cpp(41): error C2065: 'chOperator' : undeclared identifier
1>3.cpp(43): error C2065: 'chOperator' : undeclared identifier
1>3.cpp(57): error C2065: 'Operator' : undeclared identifier

This is the code:

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


double GetNumber() // får tallene
{
using namespace std;

cout << "Venligst skriv et tal (decimaler er tilladt): ";
double dValue;
cin >> dValue;

return dValue;
}

char GetOperator()
{
using namespace std;

cout << "Indtast en operator (*, /, - eller +): ";
char y;
cin >> y;
return y;
}

void PrintResult(double x)
{
using namespace std;

cout << dValue1 << " " << chOperator << " " << dValue2 << " = " << dResult << endl;
}

double CalculateResult(double w, char u, double o) // udregner tallene
{
using namespace std;

if (chOperator== '+')
return w + o;
if (chOperator== '-')
return w - o;
if (chOperator== '*')
return w * o;
if (chOperator== '/')
return w / o;
}

int main()
{
using namespace std;

double dValue1 = GetNumber(); // får det første tal

char chOperator = GetOperator(); // får operatoren

double dValue2 = GetNumber(); // får det andet tal

double dResult = CalculateResult(dValue1, Operator, dValue2); // får det andet tal

PrintResult(dResult);

cin.clear();
cin.ignore(255, '\n');
cin.get();
return 0;
}
'dValue1' and all those variables are local variables. They cannot be used outside the function (main()) where they're defined.
But how come this code works fluently? To me it seems like they're almost the same, only it's working with int instead of float.


#include "stdafx.h"
#include <iostream>
using namespace std;

int GetValue()
{
cout << "Skriv venligst en vaerdi: ";

int nValue;
cin >> nValue;
return nValue;
}

char GetOperator()
{
cout << "Indtast venligst en operator (*, /, -, +): ";

char chOperator;
cin >> chOperator;
return chOperator;
}

int CalculateResult(int nX, char Operator, int nY)
{
if (Operator=='+')
return nX+nY;
if (Operator=='-')
return nX-nY;
if (Operator=='*')
return nX*nY;
if (Operator=='/')
return nX/nY;

return 0;
}

void PrintResult(int nResult)
{
cout << "Dit resultat er: " << nResult << endl;
}
int main()
{
int nInput1 = GetValue(); // Får værdien

char Operator = GetOperator(); // Får operatoren

int nInput2 = GetValue(); // Får den anden værdi

int nResult = CalculateResult(nInput1, Operator, nInput2); // Udregner det og giver det en den midligeretidligere værdi nResult

PrintResult(nResult);
}
Please use code tags: [code]Your code[/code] http://www.cplusplus.com/articles/o13hAqkS/

The difference is the variables the compiler complains about.
1
2
3
4
5
6
void PrintResult(double x)
{
using namespace std;

cout << dValue1 << " " << chOperator << " " << dValue2 << " = " << dResult<< endl;
}
vs
1
2
3
4
void PrintResult(int nResult)
{
cout << "Dit resultat er: " << nResult << endl;
}


if you want to use a variable in more than 1 function you must define this variable outside any function but before the first function that uses the variable
Alright I'm super confused now. The calculator works - somewhat. But whenever I add the numbers, let's say 1 + 2 it gives me the result 44??! Where did I go wrong? Any help is much appreciated!

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
// nytforsog.cpp : main project file.

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	cout << "Gimme a number: ";
	double x;
	cin >> x;

	cout << "Gimme an operator: ";
	char y;
	cin >> y;

	cout << "Gimme another number: ";
	double w;
	cin >> w;

	if (y=='+')
		cout << x << " " << y << " " << w << " = " << x + y << endl;
	if (y=='-')
		cout << x << " " << y << " " << w << " = " << x - y << endl;
	if (y=='*')
		cout << x << " " << y << " " << w << " = " << x * y << endl;
	if (y=='/')
		cout << x << " " << y << " " << w << " = " << x / y << endl;

		
	cin.clear();
	cin.ignore(255, '\n');
	cin.get();
	
	return 0;
}
Last edited on
x + y where y is the '+' sign (which is ascii 43). so it is 1 + '+' or 1 + 43 hence = 44

it must be x + w
Hi

Have you considered a test if none of the 4 operators is entered to throw an error or ask for re entry?

Graham
Topic archived. No new replies allowed.