我的问题在于最终的最小功能,程序从大约25个名称中填充,每个名称有5个等级。平均值和最大值平稳运行,但我似乎无法敲定最小值。我设法得到它来拉几个名字,但然后程序崩溃。任何人都可以帮我理解我在这里俯瞰的东西吗?拜托,谢谢你。
如果你在编译器中插入它,min函数位于200-250行之间,一个简单的txt文件用于从中提取名称和等级。
// Headers
#include <iostream> // cout, cin
#include <cstdlib> // exit()
#include <string> // strings
#include <fstream> // file processing
#include <iomanip> // stream manipulation
using namespace std;
// Global variables
const int MAX_STUDENTS = 25; // We will not process more than 25 students even if the file contains more
const int MAX_GRADES = 5; // Each student has exactly 5 grades
const string FILENAME = "NamesGrades.txt"; // The name of the file that you will read
// Function declarations
int loadStudentNamesGrades(string students[], int grades[][MAX_GRADES], string fileName, int maxStudents);
void displayAverages(string students[], int grades[][MAX_GRADES], int studentCount);
void displayMax(string students[], int grades[][MAX_GRADES], int studentCount);
void displayMin(string students[], int grades[][MAX_GRADES], int studentCount);
string getLetterGrade(double grade);
int getLongestNameLength(string students[], int studentCount);
int main()
{
int studentCount = 0; // You need one to keep up with the actual number of students
int grades[MAX_STUDENTS][MAX_GRADES]; // You need a two dimensional arry for the grades of the students
string students[MAX_STUDENTS]; // You need and array of strings for the student names
char choice; // You need a variable to hold the choice of the user for the menu
// Get students and grades
studentCount = loadStudentNamesGrades(students, grades, FILENAME, MAX_STUDENTS);
// Loop until user says to quit (Do loop suggested)
do
{
// present menu and get user's choice
cout << "\nGrade Report Program\n\n";
cout << "\t1. Display Average Grade\n";
cout << "\t2. Display Maximum Grade\n";
cout << "\t3. Display Minimum Grade\n";
cout << "\t4. Quit Program\n";
cout << "\nEnter your choice (1-4): ";
cin >> choice;
// Process the choice
switch( choice )
{
case '1': // Average
displayAverages(students, grades, studentCount);
break;
case '2': // Maximum
displayMax(students, grades, studentCount);
break;
case '3': // Minimum
displayMin(students, grades, studentCount);
break;
case '4': // Quit
break;
default:
cout << "Invalid option.\n\n";
}
if(choice != '4' )
{
cout << endl;
system("PAUSE");
system("CLS");
}
} while( choice != '4' );
cout << endl;
return 0;
}
/***********************************************************
loadStudentNameGrades opens and read fileName. It will read in two strings, concatenate them, and then save
to the students array. It then reads five integers and save each to the grades array. The function will return
the actual number of student/grade combinations read
PARAM: students is an array of strings that can hold up ot maxStudents values
grades is a two dimensional array for holding the grades of each student
fileName is the name of the file that will be opened and read
maxStudents is the maximum number of students that we will read from the file
PRE: students[] is large enough to contain up to maxStudents elements
grades[] is large enough ot contain up to maxStudents elements
POST: students[] contains the names of up to maxStudents
grades[][] contains the grades for up to maxStudents
The number of student/grade combinations actually read from the file is returned. This value can range
between 0 <= numStudents <= maxStudents
NOTE: students[] and grades[] are meant to be parralel arrays. students[0] and grades[0] are the same student
************************************************************/
// Check 16:15
int loadStudentNamesGrades(string students[], int grades[][MAX_GRADES], string fileName, int maxStudents)
{
ifstream inFile; // input file stream
string studentsName, letterGrade; // Name of the student and grade character (char)
int numStudents = 0; // number of students actually read
// Open the file
inFile.open(fileName.c_str());
if( inFile.fail())
{
cout << "Could not open file" << endl;
system("PAUSE");
exit(1);
}
for( int i = 0; i < maxStudents && (inFile >> studentsName >> letterGrade); i++, numStudents++)
{
for( int j = 0; j < MAX_GRADES; j++ )
{
inFile >> grades[i][j];
}
students[i] = studentsName + " " + letterGrade;
}
inFile.close();
return numStudents;
}
/***********************************************************
displayAverages calculates the average of each student and displays the
students name, average, and letter grade of the average in a table
PARAM: students[] is an array of strings that contains the names of studentCount students
grades[] is an array of integers that contains the grades of studentCount students
studentCount contains the value of the number of elements in the students[] and grades[] arrays
PRE: students[] and grades[] contain values for studentCount elements
POST: table of student names, averages, and letter grades is displayed
************************************************************/
// Check 16:15
void displayAverages(string students[], int grades[][MAX_GRADES], int studentCount)
{
double average; // Average grades of students
int total; // total of all grades (accumulator)
int maxLength = getLongestNameLength(students,studentCount);
cout << setprecision(1) << fixed << showpoint;
// Setup table header
cout << "\n\nGrade Averages\n";
cout << setw(maxLength + 1) << left << "Name" << setw(4) << right << "Average" << setw(6) << "Grade" << endl;
for( int i = 0; i < studentCount; i++ )
{
cout << setw(maxLength + 1) << left << students[i];
total = 0;
for( int j = 0; j < MAX_GRADES; j++ )
{
total += grades [i][j];
}
average = (double)total / MAX_GRADES;
cout << setw(7) << right << average << setw(6) << getLetterGrade(average) << endl;
}
}
/***********************************************************
displayMax calculates the maximum grade of each student and displays the
students name, maximum grade, and letter grade of the maximum grade in a table
PARAM: students[] is an array of strings that contains the names of studentCount students
grades[] is an array of integers that contains the grades of studentCount students
studentCount contains the value of the number of elements in the students[] and grades[] arrays
PRE: students[] and grades[] contain values for studentCount elements
POST: table of student names, maximum grades, and letter grades is displayed
************************************************************/
// Check 16:15
void displayMax(string students[], int grades[][MAX_GRADES], int studentCount)
{
int maxGrade;
int maxLength = getLongestNameLength(students,studentCount);
cout << "\n\nGrade Maximums\n";
cout << setw(maxLength + 1) << left << "Student" << setw(4) << right << "Max" << setw(6) << "Grade" << endl;
for( int i = 0; i < studentCount; i++ )
{
cout << setw(maxLength + 1) << left << students[i];
maxGrade = 0;
for( int j = 0; j < MAX_GRADES; j++ )
{
if(maxGrade < grades [i][j]) maxGrade = grades [i][j];
}
cout << setw(4) << right << maxGrade << setw(6) << getLetterGrade(maxGrade) << endl;
}
}
/***********************************************************
displayMin calculates the minimum grade of each student and displays the
students name, minimum grade, and letter grade of the minimum grade in a table
PARAM: students[] is an array of strings that contains the names of studentCount students
grades[] is an array of integers that contains the grades of studentCount students
studentCount contains the value of the number of elements in the students[] and grades[] arrays
PRE: students[] and grades[] contain values for studentCount elements
POST: table of student names, minimum grades, and letter grades is displayed
************************************************************/
/* Notes:
Program seems to pull 2 names and freeze on the third. Unsure as to why?
Assigns Leslie Carter 36C and code seems to break down afterwords.
Issue is undetected any where else and seems to be isolated in this function.
*/
void displayMin(string students[], int grades[][MAX_GRADES], int studentCount)
{
int minGrade;
int maxLength = getLongestNameLength(students,studentCount);
cout << "\n\nGrade Minimums\n";
cout << setw(maxLength + 1) << left << "Student" << setw(4) << right << "Min" << setw(6) << "Grade" << endl;
for( int i = 0; i < studentCount; i++ ) // > is null result, < is result
{
cout << setw(maxLength + 1) << left << students[i];
minGrade = 101; // Produced a result in the correct direction
for( int j = 0; j < MAX_GRADES; j++ )
{
if(minGrade > grades [i][j]) minGrade = grades [i][j]; // Produced a result in the correct direction
}
cout << setw(4) << right << minGrade << setw(6) << getLetterGrade(minGrade) << endl;
}
}
/***********************************************************
getLetterGrade converts a numerical grade to a letter grade
PARAM: grade is the numerical grade to convert. Expected range is 0 <= grade <= 100
PRE: grade contains a value in the correct range
POST: The corresponding letter grade of the numerical grade is returned
************************************************************/
// Check 16:15
string getLetterGrade(double grade)
{
if(grade > 90 )
return "A";
else if( grade > 80)
return "B";
else if( grade > 70)
return "C";
else if( grade > 60)
return "D";
else if( grade > 50)
return "F";
}
/***********************************************************
getLongestNameLength returns the length of the longest string from a list of strings
PARAM: students[] is an array of strings that contains the name of students
studentCount is the size of the students[] array
PRE: students[] contains studentCount names
POST: The length of the longest string in students[] is returned
************************************************************/
// Check 16:15
int getLongestNameLength(string students[], int studentCount)
{
int maxLength = 0;
for( int i = 0; i < studentCount; i++)
{
if( students[i].length() > maxLength ) maxLength = students[i].length();
}
return maxLength;
}
答案 0 :(得分:2)
当成绩低于50时,getLetterGrade
函数无法返回任何内容。尝试使用尽可能多的警告进行构建。 -Wall
会警告你这个错误。