Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

HElp with functions

Need help with a function!!!

I need help with the very last function at the bottom called COMPARE...it has really been getting on my nerves because it is saying i have not initialized one of my variables...

The program is designed to prompt the user to enter positive numbers
//less than a thousand and continue until the user enters 0.
//Then it determines the 2 biggest and the 2 smallest of the input numbers.
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;

void bigs (int,int&,int&);
void smalls (int,int&,int&);
void compare (int&,int&,int&,int&,int&);

int main ()
{
int number ;
int counter;
int a = 1001;
int b = 1001;
int x = 0;
int y = 0;

cout << "Enter a number:";
cin >> number;

while (number>=1 && number<=1000)
{
counter++;
bigs(number,a,b);
smalls(number,x,y);

if (counter>=3)
compare(number,a,b,x,y);

cout << "Enter a number:" << endl;
cin >> number;
}

return 0;
}

void bigs(int number, int& a, int& b)
{
if(a<number)
a=number;
}
void smalls(int number, int& x, int& y)
{
if(x<number)
x=number;
}
void compare(int& number, int& a, int& b, int& x, int& y)
{
cout<<a-number<<endl;
cout<<b-number<<endl;
cout<<number-x<<endl;
cout<<number-y<<endl;
}
[1591 byte] By [cmoney24] at [2007-11-11 10:24:21]
# 1 Re: HElp with functions
peace!!
did you try to initialize the varaiable counter and number?
int counter=0;
int number=0;
the it would be better to define them as a static int ?
good luck
peace_comp at 2007-11-11 20:58:54 >
# 2 Re: HElp with functions
counter is the issue. you read in number so thats fine. But you ++ counter when it has never been given a value.
c++ often gives errors in the wrong place, you get used to it. The function you have is fine.
jonnin at 2007-11-11 20:59:54 >