Local varibles

I am having an issue with my local variables, I do not want global variables. My code works if I have the same variables global but that is not my intent. Could someone please explain whats going on and why my output is not correct?

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
#include <iostream>
#include <limits>
using namespace std;

double getUserValue(double valueOne, double valueTwo, char calculationSymbol);
void displayResults(double valueOne, double valueTwo, char calculationSymbol, double answerValue);

main()
{
 cout << "          Welcome to my Calculator Program!" << endl;
 cout << endl;
 cout << "Enter any equation below, example: 3 * 7" << endl;
 cout << "Operators include: *  /  +  -" << endl;
 cout << endl;
 double valueOne = 0;
 double valueTwo = 0;
 char calculationSymbol;
 double answerValue;
 char response;

 do{
 getUserValue(valueOne,valueTwo,calculationSymbol);
 displayResults(valueOne,valueTwo,calculationSymbol,answerValue);

 cout << "Would you like to make another calculation? Y/N : ";
 cin >> response;
 cout << endl;
 }while(toupper(response)=='Y');
 cout << "\nThank you for using my Program!" << endl;
 cout << endl;
 return 0;
}

double getUserValue(double valueOne, double valueTwo, char calculationSymbol)
{
    bool bFail;

    do{
    cout << "Equation: ";
    cin >> valueOne >> calculationSymbol >> valueTwo;

    bFail = cin.fail();

    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    if(bFail == true){
        cout << "ERROR: Please input valid Equation"<< endl;}
    cout << endl;
    }while(bFail == true);

}


void displayResults(double valueOne, double valueTwo, char calculationSymbol, double answerValue)
{

   switch(calculationSymbol)
    {case '+':
        answerValue = valueOne + valueTwo;
        break;
    case '-':
        answerValue = valueOne - valueTwo;
        break;
    case '*':
        answerValue = valueOne * valueTwo;
        break;
    case '/':
        answerValue = valueOne / valueTwo;
        break;
}
 cout<< "Answer: " << answerValue << endl;
}
You're passing your arguments by value, which means a copy is made and so the valueOne in getUserValue is not the same valueOne in main.

What you want to do is pass by reference instead, so that they will be the same variable (essentially):
void getUserValue(double& valueOne, double& valueTwo, char& calculationSymbol);
(I also changed the return type from double to void, since you don't actually return a value.)

You won't have to do this for displayResults, since you don't really need to change the variables. (And by the way, you also don't need answerValue -- you can just declare that variable inside of displayResults.)

On a side note, it should be int main(), not main().
Awesome! Thanks for the help, my only question I am left with is how do I declare getUserValue in the main?

I answered my own question, It wouldn't change..
Last edited on
Topic archived. No new replies allowed.