Well, I had this code done to find the biggest number out of a set amount of numbers and one to get smallest out of an unset amount of numbers and I need to change the biggest and smallest to use user defined functions, this is confusing me, I know how to do simple functions, addition subtraction etc but this one got me mixed up here's what i tried which failed.
int aNum;
int scanNum;
int getInt()
{
string tmp;
for(;;)
{
bool ok = true;
cin >> tmp;
for( unsigned i=0; i<tmp.length(); ++i )
{
if( i == 0 && tmp[0]=='-' ); // that's ok too ...
else if( tmp[i]<'0' || tmp[i] > '9' )
{
cout << "Error ... integers only please : ";
ok = false;
break; // out of inner for loop
}
}
if( ok ) // then we have a good integer ... so exit function now
{
return atoi( tmp.c_str() );
}
// ... else do outer 'forever loop' again ...
}
}
void getBiggest()
{
cout << "Please enter the number of integers you want to enter : ";
int enteredNum = getInt();
if (enteredNum > 0)
{
cout << "Enter integer #1 : ";
int bigNum = getInt(); // get first number into bigNum ...
// now ... get the rest ... and compare each ...
for(int i = 1; i < enteredNum; i++)
{
cout << "Enter integer #" << i+1 << " : ";
int scanNum = getInt();
if(scanNum > bigNum) (bigNum = scanNum);
}
cout << "Your largest integer is " << bigNum << "." << endl;
}
}
void getSmallest()
{
cout << "Please enter the first number to compare : ";
int smallNum = getInt();
cout << "Now enter the rest of the numbers end with -99 when in a space between the next number.\n";
do
{
cout << "Next number: ";
int scanNum = getInt();
cin >> aNum;
if (scanNum < smallNum) smallNum = scanNum; // update smallNum ...
int main()
{
for(;;) // loop forever ... until return from main ...
{
cout << "\nPlease enter one of the following ...\n"
<< "A - Find the largest number in a pre-fixed quantity of numbers.\n"
<< "B - Find the smallest number in a user terminated input loop of numbers.\n"
<< "C - Quit.\n" << endl;
string input;
cin >> input;
if (input=="A" || input=="a") getBiggest();
else if (input=="B" || input=="b") getSmallest();
else if (input=="C" || input=="c")
{
cout << "Press 'Enter' to terminate the program ... " << flush;
// while( cin.get() != '\n' ); // if you have a non standard compiler //
cin.sync(); // flush cin stream ...
cin.get();
return 0;
}
}
1) ReadIntegers, which takes no input parameters and returns the
integers that we're read in from the user;
2) FindSmallest, which takes the 'set of integers' as input and returns
the smallest integer;
3) FindLargest, which takes the 'set of integers' as input and returns
the largest integer.
You could also write a fourth function which prints out the menu and
gets the user's choice.
In your code above, you have combined the user input code with the
code to find the smallest and biggest. The result is that you essentially
had to write the user input code twice -- once in the smallest function
and once in the largest.
I would also not have any global variables defined. Have functions
return values instead of storing their results in global variables.
Don't think about the "whole program" at once. Just think about each individual function.
Write this function:
1 2 3
int smallest( int a, int b, int c ) {
// Return the smallest of a, b, and c.
}
Once you've got that, generalize the function to take any number of integers as input. To do that, you'll need to pass an array of integers to the function instead of a, b, and c.
#include <iostream>
#include <string>
#include <cstdlib> // re. atoi(..)
usingnamespace std;
int aNum;
int scanNum;
int bigNum;
int getInt();
double biggest
(
(scanNum > bigNum)(bigNum = scanNum)
);
{
string tmp;
for(;;)
{
bool ok = true;
cin >> tmp;
for( unsigned i=0; i<tmp.length(); ++i )
{
if( i == 0 && tmp[0]=='-' ) ; // that's ok too ...
elseif( tmp[i]<'0' || tmp[i] > '9' )
{
cout << "Error ... integers only please : ";
ok = false;
break; // out of inner for loop
}
}
if( ok ) // then we have a good integer ... so exit function now
{
return atoi( tmp.c_str() );
}
// ... else do outer 'forever loop' again ...
}
}
void getBiggest()
{
cout << "Please enter the number of integers you want to enter : ";
int enteredNum = getInt();
if (enteredNum > 0)
{
cout << "Enter integer #1 : ";
int bigNum = getInt(); // get first number into bigNum ...
// now ... get the rest ... and compare each ...
for(int i = 1; i < enteredNum; i++)
{
cout << "Enter integer #" << i+1 << " : ";
int scanNum = getInt();
if(scanNum > bigNum) (bigNum = scanNum);
}
cout << "Your largest integer is " << bigNum << "." << endl;
}
}
bool more() // defaults to yes ...
{
cout <<"More (y/n) ? ";
string reply;
getline( cin, reply );
return !( reply[0] =='n' || reply[0] =='N' );
}
void getSmallest()
{
cout << "Please enter the first number to compare : ";
int smallNum = getInt();
cout << "Now enter the rest of the numbers end with -99 when in a space between the next number.\n";
do
{
cout << "Next number: ";
int scanNum = getInt();
cin >> aNum;
if (scanNum < smallNum) smallNum = scanNum; // update smallNum ...
}while(aNum != -99);
cout << "Your smallest integer is " << smallNum << "." << endl;
}
int main()
{
for(;;) // loop forever ... until return from main ...
{
cout << "\nPlease enter one of the following ...\n"
<< "A - Find the largest number in a pre-fixed quantity of numbers.\n"
<< "B - Find the smallest number in a user terminated input loop of numbers.\n"
<< "C - Quit.\n" << endl;
string input;
cin >> input;
if (input=="A" || input=="a") getBiggest();
elseif (input=="B" || input=="b") getSmallest();
elseif (input=="C" || input=="c")
{
cout << "Press 'Enter' to terminate the program ... " << flush;
// while( cin.get() != '\n' ); // if you have a non standard compiler //
cin.sync(); // flush cin stream ...
cin.get();
double biggest(
(int scanNum > int bigNum) (int bigNum = int scanNum)
return bigNum;
}
return 0;
}
}
Lines 12-15. What is supposed to happen there? I'm guessing it's a variable declaration or a function but I'm unsure. Add comments, or some kind of comment to explain what is supposed to happen.
Recomment your whole program with what is supposed to happen in every place and repost your code. (We can't guess what is supposed to happen)