Question

This program must be written in C++ and have the following functions (names and purposes given...

This program must be written in C++ and have the following functions (names and purposes given below).
Complete this program by reading the invalid data from both files below. The files are txt file in notepad.

First input file (filename.txt)

Mickey Mouse S45 78Y W91
Minnie Mouse HH95DF E92DD P88D
Jack Robinson
Jimmy Johnson SSF54S W89W U66T DD44S DD21S
Donald Duck WQ72Q A81B EE89W

Second input file (filename2.txt)

Mickey Mouse S45 78Y W91
Minnie Mouse HH95DF E92DD P88D  
Donald Duck WQ72Q A81B EE89W

You will have to decideas to what arguments must be passed to the functions and whether such passing must be by vlue or by reference or a mixture of both.
(On a side note, it would be nice if there's no vector involved. We haven't used vector in the class yet,
and the professor emphasized to use string function, instead of vector.)


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 etc.)

3. void readScore()
The function reads and sums the student scores and keep 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 that student.

5. double calculate Average()
The function take the sum of all scores and as to how many scores were read and returns the average score.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________



_________________

dataInput.txt (Input file)

Mickey Mouse S45 78Y W91

Minnie Mouse HH95DF E92DD P88D

Jack Robinson

Jimmy Johnson SSF54S W89W U66T DD44S DD21S

Donald Duck WQ72Q A81B EE89W

___________________________

#include <fstream>

#include <iostream>

#include <iomanip>

#include <cstring>

#include <sstream>

#include <cstdlib>

using namespace std;

// Function Declarations

void greeting(ofstream& dataOut);

void getName(ifstream& dataIn,string &name);

void readScore(ifstream& dataIn,int &sum,int &cnt);

void reportNoScore(string name);

double calculateAverage(int sum,int cnt);

int main() {

            //Declaring variables

    ifstream dataIn;

    ofstream dataOut;

    int sum=0,cnt=0;

    string fname,lname,name;

   

    //Opening the input file

// dataIn.open("dataInput.txt");

    dataIn.open("invalidFile.txt");

      //checking whether the file name is valid or not

if(dataIn.fail())

{

          cout<<"** File Not Found **";

    return 1;

}

else

{

          //Opening the output file

          dataOut.open("dataOutFile3.txt");

         

                      //setting the precision to two decimal places

            dataOut << std::setprecision(2) << std::fixed;

    //Reading the data from the file

    greeting(dataOut);

   

    if (!dataIn.fail()) {

      //Reading the data from the file

    getName(dataIn,name);

    }

    while(!dataIn.fail())

    {

    sum=0;

            cnt=0;

   

    readScore(dataIn,sum,cnt);

   

    if(cnt==0)

    {

    reportNoScore(name);       

            }

            else

            {

            dataOut<<setw(15)<<left<<name<<setw(8)<<right<<calculateAverage(sum,cnt)<<endl;

            }         

            getName(dataIn,name);

            }

  

    dataIn.close();

   }

            return 0;

}

void greeting(ofstream& dataOut)

{

            dataOut<<"** Welcome to the Program which calculates students average **"<<endl;

}

void getName(ifstream& dataIn,string &name)

{

            string fname,lname;

            dataIn>>fname>>lname;

            name=fname+" "+lname;

}

void readScore(ifstream& dataIn,int &sum,int &cnt)

{

            char ch;

            int score=0,k=0;

            string line;

            string token;

            getline(dataIn,line);

            stringstream ss(line);

            getline(ss, token, ' ');

        while(getline(ss, token, ' '))

        {

       

            k=0;

            for(int i=0;i<token.length();i++)

            {

                        ch=token.at(i);

                        if(isdigit(ch))

                        {

                                    score=(10*k)*score+ch-'0';

                                    k++;

                                                }

                                    }

                       

            sum+=score;

            cnt++;

        }

}

void reportNoScore(string name)

{

            cout<<"No score for "<<name<<endl;

}

double calculateAverage(int sum,int cnt)

{

            return ((double)sum)/cnt;

}

Output (Console):

dataOutFile3.txt

______________________Thank You

Add a comment
Know the answer?
Add Answer to:
This program must be written in C++ and have the following functions (names and purposes given...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT