Help with this program?

So here it doesn't even run. I think I have the functions mostly right (i just have to get syntax right for middle).

Basically I just have to get the highest, lowest, and middle number from a users input.

I cant get the program to go past the first function.

Can I get some help being put in the right direction?


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


using namespace std;

void setFirst(int& first);
void setSecond(int& second);
void setThird(int& third);

int getFirst(int& first);
int getSecond(int& second);
int getThird(int& third);

int getHighest(int first, int second, int third);
int getLowest(int first, int second, int third);
int getMiddle(int first, int second, int third);

void showResults(int first, int second, int third);

int main(){
   int first;
   int second = 2;
   int third = 3;
   
   setFirst(first);
   showResults(first, second, third);
   cin >> first;
   return 0;

   setSecond(second);
   showResults(first, second, third);
   cin >> second;
   return 0;
   
   setThird(third);
   showResults(first, second, third);
   cin >> third;
   return 0;
   
   int getHighest(int first, int second, int third);
   int getLowest(int first, int second, int third);
   int getMiddle(int first, int second, int third);
   
   void showResults(int first, int second, int third);

}

void setFirst(int& first)
{
   cout << "please enter the first number: ";
   cin >> first;
}

void setSecond(int& second)
{
   cout << "please enter the second number: ";
   cin >> second;
}

void setThird(int& third)
{
   cout << "please enter the third number: ";
   cin >> third;
}

int getFirst(int& first)
{
   return first;
}

int getSecond(int& second)
{
   return second;
}


int getThird(int& third)
{
   return third;
}


int getHighest(int first, int second, int third)
{
   int highest;
   highest = getHighest(first, second, third);
   
   if(getSecond(second)>highest)
      highest = second;
   if(getThird(third)>highest)
      highest = third;
      
   return highest;
}

int getLowest(int first, int second, int third)
{
   int lowest;
   lowest = getLowest(first, second, third);
   
   if(getSecond(second)<lowest)
      lowest = second;
   if(getThird(third)<lowest)
      lowest = third;
      
   return lowest;
}

int getMiddle(int first, int second, int third)
{
   int middle;
   middle = getMiddle(first, second, third);
   
   if(getSecond(second)>middle)
      middle = second;
   if(getThird(third)>middle)
      middle = third;
      
   return middle;
}

void showResults(int first, int second, int third){
   cout << "the highest value was: "
        << getHighest(first,second,third) << endl;
   cout << "the middle value was: "
        << getMiddle(first,second,third) << endl;
   cout << "the lowest value was: "
        << getLowest(first,second,third) << endl;
     
}
Is there a reason why you don't use a vector, min_element and max_element ?
1
2
3
4
5
6
int getLowest(int first, int second, int third)
{
   int lowest;
   lowest = getLowest(first, second, third);
   // ...
}

This is infiinite recursion. Do you see why?

I think you're vastly overcomplicating this. What even is the point of getSecond(second), et. al?
Last edited on
@Ganado No I dont, why is it an infinite recursion? I am pretty new to this and was given a set of tasks @Thomas so we have to do it a certain way.

And to be totally frank with you I am not sure of the reasoning behind getSecond. I'm kind of lost
Last edited on
It's okay, I'll explain.

You're calling showResults, which then calls getHighest, getMiddle, and getLowest. But all 3 of these functions have the same problem, in that they all call themselves repeatedly. When a function calls itself, this is known as recursion. Recursion can be useful in certain cases, but I do not suggest it here, because it's causing an infinite loop for you.

Start here:
1
2
3
4
5
6
7
8
int getHighest(int first, int second, int third) <───┐     
{                                                    │    <-- see this?
   int highest;                                      │
   highest = getHighest(first, second, third); ──────┘

    // [ this line is never reached ]
    if(getSecond(second)>highest)
}


When you call getHighest:
1
2
3
4
5
6
7
call getHighest(first, second, third)
--> internally calls getHighest(first, second, third)
    --> internally calls getHighest(first, second, third)
        --> internally calls getHighest(first, second, third)
            --> internally calls getHighest(first, second, third)
                --> internally calls getHighest(first, second, third)
                    --> { and so on, ad infinitum } 


Basically, the function keeps calling itself, and can never get past that point.
Last edited on
Thank you I see this now. So I got rid of the recursion and updated my syntax but now it inst letting me input other commands, so it takes my first input and immediately spits out random other numbers that do adhere to the syntax but i never get a time to input second and third numbers. here is the updated 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
134
135
136
 #include <iostream>


using namespace std;

void setFirst(int& first);
void setSecond(int& second);
void setThird(int& third);

int getFirst(int& first);
int getSecond(int& second);
int getThird(int& third);

int getHighest(int first, int second, int third);
int getLowest(int first, int second, int third);
int getMiddle(int first, int second, int third);

void showResults(int first, int second, int third);

int main(){
   int first;
   int second;
   int third;
   
   setFirst(first);
   showResults(first, second, third);
   cin >> first;
   
   
   setSecond(second);
   showResults(first, second, third);
   cin >> second;
   
   
   setThird(third);
   showResults(first, second, third);
   cin >> third;

   
   int getHighest(int first, int second, int third);
   int getLowest(int first, int second, int third);
   int getMiddle(int first, int second, int third);
   
   void showResults(int first, int second, int third);

   return 0;
}

void setFirst(int& first)
{
   cout << "please enter the first number: ";
   cin >> first;
}

void setSecond(int& second)
{
   cout << "please enter the second number: ";
   cin >> second;
}

void setThird(int& third)
{
   cout << "please enter the third number: ";
   cin >> third;
}

int getFirst(int& first)
{
   return first;
}

int getSecond(int& second)
{
   return second;
}


int getThird(int& third)
{
   return third;
}


int getHighest(int first, int second, int third)
{
   int highest;
   
   
   if(getFirst(first)>second && third)
      highest = first;
   if(getSecond(second)>third && first)
      highest = second;
   if(getThird(third)>second && first)
      highest = third;
      
   return highest;
}

int getLowest(int first, int second, int third)
{
   int lowest;
   
     
   if(getFirst(first)<second && third)
      lowest = first;
   if(getSecond(second)<third && first)
      lowest = second;
   if(getThird(third)<second && first)
      lowest = third;
      
   return lowest;
}

int getMiddle(int first, int second, int third)
{
   int middle;
      
   if(getFirst(first)>second && getFirst(first)<third)
      middle = first;
   if(getSecond(second)>third && getSecond(second)<first)
      middle = second;
   if(getThird(third)>second && getThird(third)<first)
      middle = third;
      
   return middle;
}

void showResults(int first, int second, int third){
   cout << "the highest value was: "
        << getHighest(first, second, third) << endl;
   cout << "the middle value was: "
        << getMiddle(first,second,third) << endl;
   cout << "the lowest value was: "
        << getLowest(first,second,third) << endl;
     
}
Last edited on
OKay everything is working except for middle. Cant get the syntax right. any suggestions on that? Right now i have

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int getMiddle(int first, int second, int third)
{
   int middle;
      
   if(getFirst(first)<third && getFirst(first)>second); 
      middle = first;
   if(getFirst(first)>third && getFirst(first)<second);
      middle = first;
   if(getSecond(second)<third && getSecond(second)>first);
      middle = second;
   if(getSecond(second)>third && getSecond(second)>first);
      middle = second;   
   if(getThird(third)<second && getThird(third)>first);
      middle = third;
   if(getThird(third)<second && getThird(third)<first);
      middle = third;
      
   return middle;
}

Topic archived. No new replies allowed.