Help me i Try to do but i cannot.

This program is to be written to accomplish same objectives, as did the program.
Except this time we modularize our program by writing functions, instead of writing the entire code in main. The program must have the following functions(names and purposes given below). You will have to decide as to what arguments must be passed to the functions and whether such passing must be by value or by reference or a mixture of both.
1. Void greeting()
The function greeting () generates generalized greeting and outputs to the output file as the first message.
2. Void getName()
The function reads the name of the student from the data file and processes the name as needed.(for example for outputting etx.)
3. Void readScore()
The function reads and sums the student scores and keeps track of number of scores read. Both the sum of scores and number of scores read are returned by reference.
4. Void reportNoScore()
If the file has no scores for a student, the function generates a message printing student name and the fact that there are no scores found for student.
5. Double calculateAverage()
The function take the sum of all scores and as to how many scores were read and returns the average score.
6. Char assignGrade()
The function takes the average score calculated by function calculateAverage() and returns a letter grade, using the same criterion as used in Lab5.
7. Void printData()
The function printData() prints the student name, their overall average and their grade to the output file.
8. Void farewell()
The program prints the farewell message in the output file, signifying the end of program.

The approximate pseudo code for main function is given on next page.








The pseudo code for the main function is given below

Declare necessary variables
Prompt user to supply the input file name
Open the input file
If input file does not exist then inform user and exit the program
Else
Prompt user to supply the output file name
Open the output file
If the output file does not exist then inform user and exit the program
Else
Call function greeting()
Call function getName() to read the first name from the input
File
While not end of input file
Call function ReadScore()
If no valid scores then
Call function reportNoScore()
Else
Call function calculateAverage()
Call function assignGrade()
Call function printData()
Call function getName()
End of loop
Call function farewell () to print the farewell message in the data file.
Close the input file.
Close the output file.
End of main


This is question and i try to do below.
But it doesn't work... how i need to change it??

*This for data.txt for make program
Mickey Mouse 45 78 91
Minnie Mouse 95 92 88
Donald Duck 72 81 89






#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

void greeting(ofstream& outFile);//Finished
void getName(ifstream& inFile, string& firstN, string& lastN);//Finshed?
void readScore(ifstream& inFile, double& sum, int& counter);
void reportNoScore(string& firstN, string& lastN);
double calculateAverage(double sum, int counter);
char assignGrade(double avg);
void printData(string firstN, string lastN, double scores, char grade, ofstream& outFile, double avg);
void farewell(ofstream& outFile);//Finished

int main()
{
ifstream inFile;
inFile.open("data.txt");
ofstream outFile;
outFile.open("data.txt");

string firstN = string();
string lastN = string();

int counter = int();
double avg = double();
double sum = double();

char grade = char();

cout << fixed << showpoint;
cout << setprecision(2);

greeting(outFile);

while (!inFile.eof())
{
getName(inFile, firstN, lastN);

readScore(inFile, sum, counter);

if (sum == 0)
{
reportNoScore(inFile, firstN, lastN)
}

avg = calculateAverage(sum, counter);

grade = assignGrade(avg);

printData(firstN, lastN, sum, grade, outFile, avg);
}

farewell(outFile);

inFile.close();
outFile.close();

return 0;
}

void greeting(ofstream& outFile)
{
int i = int();
char titleLine = 196;

cout << "\t\Hello And Welcome To Our System! " << endl;
outFile << "\t\Hello and Welcome To Our System! " << endl;

for (i = 0; i < 50; i++)
{
cout << titleLine;
outFile << titleLine;
}

cout << endl;
outFile << endl;
}

void getName(ifstream& inFile, string& firstN, string& lastN)
{
inFile >> firstN >> lastN;
}

void readScore(ifstream& inFile, double& sum, int& counter)
{
double scores = double();

sum = 0.0;
counter = 0;

while ((inFile.peek() != '\n') && (!inFile.eof()))
{
inFile >> scores;
while (inFile.fail())
{
inFile.clear();
inFile.ignore(1, '\n');
inFile >> scores;
}
sum = sum + scores;
++counter;
}
}

void reportNoScore(ifstream& inFile, string& firstN, string& lastN)
{
cout << "No Scores Were Read For " << firstN << " " << lastN << " " << endl;
}

double calculateAverage(double sum, int counter)
{
return sum / counter;
}

char assignGrade(double avg)
{

if (avg >= 90)
{
char letterA = 'A';
return letterA;
}
else if (avg >= 80)
{
char letterB = 'B';
return letterB;
}
else if (avg >= 70)
{
char letterC = 'C';
return letterC;
}
else if (avg >= 60)
{
char letterD = 'D';
return letterD;
}
else if (avg <= 69)
{
char letterF = 'f';
return letterF;
}

}

void printData(string firstN, string lastN, double scores, char grade, ofstream& outFile, double avg)
{

cout << firstN << " " << lastN << " " << endl;
cout << "Total Score: " << scores << " " << endl;
cout << "Average Percentage Score: " << avg << "%" << endl;
cout << "Grade: " << grade << endl;

outFile << firstN << " " << lastN << " " << endl;
outFile << "Total Score: " << scores << " " << endl;
outFile << "Average Percentage Score: " << avg << "%" << endl;
outFile << "Grade: " << grade << endl;

cout << endl;
outFile << endl;
}

void farewell(ofstream& outFile)
{
int i = int();
char titleLine = 196;

for (i = 0; i < 50; i++)
{
cout << titleLine;
outFile << titleLine;
}
cout << endl;
outFile << endl;

cout << "\t\Thank You For Using Our System!" << endl;
outFile << "\t\Thank You For Using Our System!" << endl;

cout << endl;
outFile << endl;
}
















I believe the issue could be that you are opening the same file for both input and output simultaneously. When you open the file for output, the contents of the file are cleared unless you open it in a different mode like the 'append' mode which outputs onto the end of the file instead of overwriting.

My advice is to only open the file for outputting when you have finished with input, or open the file in 'append' mode using the ios::app flag.
Topic archived. No new replies allowed.