0

C++ Code

Basically i am trying to take input from a file and store the data in variables, But i am unable to do that because each time i compile this code it compiles successfully but gives a runtime error.(Right Now i have just Data of 3 Students, so i set the condition to run the loop 3 times).

The variable "Name" will get the name from the file, "sap" will get the roll number, "number" will get the number of subjects the user wants to enter marks of (Max 5), and "marks" will enter marks of those subjects.

Variables "SapFile", "NumberFile", and "MarksFile" will get the string data from the file and store them in their respective format variables as mentioned above.

What it does Exactly is read Data from the file to the variables and also prints them, But when it reaches at "i=3" Then instead of this condition becoming false and terminating the Loop and exiting the Function, it gives a runtime error. [Note: It prints the data of 3 students in the file correctly and then gives Runtime error].

The Error Snap is Attaches as follows,

Runtime Error Image

'''

This is My code

void read_in_variables(void)
{

string SapFile = "";

string NumberFile = "";
string MarksFile = "";
int studentCount = 0;

struct Student
{
    string Name = "";
    int sap = 0;
    int number = 0;
    int marks[5] = { 0 };
};
Student S[50];

ifstream outFile;
outFile.open("Students.csv", ios::in);
if (outFile.is_open())
{
    for (int i = 0; i<3; i++)
    {
        if (i == 0)
            cin.ignore();
        getline(outFile, S[i].Name, ',');
        studentCount++;
        getline(outFile, SapFile, ',');
        S[i].sap = stoi(SapFile);

        getline(outFile, NumberFile, ',');
        S[i].number = stoi(NumberFile);

        cout << i + 1 << ".  " << S[i].Name << ", " << S[i].sap << ", " << S[i].number;
        for (int j = 0; j < (S[i].number - 1); j++)
        {
            getline(outFile, MarksFile, ',');
            S[i].marks[j] = stoi(MarksFile);
            cout << ", " << S[i].marks[j];
        }
        getline(outFile, MarksFile, '\n');
        S[i].marks[S[i].number] = stoi(MarksFile);
        cout << ", " << S[i].marks[S[i].number];
        cout << endl;
    }
        outFile.close();
    if (studentCount == 0)
        cout << "\n\nNo Student Records Present.\n\a\n";
}
else
{
    cout << "File not Opened !!! ";
}
}

Moreover, when at first i tried to put "outFile.eof()" in the condition statement then it returns false in the beginning and does not even enters in the loop, and Prints "No Student Records Present." as "StudentCount" has Value 0.

[I have included All the Header Files necessary for this function].

Kindly Help me Solve this issue and make this function work perfectly.

Usman
  • 14
  • 3
  • 1
    did you try running it with a debugger? – yaodav Jun 09 '20 at 15:16
  • Have you reviewed existing examples on the internet and StackOverflow? – Thomas Matthews Jun 09 '20 at 15:52
  • BTW, you should not use a `for` loop when reading from files. For the same reason, you should use `std::vector` not an array. The reason is that you don't know the size of the file or the contents at compile time. If there are 51 items in the file, your array will overflow. If there are 2 items in the file, you have wasted 48 slots in the array. – Thomas Matthews Jun 09 '20 at 15:54
  • The problem Was Solved. Thank You All. – Usman Jun 10 '20 at 09:39

0 Answers0