my function wont update variables in main()

I am trying to make a program that can calculate linear digits of pi by using the formula pi/4 = 1 - 1/3 + 1/5 - 1/7...etc. however I cant seem to get the two functions that are supposed to keep track of both the sign oscillating back and forth and the change in denominator. the program is not complete since I need to multiply by four at the end, but it should work enough to give me some reasonable answer can someone explain how to have a function update variables that I can use again in another function that are declared in int main()

thanks.

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
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace std;

void subtractfunction(int oscillate, int denominator, int totalpi);
void additionfunction(int oscillate, int denominator, int totalpi);

int main(){
int oscillate = 0;
int denominator = 3;
int totalpi = 1;
int timestodoopp = 0;
cin >> timestodoopp;
for (int i = 1; i < timestodoopp + 1; i++){
        if (oscillate == 0){
        subtractfunction (oscillate, denominator, totalpi);
        // neither function seems to be updating the variables oscillate , denominator, or totalpi.
        }
        else{
        additionfunction (oscillate, denominator, totalpi);
        // can functions update global variables and if so how or could I have it return 3 different numbers.
        };
    };
    cout << totalpi << endl;
    return 0;
}
void subtractfunction(int oscillate, int denominator, int totalpi){
    totalpi = totalpi - (1/denominator);
    denominator = denominator + 2;
    oscillate = oscillate + 1;
};
void additionfunction(int oscillate, int denominator, int totalpi){
    totalpi = totalpi + (1/denominator);
    denominator = denominator + 2;
    oscillate = oscillate - 1;
};
Your function subtractfunction is given a copy of the variables. Anything it does, it does to the copy. When the function ends, those copies are discarded. The originals, from your main function, are unchanged.

Try passing the variables by reference, so that the functions don't get copies but instead work on the originals.

This is done by adding the & symbol in the function declaration and definition, like this:
1
2
void subtractfunction(int& oscillate, int& denominator, int& totalpi);
void additionfunction(int& oscillate, int& denominator, int& totalpi);

and similarly in the defintions (line 30 and 35).

I suggest also that since you're going to be working with numbers that aren't integers, you don't use int for your numbers. Making totalpi an integer seems silly when you know it's meant to end up 3.14159; how can you represent 3.14159 with an integer?

Note also that in C++, an int divided by an int gives an int (line 31, line 36). Here's a clue to help fix that.

1/2 = 0
1/2.0 = 0.5
Last edited on
See http://www.cplusplus.com/doc/tutorial/functions/
for "arguments by value and by reference".

Here is a little program for you:
1
2
3
4
5
6
7
8
#include <iostream>

int main() {
  for ( int x = 1; x < 5; ++x ) {
    std::cout << "1/" << x << " == " << 1/x <<'\n';
  }
  return 0;
}

Do you notice anything suspicious in its output?
ok thanks both of you for your help I appreciate it and will look into your fixes.
Topic archived. No new replies allowed.