Hello, code is giving issues need help figuring it out. I posted the code and project prompt

Question

Hello, code is giving issues need help figuring it out. I posted the code and project prompt.

#include <iostream>
using namespace std;
int min(int, int, int);
int mid(int, int, int);
int max(int, int, int);
int main()
{
int p, q, r;
int testCase = 0;
cout << "Welcome to the Number Game of Joe!" << endl;
while(true)
{
cout << ++testCase << " ========================= " << endl;
cout << "Please enter 3 integer numbers in any order (-999 to stop): ";
cin >> p >> q >> r;
if (p == -999 || q == -999 || r == -999)
{
cout << ++testCase << " ========================= " << endl;
cout << "Thank you for playing this Number Game of Joe!" << endl;
cout << ++testCase << " ========================= " << endl;
break;
}
int sum = p + q + r;
int average = sum/3;
cout << "You entered 3 numbers: " << p << " , "<< q << "," << r << endl;
cout << "These 3 numbers in order are: " << min(p, q, r) << ", " << mid(p, q, r) << mid(p, q, r) << ", " << max(p, q, r) << endl;
cout << "The sum is " << sum << " and the average is " << average << endl;
}
return 0;
int max(int p, int q, int r)
{
int max = 0;
if (p > q && p > r)
max = p;
else if (q > p && q > r)
max = q;
else
max = r;
return max;
}
int mid(int p, int q, int r)
{
int mid = 0;
if ((q >= p && q <= r) || (r <= q && q <= p)
mid = q;
else if ((q <= p && p <= r)) || (r <= p && p <= q))
mid = p;
else
mid = r;
return mid;
}
int min(int p, int q, int r)
{
int min = 0;
if (p < q && p < r)
min = p;
else if (q < p && q < r)
min = q;
else
min = r;
return min;
}
}

Please enter 3 integer numbers in any order

You must enter 3 numbers: 7,9,6
These 3 numbers in order are 6,7,9
The sum is 22 and the average is 7

Please enter 3 integer numbers in any order

You just entered 3 numbers: 7,9,7
These 3 numbers in order are 7,7,9
The sum is 23 and the average is 7

Please enter 3 integer numbers in any order

You just entered 3 numbers: 9,9,9
These 3 numbers in order are 9,9,9
The sum is 27 and the average is 9

Please enter 3 integer numbers in any order

you just entered 3 numbers: 7,6,5
These 3 numbers in order are 5,6,7
The sum is 18 and the average is 6

Please enter 3 integer numbers in any order

you just entered 3 numbers: 3,4,5
These 3 numbers in order are 3,4,5
The sum is 12 and the average is 4

Please enter 3 integer numbers in any order

you just entered 3 numbers: 5,9,7
These 3 numbers in order are 5,7,9
The sum is 21 and the average is 7

Summary

Here in the program, we find some errors Such as missing semicolons in many places, brackets in the main function which is used to close the method. And also the function which should be called once is getting call double for every test case included. These were the errors in the program but after all the errors are fixed the program code is running well.

Explanation

Program is in a way that user will enter the 3 values and while printing the values that three number will get sort in addition to that sum and average of that 3 number will also get printed on the screen as output. And if the user wants to close or stop the program execution he must enter the -999 for that. So that user can test the cases as he wants.

Code

#include <iostream>
using namespace std;
/* function headers */
int min(int, int, int);
int mid(int, int, int);
int max(int, int, int);
/* driver code */
int main()
{
int p, q, r;
int testCase = 0;
cout << "Welcome to the Number Game of Joe!" << endl;
while(true)
{
cout << ++testCase << " ========================= "<<endl;
cout<<"Please enter 3 integer numbers in any order (-999 to stop): ";
cin >> p >> q >> r;
if (p == -999 || q == -999 || r == -999)
{
cout << ++testCase << " ========================= " << endl;
cout << "Thank you for playing this Number Game of Joe!" << endl;
cout << ++testCase << " ========================= " << endl;
break;
}

int sum = p + q + r;
int average = sum/3;

cout << "You entered 3 numbers: " << p << ", "<< q << ", " << r << endl;
cout << "These 3 numbers in order are: " << min(p, q, r) << ", "<< mid(p, q, r);
cout<< ", " << max(p, q, r) << endl;
cout << "The sum is " << sum << " and the average is " << average << endl;
}

return 0;
}
/* to return the maximum of three numbers */
int max(int p, int q, int r)
{
int max = 0;
if (p > q && p > r)
max = p;
else if (q > p && q > r)
max = q;
else
max = r;
return max;
}
/* to return the middle value of three numbers */
int mid(int p, int q, int r)
{
int mid = 0;
if ((q >= p && q <= r) || (r <= q && q <= p))
mid = q;
else if((q <= p && p <= r) || (r <= p && p <= q))
mid = p;
else
mid = r;

return mid;
}
/* to return the minimum of three numbers */
int min(int p, int q, int r)
{
int min = 0;
if (p < q && p < r)
min = p;
else if (q < p && q < r)
min = q;
else
min = r;
return min;
}

Output

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *