33. Write a complete C++ program to do the following: The main program reads in
ID: 3548995 • Letter: 3
Question
33. Write a complete C++ program to do the following: The main program
reads in and prints three bowling scores, score1, score2, and score3. It
then calls a series of functions to process these scores.
The main program calls a function validgroup() to determine if this
set of three values forms a valid group. The function validgroup()
receives four parameters (the 3 scores and a reference to an ofstream).
For the group to be valid, each number must be in the range from 0 to 300
(the possible scores in a bowling game). If the group is valid, the
function prints a message saying so. If one or more of the numbers is
negative or greater than 300, the function prints an overall message that
the group is invalid. In addition, for each invalid value, the function
prints the score and a message. The function returns a signal (say 1 or
0) indicating the validity of the group. (Hint: use six
Explanation / Answer
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int validgroup (int n1, int n2, int n3, ofstream& f)
{
int flag = 0;
if ((n1 > 301) || (n1 < 0))
{
cout << n1 << " is invalid. " << endl;
f << n1 << " is invalid. ";
flag ++;
}
if ((n2 > 301) || (n2 < 0))
{
cout << n2 << " is invalid" << endl;
f << n2 << " is invalid. ";
flag ++;
}
if ((n3 > 301) || (n3 < 0))
{
cout << n3 << " is invalid" << endl;
f << n3 << " is invalid. ";
flag ++;
}
if (flag > 0)
{
cout << "Group is invalid because one or more number(s) is invalid)." << endl;
f << "Group is invalid because one or more number(s) is invalid).";
return 1;
}
else
{
cout << "Group is valid. Three scores are " << n1 << " " << n2 << " " << n3 << endl;
f << "Group is valid. Three scores are " << n1 << " " << n2 << " " << n3;
return 0;
}
}
int main()
{
int num1, num2, num3, res;
ofstream file;
cout << "Please enter 3 numbers" << endl;
cin >> num1 >> num2 >> num3;
file.open("scores.txt");
res = validgroup (num1, num2, num3, file);
file.close();
cout << "Return value is " << res << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.