# simple problem

this is a simple problem
I need to find the highest and lowest number of the numbers that have been entered.

#include <iostream.h>

int main(void)
{
int n = 0;
int high=0;
int low =0;

while(!cin.eof())
{
int x = 0;
cin >> x;

if(x>high)
high=n; //this works fine

if(x<low)
low=n;  // I always get a 0
n += x;
}
cout << n << endl;
cout << high<< endl;
cout << low << endl;
return 0;
any help would be great.
}
###### Who is Participating?

Commented:
try

#include <iostream.h>

int main(void)
{
int n = 0;
int high=0;
int low =0;
int highidx = 0;
int lowidx = 0;

while(!cin.eof())
{
int x = 0;
cin >> x;

if(x>high)
{
high=x;
highidx = n;
}
if(x<low)
{
low=x;
lowidx = n;
}
n++;
}
cout << n << endl;
cout << highidx << ":" << high << endl;
cout << lowidx << ":" low << endl;
return 0;
}
0

Author Commented:
if(x<low)
{
low=x;
lowidx = n;
} // still  getting a zero on the output;

0

Commented:
did you enter any negative numbers? If not, 0 (the initial value of low) will end up being the lowest number.
0

Author Commented:
the program is supposed to accept numbers until cin.eof() is encountered
then  display  the higest lowest and sum of the numbers entered
then only reason I am asking for help is because I am out of Ideas, I really think that I am
trying to make this to difficult.
how do I find the lowest number entered.
I would think that it would be the opposite of the high but that isn,t working.

0

Commented:
this should do it

#include <iostream.h>

int main(void)
{
int n = 0;
int high=0;
bool highn = false;
int low =0;
bool lown = false;

while(!cin.eof())
{
int x = 0;
cin >> x;

if(x>high || !highn)
{
high=n;
highn = true;
}

if(x<low || !lown)
{
low=n;
lown = true;
}
n += x;
}
cout << n << endl;
cout << high<< endl;
cout << low << endl;
return 0;

0

Author Commented:
thanks for the help
the last code you sent wouldn't compile under borland c++ 4.0 or g++under linux.
thanks anyway. I am not sure why this is so hard for me to figure out.

0

Commented:
replace

bool with int
true with 1
false with 0

and it should compile.
0

Author Commented:
it compiles but stilling getting a zero for the output of low

0

Commented:
I tried this and it works:

#include <iostream.h>

int main(void)
{
int n = 0;
int high=0;
bool highn = false;
int low =0;
bool lown = false;

while(!cin.eof())
{
int x = 0;
cin >> x;

if (!cin.eof())
{
if(x>high || !highn)
{
high=x;
highn = true;
}

if(x<low || !lown)
{
cout << "here" << x <<endl;
low=x;
lown = true;
}
n += x;
}
}
cout << n << endl;
cout << high<< endl;
cout << low << endl;
return 0;
}

basically what was happening was that when you pressed CTRL+Z to do eof, the character read was EOF which isn't a number and was being translated to 0.
0

Author Commented:
I owe you  big time
thanks very much now I can get some sleep.

0

Author Commented:
if you ever run into this  again try this instead
while(!(cin>>x).eof)
this fixed the original code the funny thing I had the answer sitting in front of the whole time
ans over looked it
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.