如何通过键盘或乐谱文件输入乐谱

How to enter the scores via the keyboard or from a scores file

#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];
        }


}

当用户输入 B 打开乐谱文件时,我的程序没有继续。谁能帮我解决这个问题。

  1. 如果用户选择键盘,那么应该首先询问用户他们想要输入的分数总数,然后询问用户每个学生的姓名和考试分数。
  2. 如果用户选择文件,则询问用户文件名和位置。文件中的第一行应该是分数总数,后面是一个学生姓名和每行他们的测试分数。 例如:

    3
    F1 L1
    82
    F2 L2
    87
    F3 L3
    92
    

而且我想知道如何为

提供更多功能
  1. 确定最低分
  2. 确定最高分
  3. 计算mean/average分

我只是一个C++的初学者,请简单点,这样我才能更好地理解程序

这些分配的更好方法是使用一个记录容器,而不是多个容器,每个容器包含一个元素。

让我们定义一条记录:

struct Score_Record
{
  std::string first_name;
  std::string last_name;
  int score;
};

为简单起见,让我们选择一个向量作为我们的容器。为了节省击键次数,我们将使用 typedef:

typedef std::vector<Score_Record> Container_Type;

如果我们希望能够从文件或键盘加载记录,我们将使用通用流的概念:

void Input_Score(std::istream& input, Score_Record& sr)
{
  input >> sr.first_name;
  input >> sr.last_name;
  input >> sr.score;
  input.ignore(100000, '\n'); // Synchronize to next input line.
}

所以,要从键盘输入:

Score_Record sr;
Input_Record(cin, sr);

从文件输入:

ifstream data_file("my_data.txt");
Input_Record(data_file);

下面是一些查找最高分的代码:

Container_Type  database;
// read into database
const size_t quantity = database.size();
int maximum_score = -1;
for (unsigned int i = 0; i < quantity; ++i)
{
  if (database[i].score > maximum_score)
  {
    maximum_score = database[i].score;
  }
}

最低点、平均值和均值可以类似于上面的循环计算。

编辑 1:更简单的解决方案
如果我们保留与 class 相关的所有事物分数的责任,我们可以让 class 从输入流中读取它的值:

class Score_Record
{
  public:
    Score_Record()
    { ; }
    virtual ~Score_Record();
    friend std::istream& operator>>(std::istream& input, Score_Record& sr);
  private:
    std::string first_name;
    std::string last_name;
    int         score;
};

std::istream& operator>>(std::istream& input, Score_Record& sr)
{
  input >> sr.first_name;
  input >> sr.last_name;
  input >> sr.score;
  input.ignore(100000, '\n'); // synchronize to next text line.
  return input;
}

要从文件中读取单个 Score_Record

Score_Record sr;
data_file >> sr;

将整个文件读入数据库:

Container_Type database;
while (database >> sr)
{
  database.push_back(sr);
}
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

void getInformationKeyBoard(int size, string Names[], int socres[]);
void getName(string & name);
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
    {
        string NameA;
        getName(NameA);
        GetInformationFile(nameA, NumberOfStudent, Name, score); // This is where my program get an error





    }
    else // If you choice is not A,a or B,b
        cout << "Your did not follow the right instruction.";


    cout << endl << endl;
    system("pause");
    return 0;

}

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 GetInformationFile(fstream name, int size, string Names[], int scores[])
{
    for (int i = 0; i < size; i++)
    {
        string line;
        getline(name, line);
            Names[i];
            scores[i];
    }

}




void getName(string & name)
{
    char again = 'Y';
    bool close = false;

    while (close == false)
    {
    cout << "Enter name of file: ";
    cin >> name;
    ifstream inFile;
    inFile.open(name.c_str());
    close = true;
    if (!inFile)
    {
        cout << "File did not open correctly" << endl;
        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;
    }
    }

}