Reputation: 5
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
Reputation: 4359
You have several errors in your code:
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
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