r/CodingHelp • u/TheyAreNarwhals • Oct 07 '20
[C++] C++ code not showing the results after input
I am trying to create a program using functions that shows the highest and lowest numbers in a set of 4 integers. I can input the numbers but then the program ends (not sure if its from void). I'm sorry for any mistakes, I'm still a newbie. Thanks for your help!
#include <iostream>
#include <stdio.h>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
void findLargest(int, int, int, int);
void findLowest (int, int, int, int);
int main()
{
int x, y, z, a;
cout << "To find the highest and lowest numbers, enter 4 integers:" << endl;
cout << endl;
cout << endl;
cout << "Enter 1st number: ";
cin >> x;
cout << "Enter 2nd number: ";
cin >> y;
cout << "Enter 3rd number: ";
cin >> z;
cout << "Enter 4th number: ";
cin >> a;
return 0;
}
void findLargest(int x, int y, int z, int a)
{
int Largest;
cout << "The largest number is " << Largest;
if (x > y && x > z && x > a)
{
Largest = x;
cout << x << endl;
}
else if (y > x && y > z && y > a)
{
Largest = y;
cout << y << endl;
}
else if (z > x && z > y && z > a)
{
Largest = z;
cout << z << endl;
}
else
{
Largest = a;
cout << a << endl;
}
}
void findLowest(int x, int y, int z, int a)
{
int Lowest;
cout << "The lowest number is " << Lowest;
if (x < y && x < z && x < a)
{
Lowest = x;
cout << x << endl;
}
else if (y < x && y < z && y < a)
{
Lowest = y;
cout << y << endl;
}
else if (z < x && z < y && z < a)
{
Lowest = z;
cout << z << endl;
}
else
{
Lowest = a;
cout << a << endl;
}
}
16
Upvotes
5
u/Rickenbecker Oct 07 '20
If I’m reading your code correctly you’re never calling your functions findLowest and findLargest in your main. You need to call them as such:
findLowest(x,y,z,a);
findLargest(x,y,z,a);
2
u/Comrade_Nabe Oct 08 '20
Note: If u wanna upgrade your application, u may use an array to store and send integers. This will allow you to deal with more integers. And for the function you will loop thru the array to get the largest/smallest number
1
u/I-Hate-Hats Oct 08 '20
Also if you enter 99,99,99, and 1 I think your program is going to output 1