Solved

finding high and low of numbers.

Posted on 1998-09-13
5
243 Views
Last Modified: 2010-04-02
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<<  
if anyone has an idea please help.
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
Comment
Question by:strmtrpr
  • 2
  • 2
5 Comments
 
LVL 7

Accepted Solution

by:
faster earned 100 total points
Comment Utility
 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

by:strmtrpr
Comment Utility
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

by:ozo
Comment Utility
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

by:strmtrpr
Comment Utility
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
it was this simple
while(!(cin>>x).eof)
thanks for your help now I can get some sleep.
0
 
LVL 7

Expert Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now