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.
//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>
usingnamespace 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();
}
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?