Each time you call the function the "ofstream" is created and destroyed when the function ends. So each time you create the "ofstream" the file is wiped clear and you have a new empty file.
Choices are to open the file for append or consider this code:
#include <iostream>
#include <fstream>
usingnamespace std;
void input(std::ofstream& outFile)
{
//std::string name; // <--- Should be using a "std::string".
char name[30];
double markEng, markMath, markSci;
cout << "Enter student name : ";
cin.getline(name, 30);
//std::getline(std::cin, name);
cout << "Enter obtained of English exam : ";
cin >> markEng;
cout << "Enter obtained of Math exam : ";
cin >> markMath;
cout << "Enter obtained of Science exam : ";
cin >> markSci;
cin.ignore();
outFile << name << " " << markEng << " " << markMath << " " << markSci << endl;
//projectFile.close(); // <--- Not required as the dtor will close the file when the function looses scope.
};
int main()
{
const std::string outFileName{ "projectfile.txt" }; // <--- Put File name here.
std::ofstream outFile(outFileName);
if (!outFile)
{
//std::cout << "\n File " << std::quoted(outFileName) << " did not open" << std::endl;
std::cout << "\n File \"" << inFileName << "\" did not open." << std::endl;
return 1;
}
input(outFile);
input(outFile);
return 0;
}
This will keep the file open until "main" ends the program.
Untested as I did not have your input file to use.
Also when you open a file for input or output you should check that is is open. Otherwise you program would continue trying to use a file that is not open.
If you are going to use a "std::string" in the program you need the header file "<string>".
Andy