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
#include<iostream>
usingnamespace std;
int main ()
{
constint 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?
#include<iostream>
usingnamespace std;
int main ()
{
constint 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;
}
#include<iostream>
usingnamespace std;
int main ()
{
constint 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.