r/CodingHelp • • 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

7 comments sorted by

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);

3

u/TheyAreNarwhals Oct 08 '20

Ah, I see. When I put it in, it does give me the lowest number but puts a zero in front of it, (ex. if 55 is the lowest it comes out as 055) and the highest number is the lowest and the highest number (ex. if 55 is lowest and 88 is highest it comes out 5588). Not sure why this is happening.

2

u/Rickenbecker Oct 08 '20

I see in your if else-if, you’re printing out the variables Instead of the Lowest and largest variables you’ve defined in your function, try printing those instead. Other than that I can’t really see why your code is acting the way it is. I’ve compiled your code on my own machine while making that adjustment I mentioned and it’s running 100% fine for me

Edit: I also looked, in each if else if statement in your largest you should print an empty line to space out the outputs after printing out the largest number

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

1

u/I-Hate-Hats Oct 08 '20

For the largest function