Showing highest and lowest (Homework)

Well they want me to keep asking for numbers, once the user puts in a negative number the program is supposed to stop and inform the highest and the lowest number that the user provided. But the thing is, I am not sure if the negative number counts and so far I got it working if that's the case.
Can someone help me with getting the lowest not to get the negative number?

here's what I have so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;
int main()

{
float A, LOW=1000000, HIGH=0;

do
  {
      cout << "Please pick any number: " <<endl;
      cin >> A; 
      if (A>=HIGH)
      {
         HIGH = A;
         cout << endl << "NEW HIGH" << endl << endl;
      }
      if (A<=LOW)
      {
         LOW = A;
         cout << endl << "NEW LOW" << endl << endl;
      }
  }
while (A>0);
cout << "The highest number you picked was: " << HIGH << endl;
cout << "The lowest number you picked was: " << LOW << endl;
system ("PAUSE");
return 0;
Last edited on
It looks like it does count. Check to make sure the number is not negative BEFORE you run the comparisons, and if so, break the loop.

-Albatross
I got it, thanks for the help :P simple stuff just didn't think the right way.


SOLUTION
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
using namespace std;
int main()

{
float A, LOW=1000000, HIGH=0;

cout << "Please pick any number: " <<endl;
cin >> A; 
if (A>=0)
{
   do
     {
         cout << "Please pick any number: " <<endl;
         cin >> A; 
         if ((A>=HIGH) && (A>=0))
            {
            HIGH = A;
            cout << endl << "NEW HIGH" << endl << endl;
             }
            if ((A<=LOW) && (A>=0))
            {
             LOW = A;
              cout << endl << "NEW LOW" << endl << endl;
          }
          }
   
while (A>0);
}
cout << "The highest number you picked was: " << HIGH << endl;
cout << "The lowest number you picked was: " << LOW << endl;
system ("PAUSE");
return 0;
}
Extra detail: What if A is equal to zero? That would terminate your loop.

-Albatross
This was such a hack-y and pointless solution that I almost cried...
You just placed an if around a do...
Correct me if I'm wrong, the point of using the do is so that the check is performed after the code is run. Don't put an if around it. Use a while loop.
Real Solution :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace std;
int main()

{
int A, LOW=std::numeric_limits<int>::max(), HIGH=0;
// why were you using a float there? seems kinda weird...
// I changed your low to be the highest integer the computer can handle. :3

cout << "Please pick any number: " <<endl;
cin >> A; 
while (A>=0)
{
         cout << "Please pick any number: " <<endl;
         cin >> A; 
         if ((A>=HIGH) && (A>=0))
            {
            HIGH = A;
            cout << endl << "NEW HIGH" << endl << endl;
             }
            if ((A<=LOW) && (A>=0))
            {
             LOW = A;
              cout << endl << "NEW LOW" << endl << endl;
          }
          }

cout << "The highest number you picked was: " << HIGH << endl;
cout << "The lowest number you picked was: " << LOW << endl;
getch(); // Don't use system... :S
return 0;
}
Last edited on
lol that made me laught a good bit.
well everything worked out besides the getch(); part. why should I use system?
and I also fixed some stuff up on the code since it was saying stuff i didnt want it to.
i just left the system in becuase i dont know what it does, and the float thing was becuase i didnt know what to put, thanks for the tips, i was wondering if i could put it to the limit.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace std;
int main()

{
float A, LOW=std::numeric_limits<int>::max(), HIGH=0;

cout << "Please pick any number: " <<endl;
cin >> A; 
HIGH = A;
cout << "FIRST NUMBER" << endl << endl;
while (A>=0)
     {
         cout << "Please pick any number: " <<endl;
         cin >> A; 
         if ((A>=HIGH) && (A>=0))
            {
            HIGH = A;
            cout << endl << "NEW HIGH" << endl << endl;
             }
            if ((A<=LOW) && (A>=0) && (A<HIGH))
            {
             LOW = A;
              cout << endl << "NEW LOW" << endl << endl;
          }
          }

cout << "The highest number you picked was: " << HIGH << endl;
cout << "The lowest number you picked was: " << LOW << endl;
system ("PAUSE");
return 0;
}


please give me some more adive if you see anything wrong, thats the easiest way to learn.
Most people here hate system() because while it provides lots of extra functionality without writing extra code, it has horrible overhead, is a potential security hole, and above all is platform dependent.

Seriously, don't use system() if you want to avoid getting bashed on this forum.

-Albatross
new problem, whenever I put in numbers but they only go up (15, 17, 18, -1) it gives me a weird error as the lowest... how do i fix it now?

i tried putting LOW = 0 in the begging but then it just kept 0 as the lowest the whole time.

what do i use instead of system() though? the other one on the code that he wrote doesnt work.
Last edited on
(15, 17, 18, -1)


The problem here is that the first number entered is not counting towards the LOW, but it is counting towards HIGH. Since all the subsequent non-negative numbers are above the initial number, nothing is being assigned to LOW. Consequently LOW is just outputting whatever value is in the memory.
Sorry, that was my bad.
I forgot that it was a part of conio.
Add the following after your first include: #include <conio.h>
Then getch(); will work.
Basically, what getch does is it takes the input of one character.
So the program will hang until a key is pressed.
Last edited on
Topic archived. No new replies allowed.