#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void getInformationKeyBoard(int size, string Names[], int socres[]);
void getInformationFile(fstream& FileName, int size, string Names[], int scores[]);
void OpenTheFile(ifstream& FileName);
int main()
{
const int size = 1024;
string Name[size];
int score[size];
int NumberOfStudent;
char choice;
cout << "You want to enter your scores by your keyboard (A) or from your input (B): ";
cin >> choice;
if (choice == 'a' || choice == 'A') // It will take information from keyboard
{
cout << "How many students do you want to enter: ";
cin >> NumberOfStudent;
getInformationKeyBoard(NumberOfStudent, Name, score);
}
else if (choice == 'b' || choice == 'B') // It will take information from file
{
ifstream FileName;
OpenTheFile(FileName);
FileName >> NumberOfStudent;
getInformationFile(FileName, NumberOfStudent, Name, score);
FileName.close;
}
else // If you choice is not A,a or B,b
cout << "Your did not follow the right instruction.";
}
void getInformationKeyBoard(int size, string Names[], int scores[]) // Information from keyboard
{
for (int i = 0; i < size; i++)
{
cout << i + 1 << ". Student First Name and Last Name: ";
cin.ignore();
getline(cin, Names[1]);
do
{
cout << i + 1 << ". Enter the score between 1 and 100: ";
cin >> scores[i];
} while (scores[i] > 100 || scores[i] < 0 );
}
}
void OpenTheFile(ifstream& FileName) // Open the File
{
char again;
string InputFile;
bool close = false;
while (close == false)
{
cout << "Open the file: ";
cin >> InputFile;
ifstream ReadFromFile(InputFile);
if (ReadFromFile.is_open())
{
cout << "Succeed to open the file!\n";
close = true;
}
else
{
cout << "Failed to open the file!\n";
do {
cout << "Do you want to do it again(Y) or Close (N)? ";
cin >> again;
} while (again != 'y' && again != 'Y' && again != 'n' && again != 'N');
if (again == 'y' || again == 'Y')
close = false;
else
close = true;
}
}
}
void getInformationFile(ifstream& FileName, int size, string Names[], int scores[]) // Get information from the File
{
string FName, LName;
for (int i = 0; i < size; i++)
{
FileName >> FName >> LName;
Names[i] = FName + " " + LName;
FileName >> scores[i];
}
}
My program didn't continue when the user enter B to open the scores file. Can someone help me with this problem.
- If the user chooses keyboard, then the user should be first asked the total number of scores they want to enter and then ask the user for each student name and test score.
-
If the user chooses file, then ask the user for the file name and location. The first line in the file should be the total number of scores followed by one student name and their test score per line. for example:
3 F1 L1 82 F2 L2 87 F3 L3 92
And I wonder how to make more functions to
- Determine lowest score
- Determine highest score
- Calculate mean/average score
I just a beginner of C++, please make it easy so I can understand the program well
Aucun commentaire:
Enregistrer un commentaire