#include <iostream>
#include <iomanip>
using namespace std;
/** \brief Input integers.
* \param s invitation string
* \return int entered number
* \details The method displays invitations to enter an integer number from the console.
* It checks that the input is correct. If illegal characters appear, the prompt is duplicated and the input is repeated.
*/
int inputInt(string s)
{
int d;
bool b;
do {
b = true;
cout << s;
cin >> d;
if (!cin)
{
cout << "The input is not correct. Try again.\n";
cin.clear();
while (cin.get() != '\n');
}
else
{
b = false;
}
} while (b);
while (cin.get() != '\n');
return d;
}
int main()
{
int responses[30];
bool finish = false;
int i, k;
do
{
int frequency[5] = { 0, 0, 0, 0, 0 };
for (i = 0; i < 30; i++)
{
do
{
cout << "Student number " << i + 1 << ", please enter the library grade (1-5) : ";
k = inputInt("> ");
if ((k < 1 || k > 5))
{
cout << "The evaluation should be in the range [1-5]. Try again." << endl;
}
} while (k < 1 || k > 5);
responses[i] = k;
}
for (i = 0; i < 30; i++)
{
frequency[responses[i] - 1]++;
}
cout << endl;
for (i = 0; i < 5; i++)
{
cout << "Score " << i + 1 << " met " << frequency[i] << " times. ";
cout << "This represented " << fixed << setw(3) << setprecision(2) << right << setfill(' ') << ((double)frequency[i]) / 30 * 100 << "% of the total number of evaluations." << endl;
}
if (inputInt("Press \'0\' to exit or \'1\' to repeat. > ") == 0)
{
finish = true;
}
} while (!finish);
}
Comments
Leave a comment