Help with basic calculator.

I've been trying to make just a basic calculator.
The problem is at line 33 where i expected WriteAnswer to write the variable 'ans' but it did not.
What am I doing wrong?

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
#include <iostream>

using namespace std;

int ReadNumber(){

    cout << "enter first number:" << endl;
    int fNum;
    cin >> fNum;

    cout << "enter second number:" << endl;
    int sNum;
    cin >> sNum;

    int ans(fNum + sNum);

    return ans;
}


void WriteAnswer(){

    int ans;
    cout << "The sum is:" << ans << endl;
}


int main() {

    int fNum;
    int sNum;

    WriteAnswer(fnum + sNum);

    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
#include <iostream>

using namespace std;

int ReadNumber(){

    cout << "enter first number:" << endl;
    int fNum;
    cin >> fNum;

    cout << "enter second number:" << endl;
    int sNum;
    cin >> sNum;

    int ans(fNum + sNum);

    return ans;
}


void WriteAnswer(int ans){   
    cout << "The sum is:" << ans << endl;
}


int main() {

    int fNum;
    int sNum;

    WriteAnswer(fNum + sNum);

    return 0;
}


At line 33 you wrote 'fnum', not the same with 'fNum',C++ is case sensitive.
At line 33 you are passing an integer to the function,but the function has no parameters.So modify it's definition,and print the passed value.
You are not initializing fNum and sNum.
Last edited on
More changes in bold:
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
#include <iostream>

using namespace std;

int ReadNumber(){

    cout << "enter first number:" << endl;
    int fNum;
    cin >> fNum;

    cout << "enter second number:" << endl;
    int sNum;
    cin >> sNum;

    int ans(fNum + sNum);

    return ans;
}


void WriteAnswer(int ans){   

    int ans;
    cout << "The sum is:" << ans << endl;
}


int main() {

    int fNum;
    int sNum;

    WriteAnswer( ReadNumber() );

    return 0;
}
Last edited on
Topic archived. No new replies allowed.