# Small Logic Issue

This is a discussion on Small Logic Issue within the C++ Programming forums, part of the General Programming Boards category; Or atleast thats what I THINK the problem is. Here is the function I am having toubles with. The idea ...

1. ## Small Logic Issue

Or atleast thats what I THINK the problem is. Here is the function I am having toubles with.

The idea is that two numbers are entered, and the code should check to see how many numbers in the array are within those two numbers.

The array was sorted from least to highest in a different function. I always end up with the program only returning '1' for the number of students with in the range... which leads me to believe that my logic in the for loops are incorrect.

Any ideas? Attached is the entire program.

Code:
```void RangeData(int *grades, int numStudents)
{
int studentNumber;
int highRange = 0;
int lowRange = 0;
int lowRangeReturn = 0;
int highRangeReturn = 0;
char rangeAgain;

cout<<"\n\nEnter the low value for a grade range look up: ";
cin>>lowRange;
cout<<"Enter the high vaule for a grade range look up: ";
cin>>highRange;

for (studentNumber = 0; lowRange == studentNumber; studentNumber++)
{
{
}
}//find low range number

for (studentNumber = 0; highRange == studentNumber; studentNumber++)
{
{
}
}//find high range number

cout<<"\nThere are "<<gradesInRange<<" students within that range."<<endl;
cout<<"\nWould you like to look up another range? (y/n) ";
cin>>rangeAgain;

if(rangeAgain == 'y' || rangeAgain == 'Y')
{
}
else if(rangeAgain == 'n' || rangeAgain == 'N')
{
return;
}
}//end RangeData```

2. Code:
```for (studentNumber = 0; studentNumber < numStudents; ++studentNumber)
{
}```
That would get the number of grades between the low and high ranges (exclusive)... if you wanted to include the low and high points you would change the < to <=.

Code:
```void RangeData(int *grades, int numStudents)
{

...

cout<<"\nWould you like to look up another range? (y/n) ";
cin>>rangeAgain;

if(rangeAgain == 'y' || rangeAgain == 'Y')
{
}
else if(rangeAgain == 'n' || rangeAgain == 'N')
{
return;
}
}//end RangeData```
I would suggest using another loop within the function to take the place of the recursion. Something similar to:
Code:
```void RangeData(int *grades, int numStudents)
{
...

do
{
cout<<"\n\nEnter the low value for a grade range look up: ";
cin>>lowRange;
cout<<"Enter the high vaule for a grade range look up: ";
cin>>highRange;

...

cout<<"\nWould you like to look up another range? (y/n) ";
cin>>rangeAgain;
} while( rangeAgain == 'Y' || rangeAgain == 'y' );

}//end RangeData```

3. Edit: i'm slow

Hint:
for (studentNumber = 0; highRange == studentNumber; studentNumber++)

highRange will only == studentNumber once

4. Code:
```for (studentNumber = 0; studentNumber < numStudents; ++studentNumber)
{
}```
Using this still only gives me a result of 1 students in the range, even though there are more.

Edit: Nevermind, it does work, I just didn't implement it into my code properly. Thanks for all the help guys.

5. Originally Posted by Flakster
Using this still only gives me a result of 1 students in the range, even though there are more.
What are the values in the grades array and what are your choices for lowRange and highRange?

Guess you figured it out... nevermind.[/edit]

6. =D Too slow! Thanks anyways =)