So below is a code I have written; however, it has some specifications that are not being met and I am unsure what I am supposed to change.
1. Both the size of the array (a constant) and the array itself must be declared locally inside of int main().
2. Each option in the menu must simply call a function. Each function must at minimum pass in the array and the size of the array. You must list all function prototypes above int main () and all function definitions must appear after int main().
#include<iostream>
#include<fstream>
#include<string>
usingnamespace std;
longint scores [10];//defining the score array
void readingscores();
void printscores(longint * scores);
void greatest(longint * scores);
void smallest( longint * scores);
void average (longint * scores);
void entry (longint * scores);
void readFile();
void menu () {
int choice;
cout <<" ---------------------------------------------------\n";
cout <<"1-D ARRAY PROCESSING MENU OPTIONS\n";
cout<<"---------------------------------------------------\n";
cout<< "1. Read in 10 scores from user.\n";
cout<<"2. Read in 10 scores from the file, scores.txt.\n";
cout<<"3. Print all scores.\n";
cout<<"4. Print the highest score.\n";
cout<<"5. Print the lowest score.\n";
cout<<"6. Print the average score.\n";
cout<<"7. Print one score (give its entry number)\n";
cout<<"8. Quit program\n";
cout<<" Enter your choice:\n";
cin >> choice;
switch (choice) {
case 1: readingscores(); break;
case 2:readFile(); break;
case 3: printscores(scores); break;
case 4: greatest(scores); break;
case 5: smallest(scores); break;
case 6: average(scores); break;
case 7: entry(scores); break;
case 8:; break;
}
}
int main ()
{
menu();
return 0;
}
void readingscores() {
for (int i =0; i<= 9; i++) //filling up the array
{
std::cin >> scores [i];
}
menu();
}
void printscores( longint * scores) {
for(int i=0; i<= 9; i++)
{
cout << scores [i];
}menu();
}
void greatest(longint * scores){
longint great = scores[0];
for (int i=0; i<= 9; i++)
{
if (scores[i+1]>= scores[i]) {
great = scores[i+1];
}
}
cout <<"The highest scores is:"<< great;
menu();
}
void smallest(longint * scores){
longint small = scores[0];
for (int i=0; i<=9; i++)
{
if (scores[i]< small) {
small = scores[i];
}
}
cout <<"The lowest scores is:"<< small;
menu();
}
void average(longint * scores){
longint sum = 0;
for (int i=0; i<=9; i++) {
sum += scores [i];
}
cout << "The average score is:"<< sum/10;
menu();
}
void entry(longint * scores){
longint number;
cout <<" Enter your number (0-9)";
cin >> number;
cout << scores [number];
menu();
}
void readFile() {
ifstream myfile ("scores.txt");
string line;
if(myfile.is_open() )
{ int i=0;
while(getline(myfile,line) && i<=9)
{
cout<<line<<"\n";
scores [i]= stol(line);
i++;
}
myfile.close();
}
menu();
}
/*
---------------------------------------------------
1-D ARRAY PROCESSING MENU OPTIONS
---------------------------------------------------
1. Read in 10 scores from user.
2. Read in 10 scores from the file, scores.txt.
3. Print all scores.
4. Print the highest score.
5. Print the lowest score.
6. Print the average score.
7. Print one score (give its entry number)
8. Quit program
Enter your choice:
1
2
4
5
6
7
8
9
43
34
23
---------------------------------------------------
1-D ARRAY PROCESSING MENU OPTIONS
---------------------------------------------------
1. Read in 10 scores from user.
2. Read in 10 scores from the file, scores.txt.
3. Print all scores.
4. Print the highest score.
5. Print the lowest score.
6. Print the average score.
7. Print one score (give its entry number)
8. Quit program
Enter your choice:
4
The highest scores is:43 ---------------------------------------------------
1-D ARRAY PROCESSING MENU OPTIONS
---------------------------------------------------
1. Read in 10 scores from user.
2. Read in 10 scores from the file, scores.txt.
3. Print all scores.
4. Print the highest score.
5. Print the lowest score.
6. Print the average score.
7. Print one score (give its entry number)
8. Quit program
Enter your choice:
The average score is:14 ---------------------------------------------------
1-D ARRAY PROCESSING MENU OPTIONS
---------------------------------------------------
1. Read in 10 scores from user.
2. Read in 10 scores from the file, scores.txt.
3. Print all scores.
4. Print the highest score.
5. Print the lowest score.
6. Print the average score.
7. Print one score (give its entry number)
8. Quit program
Enter your choice:
*/
void readingscores(int scores[], int SIZE);
void printscores(int scores[], int SIZE);
void greatest(int scores[], int SIZE);
void smallest(int scores[], int SIZE);
void average (int scores[], int SIZE);
void entry (int scores[], int SIZE);
void readFile(int scores[], int SIZE);
void menu(int scores[], int SIZE);
int main ()
{
constint SIZE = 10;
int scores[SIZE] = {0};
menu(scores, SIZE);
return 0;
}
void menu (int scores[], int SIZE) {
int choice;
cout <<" ---------------------------------------------------\n";
cout <<"1-D ARRAY PROCESSING MENU OPTIONS\n";
cout<<"---------------------------------------------------\n";
cout<< "1. Read in 10 scores from user.\n";
cout<<"2. Read in 10 scores from the file, scores.txt.\n";
cout<<"3. Print all scores.\n";
cout<<"4. Print the highest score.\n";
cout<<"5. Print the lowest score.\n";
cout<<"6. Print the average score.\n";
cout<<"7. Print one score (give its entry number)\n";
cout<<"8. Quit program\n";
cout<<" Enter your choice:\n";
cin >> choice;
switch (choice) {
case 1: readingscores(scores, SIZE);
break;
// and so on
}
}