C++ Return Problem

Hey guys and galls. I am trying to create a compare function, but it doesn't work properly. Somehow the program is "forgetting" the value I told it previously.

Here is the code

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
130
131
132
133
#include <iostream>
#include <cmath>
using namespace std; 



int sum1 (int);
int sum2 (int);
int sum3 (int);
int sum4 (int);
int compare (int);


int main ( )

{
    
    int n = 5;
    int sumone = sum1(n);
    int sumtwo = sum2 (n);
    int sumthree = sum3 (n);
    int sumfour = sum4 (n);
    int comp1; 
    int comp2; 
    int comp3; 
   



    
    
    cout << "The value of sum1 is " << sumone << endl; 
    cout << "The value of sum2 is " << sumtwo << endl; 
    cout << "The value of sum3 is " << sumthree << endl; 
    cout << "The value of sum4 is " << sumfour << endl; 
    
    if (comp1 = 0)
    cout << "sum1 and sum2 are different" << endl; 
    if (comp1 = 1)
    cout << "sum1 and sum2 are the same" << endl; 
    if (comp2 = 0)
    cout << "sum2 and sum3 are different" << endl; 
    if (comp2 = 1)
    cout << "sum2 and sum3 are the same" << endl; 
    if (comp3 = 0)
    cout << "sum3 and sum4 are different" << endl; 
    if (comp3 = 1)
    cout << "sum3 and sum4 are the same" << endl;
    

    system("Pause");
    return 0; 
}

    int sum1 (int n )
    {
    
    
    int sumone = 0;
    int k = 1;
    
        for (k = 1; k <= n; k++)
        
        sumone += k * k * k;
         
    return sumone;
    }
    
    int sum2 (int n)
    {
    int sumtwo = (n * n) * (n + 1) * (n + 1) / 4;
    
    return sumtwo; 
    }
    
        int sum3 (int n)
    {
    
    int insideparen = 0;
    int m = 1;
    
        while (m <= n){
        insideparen += m;
        m++; }
        
    int sumthree = insideparen * insideparen;
    return sumthree; 
    }
    
        int sum4 (int n)
    {
    int sumfour = n * (n + 1) * (2 * n + 1) / (4 + 2); 
    
    return sumfour;
    
    }

int compare (int sumone, int sumtwo, int sumthree, int sumfour)
{
    int comp1;
    int comp2; 
    int comp3; 
    
    
    if (sumone != sumtwo)
    
             return comp1 = 0; 
             
    else
    
             return comp1 = 1; 
         
    if (sumtwo != sumthree)
    
             return comp2 = 0; 
             
    
    else
    
             return comp2 = 1;
         
    if (sumthree != sumfour)
    
             return comp3 = 0;
             
    
    else
    
             return comp3 = 1; 
}
    

    



The value of sum1 is 225
The value of sum2 is 225
The value of sum3 is 225
The value of sum4 is 55
sum1 and sum2 are the same
sum2 and sum3 are the same
sum3 and sum4 are the same
Press any key to continue . . .

Obviously 3 and 4 are not equal, lol.
Last edited on
You're trying to return too many results. Return can only handle 1. so any int function can return 1 variable. if you want to 'return' multiple you need to call that variable in the parameters using the call-by-reference method. [done by type&, where type is int short, double etc.]

here is a debugged version of your compare function:
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

void compare (int& sumone, int& sumtwo, int& sumthree, int& sumfour,int& comp1,int& comp2, int& comp3 )
{ 
    
    
    if (sumone != sumtwo)
   comp1 = 0; 
             
    else
    
    comp1 = 1; 
         
    if (sumtwo != sumthree)
    
     comp2 = 0; 
             
    
    else
     comp2 = 1;
         
    if (sumthree != sumfour)
    
     comp3 = 0;
             
    
    else
      comp3 = 1; 
}


also in your displaying if statements you must use the equal to operator not the assignment operator [==; not =] This should clear up your troubles. I ran it on my system with the necessary changes and it worked just fine. Hope this answers your questions.
--Edit--
Oh! I almost forgot! I declared all the variables as global for the trial, make sure you declare your comp1 comp2 comp3 inside main!
Last edited on
Thanks!!!

However, I had to change the if statements

if (comp1 = 0)
cout << "sum1 and sum2 are different" << endl;
if (comp1 = 1)
cout << "sum1 and sum2 are the same" << endl;
if (comp2 = 0)
cout << "sum2 and sum3 are different" << endl;
if (comp2 = 1)
cout << "sum2 and sum3 are the same" << endl;
if (comp3 = 0)
cout << "sum3 and sum4 are different" << endl;
if (comp3 = 1)
cout << "sum3 and sum4 are the same" << endl;

to

if (comp1 == 0)
cout << "sum1 and sum2 are different" << endl;
else
cout << "sum1 and sum2 are the same" << endl;
if (comp2 == 0)
cout << "sum2 and sum3 are different" << endl;
else
cout << "sum2 and sum3 are the same" << endl;
if (comp3 == 0)
cout << "sum3 and sum4 are different" << endl;
else
cout << "sum3 and sum4 are the same" << endl;

for the output to display properly. Guess that's a lesson for me. Better to use if/else then all ifs...

I wonder why I got this with the original couts...

The value of sum1 is 225
The value of sum2 is 225
The value of sum3 is 225
The value of sum4 is 55
sum1 and sum2 are the same
sum2 and sum3 are the same
sum3 and sum4 are different
Press any key to continue . . .


I'm just musing at this point. My question has been answered :)!!!
Hmm... thought I mentioned that at the end of my post... *re-reads* Yeah I did, I'll make sure things like that end up first next time :P

OH! and just a heads up, don't mix the parameter call methods. I.e. is you want one to be int& they all must be type& in order for the function to work properly, even if you don't change the value in some of the parameters :~
Topic archived. No new replies allowed.