如何通过键盘或分数文件输入分数

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

本文关键字:输入 文件 何通过 键盘      更新时间:2023-10-16
#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. 计算平均/平均得分

我只是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.
}

so,要从键盘输入:

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