geno84
geno84

Reputation: 5

Comparing a user specified amount of numbers inside of a user definded function in c++

first time poster here. Please keep in mind I'm still very new to c++ and coding in general. I'm trying to rewrite a program that asks the user how many numbers they want to compare, then they enter the values of each number. I have achieved this via for and if statements, but i am now asked to do this with user defined functions instead. The problem I have so far is that the first number I enter is the one saved, but it does prompt for additional numbers and the for loop inside of the function works properly. Some suggestions to what I'm not doing correctly would be greatly appreciated. I have all the variables declared that are needed, so i only put the prototype in to save on space.

int main()
{ 

    int DisplayGreatest(int,int);

    do { 
        cout << "Please select an option" << endl;// Menu Options
        cout << "A: Highest" << endl;
        cout << "B:Lowest" << endl;
        cout << "C: Quit " << endl;

        cin >> Selection; // Menu Selection
        cout << endl;

        if ( (Selection == 'a') || (Selection == 'A') )
        {
                cout << "How many numbers do you want to use?" << endl;
                cin >> Aselection;
                cout << "enter your first choice" <<endl;
                cin >> currentAinput;
                    DisplayGreatest (Aselection,currentAinput); 

                cout << "The largest number is "<< currentAinput << endl;
        }
    }
}   

The function:

int DisplayGreatest(int Aselection, int currentAinput) 
{
    int i;
    int keepAinput=0;


    for(i=1;i<Aselection;i++)
        {
            cin >> currentAinput;
            if (currentAinput > keepAinput) 
                {
                        keepAinput=currentAinput; 
                }
        }
    return currentAinput;            
}

Upvotes: 0

Views: 56

Answers (2)

Johan Falk
Johan Falk

Reputation: 4359

You have several errors in your code:

  • First you are not using the returned value from the function DisplayGreatest
  • You are never comparing the first value you enter with the others (you do two cin in a row without before the first comparison)
  • Declaring the function DisplayGreatest in the wrong scope (should be in the global scope, or place the whole function before main)
  • You have no end condition to your do-loop in main. (missing while statement, maybe you have that in your own version)
  • You returned the last value entered in the DisplayGreatest function, not the largest one.

Here is a functional version:

#include <iostream>

using namespace std;

int DisplayGreatest(int Aselection) 
{
    int keepAinput = 0;
    int currentAinput;

    do {
        cin >> currentAinput;
        if (currentAinput > keepAinput) 
        {
            keepAinput = currentAinput; 
        }
    } while(--Aselection > 0);

    return keepAinput;
}

int main()
{
    char Selection;

    do {

        int Aselection;
        int currentAinput;

        cout << "Please select an option" << endl;// Menu Options
        cout << "A: Highest" << endl;
        cout << "B:Lowest" << endl;
        cout << "C: Quit " << endl;

        cin >> Selection; // Menu Selection
        cout << endl;

        if ( (Selection == 'a') || (Selection == 'A') )
        {
                cout << "How many numbers do you want to use?" << endl;
                cin >> Aselection;
                cout << "enter your first choice" <<endl;
                cout << "The largest number is "<< DisplayGreatest (Aselection) << endl;
        }
    } while(Selection != 'C' && Selection != 'c');
}

Upvotes: 1

IllusiveBrian
IllusiveBrian

Reputation: 3214

int DisplayGreatest(int,int) should probably be declared in the global namespace, ie:

int DisplayGreatest(int, int);

int main()
{
}

int DisplayGreatest (int Aselection, int currentAinput)
{
}

Also, you can take in input in a loop but it is somewhat more natural take in a whole vector worth of ints and then find the highest/lowest using the same idea (loop through each one and save the current highest/lowest).

EDIT: Oh, I see what your issue is now. You need to return keepAinput at the end of your function and assign it to currentAinput or another variable and print whichever you assign it to. Or just print the result directly, IE:

cout << "Highest number is " << DisplayGreatest(Aselection, currentAinput) << endl;

Upvotes: 0

Related Questions