C++ Calculator won't sum the numbers.

Hello,

The code I pasted below is runnable however, at the end as you can see it will not sum the numbers, thanks for any help in advance.

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
125
126
127
128
129
  
// Test.cpp : Defines the entry point for the console application.
// This C++ code was created by Nathan Bullock and is not to be editied or used by any other persons.

#include <iostream>
#include <string>
using namespace std;
 
int GetTitle()
{
cout <<"Calculator Version 1.0 created and developed by Nathan Bullock\n ";    
}

int GetUserInput()
{
    cout << "\nEnter a number: ";
    int nValue;
    cin >> nValue;
    return nValue;
}
 
char GetMathematicalOperation()
{
    cout << "\nEnter a valid operator (+,-,*,or /): ";
 
    char chOperation;
    cin >> chOperation;
    return chOperation;
}
 
int CalculateResult(int nX, char chOperation, int nY)
{
    if (chOperation=='+')
        return nX + nY;
    if (chOperation=='-')
        return nX - nY;
    if (chOperation=='*')
        return nX * nY;
    if (chOperation=='/')
        return nX / nY;
	return 0;
}
 
void PrintResult(int nResult)
{
    cout << "\nYour result is: " << nResult << endl;
	cout << "\nWould you like to do anything else with " << nResult << " ?" << endl;
    int choiceOne_Path;
    cout << "\n\t >> Enter '1' Yes" << endl;
    cout << "\t >> Enter '2' No\n" << endl;
    cout << "\nEnter your choice: \n ";
    cin >> choiceOne_Path;
    if(choiceOne_Path == 1);
    cout << "What would you like to do with " << nResult << " ?" << endl;
}
char GetInput()
{
  cout << "\nEnter a valid operator (+,-,*,or /): ";
 
    char DoTheSum;
    cin >> DoTheSum;
    return DoTheSum;   
}

int GetUserInput3()
{
    cout << "\nEnter a number: ";
    int nValue2;
    cin >> nValue2;
    return nValue2;
}

int CalculateResult2(int nResult2, char DoTheSum, int nValue2)
{
    if (DoTheSum=='+')
        return nResult2 + nValue2;
    if (DoTheSum=='-')
        return nResult2 - nValue2;
    if (DoTheSum=='*')
        return nResult2 * nValue2;
    if (DoTheSum=='/')
        return nResult2 / nValue2;
        
        return 0;
}
void PrintResult2(int nResult)
{
     cout << "\nYour result is: " << nResult << endl;
}

	


int main()
{
    
	//Title
	int title = GetTitle();
    
    // Get first number from user
    int nInput1 = GetUserInput();
 
    // Get operation from user
    char chOperator = GetMathematicalOperation();
    
    // Get second number from user
    int nInput2 = GetUserInput();
 
    // Calculate result
    int nResult = CalculateResult(nInput1, chOperator, nInput2);
 
    // Print result
    PrintResult(nResult);
    
    // Would you like to do anything else to your answer?
    char SecondTime = GetInput();
    
    // Get users number again
    int nInput3 = GetUserInput3();
    
    //int nResult2 = CalculateResult2(nResult, DoTheSum, nInput3); 
    
    int PrintResult2(nResult);

	char DoTheSum = GetInput();
    
    system ("PAUSE");
 }
This doesn't build for me:

1
2
3
4
int GetTitle()
{
cout <<"Calculator Version 1.0 created and developed by Nathan Bullock\n ";    
}

You've told the compiler you want to return an integer value, but you are not, make it a void function.

Also at a quick glance i see some other issues. E.g. :
 
if(choiceOne_Path == 1);

that semi-colon means the line proceding that will have nothing to do with your if statement.
Last edited on
I don't understand why it won't build for you, I have tried using the website and it builds fine, also the:

1
2
3
4
 int GetTitle()
{
cout <<"Calculator Version 1.0 created and developed by Nathan Bullock\n ";    
}

if(choiceOne_Path == 1);


work fine, it's just at the end where it wont's the sum the numbers together.

work fine


not really no, it means that this line:

cout << "What would you like to do with " << nResult << " ?" << endl;

will be printed out regardless of what the user enters.

psyduck what compiler are you using? Do you at least have a warning message about you not returning an int from this method?

and can you tell me where you think it's summing the numbers together?
Last edited on
I am using DEV C++.

The numbers sum together the first time, however if you say 'yes' to 'Would you like to do anything else to these numbers?' it won't -,+,/,* the numbers.
I am using DEV C++.

i think that's very old, but maybe someone else can confirm that. That's why it's not being too strict with return values from functions.

I've already explained that line 53 isn't doing anything.

And you aren't calling calculateResult() again so why would you expect it to do anything else?

You have functions that are doing exactly the same thing e.g. GetUserInput and GetUserInput3. Have a look at this:

http://www.cplusplus.com/doc/tutorial/functions/

The point of functions is that you can use them again with different input parameters so that you don't have to write duplicate code.
Last edited on
Topic archived. No new replies allowed.