#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 ++的初学者,请轻松一点,这样我就能很好地理解程序
答案 0 :(得分:0)
这些分配的更好方法是使用一个记录容器,而不是每个容器一个元素的几个容器。
让我们将记录定义为:
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 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);
}
答案 1 :(得分:0)
#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;
}
}
}