Trouble converting a program into user-defined functions

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.

#include <iostream>
#include <string>
#include <cstdlib> // re. atoi(..)

using namespace std;

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

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

double biggest(
int scanNum = getInt());{
if(scanNum > bigNum) (bigNum = scanNum);
return bigNum;
}
}

any help would be appreciated
I'd write three functions:

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.
Can you elaborate please? I'm trying to understand what you mean but I don't get how to do it sorry i'm retarded.
Please be clear and not to fast...

i can understand you...

arrange your code first..
Do you understand 1), 2), and 3)? I can't be much more explicit than that without writing the code for you.

Once you've done that, the rest of the program is fairly small.
that's my problem, I don't know how to write a complex program correctly I couldn't find any good examples for me to know how to
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.
Sorry i don't follow how to do that
Ok, how could you have written the code you pasted above if you cannot write the function I suggested above?

The minimum of A, B, and C is equal to the min( A, min( B, C ) ).
Can you write an if-statement that determines what the minimum of B and C is?

trying to do this I think is right but i keep getting this error (14) : error C2064: term does not evaluate to a function taking 1 arguments

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
 #include <iostream>
#include <string>
#include <cstdlib> // re. atoi(..)

using namespace 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 ...
            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;
    }
}

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


    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)
Last edited on
my bad, i got it to work meant to say that was the one that i got working copied and pasted the wrong thing
oh wow didn't even copy the right program oh well
Topic archived. No new replies allowed.