[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
Solved

finding high and low of numbers.

Posted on 1998-09-13
Medium Priority
251 Views
I have a little problem with this little program
all I have to do is accept numbers from the user
have a running total and then print to screen
the sum, the highest and lowest of the numbers entered.
using cin>>  and cout<<
this is what I have so far  #include<iostream.h>
void main()
{
int n =0;
int high=0;
int low =0;

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

}

if (x>high)  //this is where the problem is
{              // it is always zero
low=x;
}
n +=x;
}

cout << n << endl;
cout << high<< endl;
cout << low << endl;
}
0
Question by:strmtrpr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2
• 2

LVL 7

Accepted Solution

faster earned 400 total points
ID: 1172622
if (x>high)  //this is where the problem is
{              // it is always zero
low=x;
}

It should be

if (x<low)  //this is where the problem is
{              // it is always zero
low=x;
}

Besides. you have to set the init value of low and high correctly.  For example, if you only expect  x to be from 0 - 10, set high low than 0 and low greater than 10.
0

Author Comment

ID: 1172623
I must have missed typed the code on here the actual code reads (x<low)
you made a light bulb go off when you wrote set int high less than zero.
so I tried
int high -999;
int low 1000;
still getting a zero for low.
thanks for the help
0

LVL 84

Expert Comment

ID: 1172624
Is there anything in the input after the last non-zero number?
(what happens if you change
int x=0;
to
int x=low;
?)
0

Author Comment

ID: 1172625
I figured it out with some help  you got me thinking and prasanth help with some code he sent
the problem  he figured  was that EOF was sending a zero .
his code worked in a round about way
I started thinking and figurred it out
while(!(cin>>x).eof)
thanks for your help now I can get some sleep.
0

LVL 7

Expert Comment

ID: 1172626
as ozo said, you probably have a blank line or something, but not necessarily after your last non-zero number.
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses
Course of the Month13 days, 3 hours left to enroll