Aug 24, 2011 at 2:41pm UTC
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;
}
Aug 24, 2011 at 2:46pm UTC
'dValue1' and all those variables are local variables. They cannot be used outside the function (main()) where they're defined.
Aug 24, 2011 at 3:01pm UTC
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);
}
Aug 26, 2011 at 7:06am UTC
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 Aug 26, 2011 at 7:19am UTC
Aug 26, 2011 at 8:14am UTC
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
Aug 26, 2011 at 11:04pm UTC
Hi
Have you considered a test if none of the 4 operators is entered to throw an error or ask for re entry?
Graham