I am trying to write a program that allows the user to input positive integers between 0 and 1000 and then outputs the highest value and the lowest value. The user should enter -1 to signify the end of their input. I am supposed to use a loop to achieve this.

I am not allowed to use arrays and I am not sure which loop to use. Here is what I have so far:

#include <iostream>
using namespace std;

int main ()
{
	int first_input, largest, smallest

cout << "Enter numbers between 0 and 1000 and i will tell you the highest and"<<endl;
cout << " lowest numbers. Enter -1 to signal the end of your data entered." << endl;
	cin >> first_input

		if (first_input > 1000)
		{
		cout << "Please enter a positve number lower than 1000." << endl;
			cin >> first_input
		}

Recommended Answers

All 7 Replies

Do you know anything about data structures, because this sounds like homework to me. Anyway I'm not giving you code because of the last statement but I will help you understand what you have to do. You're going to want to put all the numbers in vector, then you are going to want to put the first number of that vector in an integer and compare it to the next value that is in the stack, I would use a for loop. I'll give you code right now because it's hard to describe

int compareValue;
int lowestint;
int highestint;
//inside your while loop
for(int i = 1; i < stack.size();i++)//stack is whatever you put your entered values in
{
compareValue = stack[0];
if(compareValue < stack[i])
{
  lowestint = compareValue;
}
else
{
  lowestint = stack[i];
}
if(compareValue > stack[i])
{ 
  highestint = compareValue;
}
else
{
  highestint = stack[i];
}

}

This is a start, I know I said I wouldn't give you code but it just seems easier to give this part to you and make you figure it out. I may be wrong too but that sounds like the pseudocode you should use when writing the program.

At the end of the program you are going to want to have these two lines to show your highest and lowest values shown:

cout << "Highest value is:" << highestint << endl;
cout << "Lowest value is:" << lowestint << endl;

I don't understand why people (sonsofliberty84) insist on difficult solutions to simple problems, but many people like to water their lawn by blowing up the dam.

A simple array, a simple loop, 2 IF statements, and formatted is all you need. No vectors necessary.

@WaltP

This is what the OP said:

I am not allowed to use arrays and I am not sure which loop to use. Here is what I have so far:

@OP

You could try this:

int min = 1000, max = 0, inputNumber;
 
        do {
                cin >> inputNumber;
 
                if (inputNumber <= min && inputNumber != -1)
                        min = inputNumber;
                
                if (inputNumber >= max)
                        max = inputNumber;
                                                
        } while (inputNumber != -1);
 
        cout << "max:" << max << "\n" << "min:" << min;

It's not a difficult solution when he said he can't use arrays, try reading the whole message that the OP puts up next time

You're right, I missed the array part. But if he can't use an array, why would a vector be OK? Isn't it just a high-faluting array?

That's like saying if you can't use a loop, use recursion... :icon_rolleyes:

After each input, check to see if it's the highest/lowest so far...

if you read my post, I said that it is probably homework for a class, because I had a similar problem back when I was in college and I had to use something other than an array. I would use an array personally but when you're told not to use something it sounds like the person is trying to get other people to do their homework/classwork for them when they should just do their own work or ask a classmate.

And if you read my post you'd see that this can be done without storing any numbers at all -- no arrays, vectors, heaps, nothing. Just read and test, read and test....

1) And if I didn't read your post, how would I have known you suggested a vector? :icon_wink:
2) After 4 years and 5000 posts, do you really think I can't tell a homework problem by now? :icon_rolleyes:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.