Hey all this is the problem i am currently stuck on; in fact my whole class doesn't get it...

Write a C++ program with a loop that asks user to enter a series of integers in the range of [-100 100]. If user enters a number out of that range, your program shouldn’t consider it, and should ask for a new number. The user should enter 0
to stop the loop. When the loop stops, your program must display:
• the largest TWO numbers entered in the range of [-100 100],
• the smallest TWO numbers entered in the range of [-100 100],
• the average of negative numbers entered in the range of [-100 100],
• the average of positive numbers entered in the range of [-100 100].

``````int max1, max2, min1, min2,
int poscount, negcount;
double sumpos, sumneg, avgpos, avgneg;

while (num1= 0)
{int max1, max2, min1, min2
int poscount, negcount;
double sumpos, sumneg, avgpos, avgneg;
cin >> num;
while {(num < -100) || num > 100);}
cout << "Enter number again\n"
cin >> num;}``````

This is what i have am i anywhere near right i know i have some errors but can anyone help me solve this!!!!!!

## All 8 Replies

Well its a start, but your current code has several errors.
Line 6 will give you errors because num1 has not been declared. You should change it to num and declare it before the loop. Also you must use

``while(num!=0)``

You want the loop to continue until the user enters 0.
Lines 7, 8, and 9 are not needed as they are just redeclarations of variables.
Compare your code with mine and see if you can understand your mistakes. You need and if/else instead of another while loop. You want to test the values. If they are not valid you print an error. If they are valid, you ask for another entry.

``````int num;
while (num != 0)
{
cin >> num;
if (num < -100 || num > 100)
{
cout << "Enter number again\n"<<endl;
}
else
{
cout<<"enter another number"<<endl;
}
}``````

However, if you want to use the number when after entry, you should consider using an array. This way, you store all the values in the array and can test which value is smallest, largest, etc.

Thanks again for your help. I cannot use arrays because we have yet to learn them in class. I will compose this code later and see if this works I will let you know.

ok when i complied this code it said that there was an expected unqualified id before while in line 2

Can you post the new code please. I cant see whatever line 2 is in your program and therefore cant resolve the problem.

In this case it would be line 5 in the code.

``````int max1, max2, min1, min2, num;
int poscount, negcount;
double sumpos, sumneg, avgpos, avgne;

while (num != 0)
{
cin >> num;
if (num < -100 || num > 100)
{
cout << "Enter number again\n"<<endl;
}
else
{
cout<<"enter another number"<<endl;
}
}``````

In this case it would be line 5 in the code.

``````int max1, max2, min1, min2, num;
int poscount, negcount;
double sumpos, sumneg, avgpos, avgne;

while (num != 0)
{
cin >> num;
if (num < -100 || num > 100)
{
cout << "Enter number again\n"<<endl;
}
else
{
cout<<"enter another number"<<endl;
}
}``````

Im using the following code and it compiles fine

``````#include <iostream>
using namespace std;
int main()
{
int max1, max2, min1, min2, poscount, negcount, num;
double sumpos, sumneg, avgpos, avgneg;

while (num != 0)
{
cin >> num;
if (num < -100 || num > 100)
{
cout << "Enter number again"<<endl;
}
else if(num !=0)
{
cout<<"enter another number"<<endl;
}
}

char wait;
cout<<"enter any key to exit"<<endl;
cin>>wait;
return 0;
}``````

ok thanks a lot i figured it out.

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.