Solved

simple problem

Posted on 1998-09-13
11
198 Views
Last Modified: 2010-04-02
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.
}
0
Comment
Question by:strmtrpr
  • 6
  • 5
11 Comments
 
LVL 2

Accepted Solution

by:
prasanth earned 120 total points
ID: 1172608
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 Comment

by:strmtrpr
ID: 1172609
if(x<low)
      {
        low=x;
        lowidx = n;
      } // still  getting a zero on the output;
 
0
 
LVL 2

Expert Comment

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

Author Comment

by:strmtrpr
ID: 1172611
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.
thank you for your help.
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.

thank you for your time

0
 
LVL 2

Expert Comment

by:prasanth
ID: 1172612
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
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

 

Author Comment

by:strmtrpr
ID: 1172613
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.
thanks for your time anyway.

0
 
LVL 2

Expert Comment

by:prasanth
ID: 1172614
replace

bool with int
true with 1
false with 0

and it should compile.
0
 

Author Comment

by:strmtrpr
ID: 1172615
it compiles but stilling getting a zero for the output of low

0
 
LVL 2

Expert Comment

by:prasanth
ID: 1172616
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 Comment

by:strmtrpr
ID: 1172617
I owe you  big time
thanks very much now I can get some sleep.

0
 

Author Comment

by:strmtrpr
ID: 1172618
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

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

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

746 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

11 Experts available now in Live!

Get 1:1 Help Now