gpt4 book ai didi

c++ - 如何使输出函数将格式化输出写入屏幕和输出文件

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:43:18 25 4
gpt4 key购买 nike

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

void getInformationKeyBoard(int, string[], int[]);
bool openFile(ifstream &infile, string fileName);
void display(int size, string array[]);
void read2Array(ifstream &infile, int size, string array[]);
void printReport(string name[], int score[], int NumberOfStudent);
int main()
{
const int size = 1024;
string Name[size], scoreFile[size];
int score[size];
int NumberOfStudent;
const int SIZE = 7;

ifstream inFile;

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);
printReport(Name, score, NumberOfStudent);
}

else if (choice == 'b' || choice == 'B') // It will take information from file
{

string name;
char again = 'Y';
bool close = false;

cout << "Enter name of file: ";
cin >> name;
openFile(inFile, name);
read2Array(inFile, SIZE, scoreFile);
display(SIZE, scoreFile);




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

}

// Open file
bool openFile(ifstream &infile, string fileName){
infile.open(fileName);
if (infile)
return true;
return false;
}

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[i]);
do
{
cout << i + 1 << ". Enter the score between 1 and 100: ";
cin >> scores[i];
} while (scores[i] > 100 || scores[i] < 0);

}
}



void read2Array(ifstream &infile, int size, string array[]){
int index = 0;
string line;

while (getline(infile, line)){
array[index] = line;
++index;
}
}

// Display array
void display(int size, string array[]){
for (int index = 0; index < size; ++index){
cout << array[index] << endl;
}
}


void printReport(string name[], int score[], int NumberOfStudent)
{
int lowest, highest, mean;
cout << "Enter lowest score: ";
cin >> lowest;
cout << "Enter highest score: ";
cin >> highest;
cout << "Enter mean score: ";
cin >> mean;

cout << "================================================================================";
cout << setw(10) << "Number of scores = " << NumberOfStudent << endl;
cout << setw(10) << "Lowest Score = " << lowest << endl;
cout << setw(10) << "Highest Score = " << highest << endl;
cout << setw(10) << "Mean Score = " << mean << endl;
cout << "Name" << setw(15) << "Score" << setw(15) << "IsLowest" << setw(15) << "IsHighest" << setw(15) << "Mean" << endl;
cout << "-----------------------------------------------------------------" << endl;
cout << name[NumberOfStudent] << endl;
cout << endl;

}

标题统计部分的分数、最低分数、最高分数和平均分数,然后是每个学生的详细信息 - 每行一个。

我坚持将输出输出到屏幕。我希望我的输出看起来像这样

Number of scores = 3
Lowest Score = 82
Highest Score = 92
Mean Score = 87
Name Score IsLowest IsHighest >=Mean
F1 L1 82 Y N N
F2 L2 87 N N Y
F3 L3 92 N Y Y

最佳答案

也许您可以传入一个 ostream 对象作为参数,然后用该参数的名称替换函数中所有出现的 cout。然后你可以运行该函数两次

printReport(cout, other args);
printReport(outFile, other args);

(或者将函数设置为调用自身,这样您就不必每次都输入 cout)。这种方法的唯一问题是我看到你在函数内部获取输入。如果你想使用上面的方法,你必须移动一些东西。相反,我会设置一个不同的函数,它将一个字符串作为输入并将所述字符串插入到 cout 和您的文件中。所以不必写

cout<<"hi";
file<<"hi";

你可以做

...
functionName("hi", outfile);
...
void functionName(string str, ostream outfile)
{
cout<<str;
outfile<<str;
}

希望对你有帮助

关于c++ - 如何使输出函数将格式化输出写入屏幕和输出文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44792658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com