如何通过键盘或分数文件输入分数
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打开分数文件时,我的程序没有继续。有人可以帮助我解决这个问题。
- 如果用户选择键盘,则应首先向用户询问他们要输入的分数总数,然后向用户询问每个学生名称和测试分数。
如果用户选择文件,请向用户询问文件名和位置。文件中的第一行应该是分数的总数,然后是一个学生名称和每行测试分数。例如:
3 F1 L1 82 F2 L2 87 F3 L3 92
我想知道如何使更多功能
- 确定最低分数
- 确定最高分数
- 计算平均/平均得分
我只是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;
}
}
}
相关文章:
- 用c++从输入文件中读取另一行
- 更改.cpp程序的输入文件中数据的位置会意外更改输出
- 获取通过重定向传入的输入文件的名称?
- C++ 如何打开带有另一个目录的输入文件?
- C++模板编程设计问题 - 根据输入文件返回不同的类型
- 如何从输入文件 c++ 创建随机名称生成器
- 从输入文件修剪空格
- 链接器输入文件未使用,因为链接未完成,我无法获得.o
- 我正在尝试从输入文件中读取,然后使用它们的子字符串比较字符串的特定部分
- 我遇到使用 c++ 逐行读取输入文件的问题
- 使用重定向命令从 stdin 读入的字符串"<"输入文件未正确附加
- 使用 getline 从输入文件打印时如何忽略空白行
- 如何按升序对输入文件中的数字进行排序,并找到它们的范围和中值
- 数组大小由输入文件中的行数定义
- 从C++输入文件读取
- 在课堂上创建了一个朋友,给出"无输入文件"错误
- 通过显示一行星号来创建条形图,该条形图将读取数据的输入文件
- 利用输入文件中的数据(字符串和int)并在循环中利用
- C++ 仅读取输入文件中的部分(不是全部)数据以添加到记录中
- 如何从输入文件中读取字符并将其存储到向量中?