what wrong here

ok im trying to fill an array with numbers this part works but i need to numbers to be in a specific range (-15 - 130) if not in this range it needs to not accept the number and ask for another ...however this does not work
the code thus 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 ()
{
	const int tmp_num=24;
	int hr_tmps[tmp_num];
	int x=0;
    cout<<"please enter a temp for each hour of the day\n please use only #'s between -15 and a 130\n";
	
    for ( x=0; x<tmp_num; x++)
	{

	cin>>hr_tmps[x];

	if (hr_tmps[x] >= -15 || hr_tmps[x] > 130)
	{
		
		cin>>hr_tmps[x];
		cout<< "That number is out of range,Please enter as number between -15 and a 130 degrees"<<endl;
		x=0;
	}

	}
	
return 0;	
}
Two suggestions:
1. change hr_tmps[x] > 130 on line 15 to hr_tmps[x] < 130
2. change your if statement to a loop so that you can ask to change the input until a valid number is given. The way you have it set up now is so that you will have to start over when you enter invalid input. How terrible would it be to be at the 23rd hour and have to start over?
I think I get what your saying try'd to change it to a while loop and no dice just make every input wrong
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

#include<iostream> 
using namespace std;
int main ()
{
	const int tmp_num=24;
	int hr_tmps[tmp_num];
	int x=0;
    cout<<"please enter a temp for each hour of the day\n please use only #'s between -15 and a 130\n";
	
    for ( x=0; x<tmp_num; x++)
	{

	   cin>>hr_tmps[x];

	   while (hr_tmps[x] > -15 || hr_tmps[x] < 130) 
	     {
            
		    cout<< "That number is out of range,Please enter as number between -15 and a 130 degrees"<<endl;
		    x=0;
		    cin>>hr_tmps[x];
	      }
	
	}
	cout<< "the array numbers are\n"<<endl;
	for ( x=0; x<tmp_num; x++)

	cout<<hr_tmps[x]<<"\n";
return 0;	
}
though if i do this i get it to read the wrong negative but it still starts to fill the array over
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
#include<iostream> 
using namespace std;
int main ()
{
	const int tmp_num=24;
	int hr_tmps[tmp_num];
	int x=0;
    cout<<"please enter a temp for each hour of the day\n please use only #'s between -15 and a 130\n";
	
    for ( x=0; x<tmp_num; x++)
	{

	   cin>>hr_tmps[x];

	   while (hr_tmps[x] < -15) 
	     {
            
		    cout<< "That number is out of range,Please enter as number between -15 and a 130 degrees"<<endl;
		    x=0;
		    cin>>hr_tmps[x];
	      }
	
	}
	cout<< "the array numbers are\n"<<endl;
	for ( x=0; x<tmp_num; x++)

	cout<<hr_tmps[x]<<"\n";
return 0;	
}
Get rid of line 20. Your're while statement currently saying that while the tempurature is between -15 and 130 degrees force the user to change their input. To fix this add an ! in front of the parenthesis and your code should work perfectly.
Topic archived. No new replies allowed.