How can I get the sum of the numbers?

In this program you can type in any non-negative integer and it will go through the formulas inside the program and give you an output. the program runs perfectly fine if you want to compile and run it, but how can i find the sum of the numbers that are output? i tried adding a sum formula in the while loop, but it only added up the last two numbers.
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
#include <cmath>  
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

void Calculate();
void ContinueRun();
void CheckItOut();



int main()
{
   
   
   
  Calculate();
  ContinueRun(); 
 
  
  
   
}

//////////////////////////////////////////////////////////////
void Calculate()
{
    int x;
    int a;
    int y;
    int sum;
    int sub;
    int even;
    int odd;
    
    sub = 0;
    even = 0;
    odd = 0;
    
    
    cout << "*************************************************" << endl << endl;
    cout << "Please Enter A Nonnegative Integer: ";
    cin >> x;
    
    cout << "You Entered: " << x << endl;
    
    while (x > 1)
    {
    if (x % 2 == 0)
    {     
    a = x / 2;
    
    cout << "a" << sub << "= " << x << endl;
    x = a;

    even++;
    }  

    
    else
    {
    a = 3 * x + 1;
    
    cout << "a" << sub << "= " << x << endl;
    x = a;
    
    odd++;
    }
    sub++;
    }
    
    cout << "a" << sub << "= " << x << endl << endl << endl;
    odd++;
    cout << "The integer k such that a_k=1 is " << sub << endl;
    cout << "The number of even integers were " << even << endl;
    cout << "The number of odd integers were " << odd << endl;
    cout << "The sum of the numbers is " << sum << endl;
    
}
////////////////////////////////////////////////////////////////////////////////
void ContinueRun()
{
   int count;
   int time;
   char answer;
   
   time = 1;
   count = 1;
   
     
   while (time == 1)
   { 
   cout << endl << "Would You Like To Run The Program Again? (Y/N) ";
   cin >> answer;
   cout << endl;
   if (answer == 'y' || answer == 'Y')
   {
     Calculate();
     cout << endl << endl;
     time = 1;
     count++;
   }
   
   else 
   {
     cout << "The Program Was Run " << count << " Times." << endl << endl;
     time = 0;
     system ("pause");
   }
   }  
}
////////////////////////////////////////////////////////////////////////////////
void CheckItOut()
{
   
}
the + operator
was that supposed to be a joke?
You don't do anything with 'sum' not even intialization.

->
1
2
3
sum = 0;
...
sum += x; // After line 50 
Last edited on
Topic archived. No new replies allowed.