Don't know how to set up range

Thanking anyone in advance who might be able to help. This code is supposed to count matching records, a given filename, a low score, and a high score and return the number of matching records from a text file we are given. Whenever I set up the range, it doesn't give me the number of matching records. I have attached my code. It works when I build and run for the low score and the high score, but then it does not run the third function, nor does it exit the function.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//this is a part of part A of the same problem, has already been solved, just included it to show that i have declared the function howManyLines for part B.
 #include <iostream>
#include <fstream>
using namespace std;

int howManyLines()
{
   int count_lines = 0;

   std::string line;
   std::ifstream grades_file("StudentScores1.txt");

       while (std::getline(grades_file,line))
       ++count_lines;

   std::cout << " Number of lines: " << count_lines <<endl;

   void close();
}
//PART B
//This function will determine the amount of lines in a given text file

//Using the user inputted scores to determine the count of students with a grade that falls between that range

int countMatchingRecords()
{
   std::string line;
   std::ifstream grades_file("StudentScores1.txt");
   int low_score,high_score;
   int matches;
   int exam_grade;

   cout << " Enter a low score: " << endl;
   cin >> low_score;

   cout << " Enter a high score: " << endl;
   cin>> high_score;

   while(low_score <= exam_grade <= high_score)
       matches++;

   cout << " Number of Records: " << matches <<endl;






}

// After taking the criteria for a certain grade, the grade each student has will be converted into a letter grade for display
int convertToLetterGrade()
{


}


int main()
{
   howManyLines();
   countMatchingRecords();
   convertToLetterGrade();
}
Last edited on
closed account (iN6fizwU)
exam_grade hasn't been set, so you can't compare it with anything.

Your line 39 (with the two <= comparisons) might look OK on paper, but it isn't valid C++. Use an 'and' (&&) to test for two simultaneous conditions.

How do you know the third function doesn't run - your code snippet hasn't anything in this function (convertToLetterGrade). Are you sure this is your current code (or some previous work version)?
Note: Your three functions should return an int value (lines 6, 25, 52), but none does. You don't use the value at the caller either (lines 61-63).

Lets take your countMatchingRecords() and make it take the limits and return the matches:
1
2
3
4
5
6
7
8
9
10
11
12
int countMatchingRecords( int low_score, int high_score )
{
   std::string line;
   std::ifstream grades_file("StudentScores1.txt");
   int matches;
   int exam_grade;

   while( low_score <= exam_grade <= high_score )
       matches++;

   return matches;
}

The real action is clearly in the loop (lines 8-9). However, there is unused stuff. I'll remove that:
1
2
3
4
5
6
7
int countMatchingRecords( int low_score, int high_score )
{
   int matches;
   int exam_grade;
   while( low_score <= exam_grade <= high_score )  matches++;
   return matches;
}

Now we have three questions remaining:
1. What is the value of the 'matches' before the loop starts?
2. What is the value of the 'exam_grade'?
3. How is the condition evaluated?

For the third:
https://en.wikipedia.org/wiki/Operator_associativity
https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence

Says that
low_score <= exam_grade <= high_score
is same as
(low_score <= exam_grade) <= high_score
and thus like
1
2
3
bool state = (low_score <= exam_grade);

state <= high_score

You do need a logical AND:
https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#C.2B.2B_operator_synonyms
low_score <= exam_grade and exam_grade <= high_score
Topic archived. No new replies allowed.