# Thread: loop to get greatest and least number, its not working please help

1. ## loop to get greatest and least number, its not working please help

I can not seem to get this program to work, I am having no luck with loops for anything, very lost. This is my latest attempt and it is not working. Any help I can get would be greatly appriciated. Thank you in advance.
Code:
```// This program should use a loop that lets the user enter a series of integers.
// The user should enter -99 to signal the end of the series.
// After all the numbers have been entered,
// the program should display the largest and smallest numbers entered.

#include <iostream?
using namespace std;

int main()

{
//enter up to 5 integers
int least, int greatest;        //determine least and greatest

double num1 = 0;
double num2 = 0;
double num3 = 0;
double num4 = 0;
double num5 = 0;

cout << " Please enter up to 5 integers:\n ";
cin >> num1,  num2,  num3, num4, num5;

cout << " The integer with the least value is: \n";
cin >>  least;

cout << " Of the integers you choose the one with the greatest value is: \n"
cin >>  greatest;

if ( num1 < num2 )
least = num1;

else if ( num2 < num3)
least = num2;

else if ( num3 < num4)
least = num3;

else if ( num4 < num5)
least = num3;

else if ( num 5 < num1 )
least = num5

if ( num1 > num2 )
greatest = num1;

else if ( num2 > num3)
greatest = num2;

else if ( num3 > num4)
greatest = num3;

else if ( num4 > num5)
greatest = num3;

else if ( num 5 > num1 )
greatest = num5

if( user input == -99 ) break;

return 0;

}```

2. For starters a loop will need a loop construct such as for or while.

Find the missing semicolon(s) to make this code compile.

3. Also, do post the errors that you get!

QuantumPete

4. Code:
`if( user input == -99 ) break;`
What's this supposed to be? It's two variables (user, input) that haven't been declared at all.

--
Mats

5. And whilst it's not REALLY wrong (it would be worse the other way around), your text says "Enter 5 integers", whilst your variables are of the type double, which means that they are floating point variables - it's a bit confusing.

--
Mats

6. > #include <iostream?
Perhaps the first trivial problem?
Replace the ? with a >

Just so you know, just saying "there are errors" isn't good enough sometimes. You need to be specific, by posting actual compile logs for example.

7. The list of 'if else's is really quite a bad way of doing things, you need to loop. And don't do num1, num2, ect, rather do num[5].

8. Perhaps you should have a look at some tutorials, such as

9. Here. As you can see, loops make my code alot smaller and more flexible.
Code:
```// By Yarin, public domain.

#include <iostream>
using namespace std;

int main(void)
{
int amount;
cout << "How many arguments are you inputing? ";
cin >> amount;

int *num = new int[amount];
int x;

cout << "Enter your arguments: ";
for(x = 0; x < amount; x++)
cin >> num[x];
cout << "Arguments submited..." << endl;

// Now we find the max and min numbers:
int max = 0, min = 0;
for(x = 0; x < amount; x++)
{
if(num[x] > num[max]) max = x;
if(num[x] < num[min]) min = x;
}

cout << "The largest argument submited is " << num[max] << "." << endl;
cout << "The smallest argument submited is " << num[min] << "." << endl;

delete [] num;
cin.get();
return 0;
}```
I'm in an okay mood today. So you don't expect handouts from everyone on the board like this.
Welcome to the boards btw.

10. A warning: Yarin's code uses variable length arrays, which is a non-standard feature provided as an extension by some compilers. getch() is also non-standard and probably unnecessary in this case.

11. It wouldn't even compile.
Code:
`for(x = 0; x < amount, x++)`
->
Code:
`for(x = 0; x < amount; x++)`
And what's this? . . .
Code:
```   cout << "The largest argument submited is " << num[max] << "." << endl;
cout << "The smallest argument submited is " << num[max] << "." << endl;```
 Also, getch() would likely be in <conio.h>. But cin.get() is a much better idea anyway. [/edit]

12. True. Hey, I didn't compile to check, and those 3 problems are very small. Fixed anyway.

13. I didn't compile to check, and those 3 problems are very small.
I thought "they are there to test stressedstudent" would be a better comeback

Honestly though, fix the use of VLAs (variable length arrays) and remove conio.h. It is a bad habit to inculcate in a stressed student.

14. Don't change the VLA's to dynamic array, change it to a vector. There's no good reason to use a dynamic array over a vector here.

15. Good now? What are VLAs?

Popular pages Recent additions