Unten habe ich meinen aktuellen Code für ein Projekt, an dem ich gerade arbeite. Es ist vorgesehen, eine Datei mit Schülern und ihren Noten aufzunehmen, die Noten zu sortieren und sie dann alle wieder in eine Ausgabedatei zu legen. Die Anzahl der Schüler sollte 10 nicht überschreiten. Wenn es also mehr als 10 Schüler gibt, sollte sie nur die ersten 10 lesen und dann die Reihenfolge umkehren. Mein Code funktioniert perfekt, wenn Sie eine Datei mit mehr als 10 Studenten erhalten. Es scheint, dass es versucht, auf einen Teil im Speicher zuzugreifen, der nicht existiert. Ich habe versucht, den Code zu leeren Zeilen zu ignorieren, was es sollte, aber das scheint es nicht zu beheben. Meine "Lese" -Funktion ist, wo ich glaube, das Problem ist. StattCode kann keine Datei akzeptieren, die die Array-Größe überschreitet?
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
using namespace std;
//My student structure that holds the variable for each student object.
struct Student
{
string fname;
string lname;
double average;
};
//Prototyping the functions to read the input file into an array and then reverse it.
int read(ifstream &fin, Student s[]);
void print(ofstream &fout, Student s[], int amount);
void reverse(Student s[], int amount);
int main()
{
//Creating the file streams and global constant for the array and the filling it with I/O.
const int size = 10;
ifstream fin;
ofstream fout;
string inputFile;
string outputFile;
Student s[size];
cout << "Enter input filename: ";
cin >> inputFile;
cout << "Enter output filename: ";
cin >> outputFile;
//opeining the files given by the user and then testing if the opened.
fin.open(inputFile.c_str());
fout.open(outputFile.c_str());
if(fin.fail()) {
cout << "Unable to open input file.\n";
return 1;
}
//calling my 3 functions and then returning to main(). Closing files as well.
int count = read(fin , s);
reverse(s, count);
print(fout, s, count);
count = 0;
fin.close();
fout.close();
}
//This function reads the file given and breaks it up using string stream. It then calculates the averages for each stuent and assigns it to the array.
int read(ifstream &fin, Student s[])
{
istringstream sin;
string line;
string firstName;
string lastName;
double score;
double total;
double i=0;
int totalStudents=0;
Student stu;
for(int j = 0; j < 10; j++){
while(getline(fin, line)){
sin.clear();
if(line.empty())
{
j--;
}else{
sin.str(line);
while(sin >> firstName >> lastName){
stu.fname = firstName;
stu.lname = lastName;
while(sin >> score){
total += score;
i++;
stu.average = (total/i);
}
}
s[totalStudents]=stu;
totalStudents++;
stu.average = 0;
total = 0;
i = 0;
}
}
}
//returning the number of students in the file so it can later be used for the variable of total students.
return totalStudents;
}
//My print function that puts the array into a given output file.
void print(ofstream &fout, Student s[], int amount)
{
for(int i = 0; i<amount; i++)
{
if(s[i].lname.empty())
{
fout<<"No students to report.";
}else{
ostringstream sout;
sout << s[i].lname.c_str() << ", " << s[i].fname.c_str();
fout <<setw(21)<< left << sout.str() << setprecision(2) << fixed << "= " << s[i].average << '\n';
}
}
}
//the function that reverses the order of the students by copying the last student into a temporary variable and casting it to the beggining.
void reverse(Student s[], int amount)
{
Student temp;
for(int i=0; i< amount/2; i++)
{
temp=s[i];
s[i]=s[amount-i-1];
s[amount - i - 1] = temp;
}
}
'for (int j = 0; j <10; j ++) { while (getline (fin, Linie)) {' Das sieht fischig – AndyG
Das richtige Werkzeug, um solche Probleme zu lösen, ist Ihr Debugger. Sie sollten Schritt für Schritt durch Ihren Code * gehen, bevor Sie auf Stack Overflow nachfragen. Für weitere Hilfe lesen Sie bitte [Wie kleine Programme zu debuggen (von Eric Lippert)] (https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). Zumindest sollten Sie Ihre Frage bearbeiten, um ein [minimales, vollständiges und verifizierbares] (http://stackoverflow.com/help/mcve) Beispiel einzufügen, das Ihr Problem zusammen mit den Beobachtungen, die Sie in der Debugger. –