Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <iostream> #include <fstream> using namespace std ; int validgroup( ost

ID: 3549034 • Letter: #

Question

#include<iostream>

#include<fstream>

using namespace std;


int validgroup(ostream & os,int score1,int score2,int score3);

void onegamescore(int score,ostream& os);

int avg3scores(int score1,int score2,int score3);


/* validgroup()

* Input:;

*  ostream& os - a reference to the output file

*  score1

*  score2

*  score3

* Process:

* Validates the scores by checking if the scores are between 0 and 300

* and moving on to the next score

* Output:

* prints the valid scores

*/



int validgroup(ostream& os,int score1,int score2,int score3)

{

    int count=0;

    if(score1>=0 && score1<=300)

    {

        

        os << "score1 is valid and its score is " << score1 << endl;

        count++;

    }

    if(score1<0 || score1>300)

        os << "score1 is Invalid and its score is " << score1 << endl;

    

    if(score2>=0 && score2<=300)

    {

        os << "score2 is valid and its score is " << score2 << endl;

        count++;

    }

    if(score2<0 || score2>300)

        os << "score2 is In Invalid and its score is " << score2 << endl;

    

    if(score3>=0 && score3<=300)

    {

        os << "score3 is valid and its score is " << score3 << endl;

        count++;

    }

    

    if(score3<0 || score3>300)

        os << "score3 is Invalid and its score is " << score3 << endl;

    

    if(count==3)

        os << "Group is Valid" << endl;

    else

        os << "Group is InValid" << endl;

    return (count==3);

}




/* onegamescore()

* Input:

*  ostream& os - a reference to the output file

*  score

* Process:

*  prints a rating to the score entered by evaluating the score     with the given paramaters

* Output:

* prints a rating for the given score

*/


void onegamescore(int score,ostream& os)

{

    // 250 to 300 is a professional game;

    if(score>=250 && score<=300)

        os << "Score is " << score << " Bowler's Rating is professional game" << endl;

    // 200 to 249 is an excellent game;

    else if(score>=200 && score<=249)

        os << "Score is " << score << " Bowler's Rating is excellent game" << endl;

    // 140 to 199 is a very good game;

    else if(score>=140 && score<=199)

        os << "Score is " << score << " Bowler's Rating is very good game" << endl;

    // 100 to 139 is a good game;

    else if(score>=100 && score<=139)

        os << "Score is " << score << " Bowler's Rating is good game" << endl;

    // 50 to 99 is a poor game;

    else if(score>=50 && score<=99)

        os << "Score is " << score << " Bowler's Rating is poor game" << endl;

    // below 50 is a horrible game.

    else

        os << "Score is " << score << " Bowler's Rating is horrible game" << endl;

}


/* avg3escore()

* Input:

* score1

* score2

* score3

*Process:

* calculate the average score by adding them together then dividing by 3

* Output:

*Prints the average scores

*/



int avg3scores(int score1,int score2,int score3)

{

    return (score1+score2+score3)/3;

}


int main()

{

    int score1,score2,score3;

    

    ifstream  infile("/Users/keron1988/Documents/bowlingscores.txt");

    //ifstream infile("/dev/stdout");

    

    ofstream os ("/Users/keron1988/Downloads/bowlingscores.txt"); // Mac rules

    //ofstream os ("/dev/stdout");

    

    infile.close();

    os.close();

    

    

    while(true)

    {

        cout << "Enter score 1 : ";

        cin >> score1;

        cout << endl;

        cout <<"Enter score 2: ";

        cin >> score2;

        cout << endl;

        cout <<"Enter score 3 : ";

        cin >> score3;

        cout << endl;

        int group_validity = validgroup(cout,score1,score2,score3);

        if(group_validity)

        {

            onegamescore(score1,cout);

            onegamescore(score2,cout);

            onegamescore(score3,cout);

            int average = avg3scores(score1,score2,score3);

            cout << "Average Score given by " << average << endl;

            onegamescore(average, cout);

            

        }

        cout<<endl;

    }

    return 0;

}


The main program then prints three blank lines.

Then the main program goes on to the next group of three values.

When the main program runs out of groups (hint: use a sentinel), it

prints the final values of three counters it has been keeping track of:

the total number of groups processed, the number of valid groups, and the

number of invalid groups.


also trying to setup an of and ifstream.

Explanation / Answer

#include<iostream>

#include<fstream>

using namespace std;


/* validgroup()

* Input:;

* ostream& os - a reference to the output file

* score1

* score2

* score3

* Process:

* Validates the scores by checking if the scores are between 0 and 300

* and moving on to the next score

* Output:

* prints the valid scores

*/

int validgroup(ostream& os,int score1,int score2,int score3)

{

int count=0;

if(score1>=0 && score1<=300)

{

os << "score1 is valid and its score is " << score1 << endl;

count++;

}


if(score1<0 || score1>300)

os << "score1 is Invalid and its score is " << score1 << endl;


if(score2>=0 && score2<=300)

{

os << "score2 is valid and its score is " << score2 << endl;

count++;

}


if(score2<0 || score2>300)

os << "score2 is In Invalid and its score is " << score2 << endl;


if(score3>=0 && score3<=300)

{

os << "score3 is valid and its score is " << score3 << endl;

count++;

}


if(score3<0 || score3>300)

os << "score3 is Invalid and its score is " << score3 << endl;


if(count==3)

os << "Group is Valid" << endl;

else

os << "Group is InValid" << endl;


return (count==3);

}


/* onegamescore()

* Input:

* ostream& os - a reference to the output file

* score

* Process:

* prints a rating to the score entered by evaluating the score with the given paramaters

* Output:

* prints a rating for the given score

*/

void onegamescore(int score, ostream& os)

{

// 250 to 300 is a professional game;

if(score>=250 && score<=300)

os << "Score is " << score << " Bowler's Rating is professional game" << endl;

// 200 to 249 is an excellent game;

else if(score>=200 && score<=249)

os << "Score is " << score << " Bowler's Rating is excellent game" << endl;

// 140 to 199 is a very good game;

else if(score>=140 && score<=199)

os << "Score is " << score << " Bowler's Rating is very good game" << endl;

// 100 to 139 is a good game;

else if(score>=100 && score<=139)

os << "Score is " << score << " Bowler's Rating is good game" << endl;

// 50 to 99 is a poor game;

else if(score>=50 && score<=99)

os << "Score is " << score << " Bowler's Rating is poor game" << endl;

// below 50 is a horrible game.

else

os << "Score is " << score << " Bowler's Rating is horrible game" << endl;

}


/* avg3escore()

* Input:

* score1

* score2

* score3

*Process:

* calculate the average score by adding them together then dividing by 3

* Output:

*Prints the average scores

*/

int avg3scores(int score1,int score2,int score3)

{

return (score1+score2+score3)/3;

}


/*

The main program then prints three blank lines.

Then the main program goes on to the next group of three values.

When the main program runs out of groups (hint: use a sentinel), it

prints the final values of three counters it has been keeping track of:

the total number of groups processed, the number of valid groups, and the

number of invalid groups.


also trying to setup an of and ifstream.

*/

int main()

{


int score1, score2, score3;

//ofstream os ("/Users/keron1988/Downloads/bowlingscores.txt"); // Mac rules

ofstream os;

int average, group_validity;

char ans = 'Y';


os.open ("bowlingscores.txt");


do

{

cout << "Enter score 1 : ";

cin >> score1;

cout << endl;

cout <<"Enter score 2: ";

cin >> score2;

cout << endl;

cout <<"Enter score 3 : ";

cin >> score3;

cout << endl;


group_validity = validgroup(os, score1, score2, score3);


if(group_validity)

{

onegamescore(score1, os);

onegamescore(score2, os);

onegamescore(score3, os);

average = avg3scores(score1, score2, score3);

cout << "Average Score given by " << average << endl;

onegamescore(average, os);

}

cout << " ";

cout << "Do you like to continue:(Y/N) " << endl;

cin >> ans;

} while (toupper(ans) == 'Y');

os.close();




return 0;

}