C++,在用户确认后继续向文件添加记录
C++, Keep Adding Records To a File besd on User confirmation
下面的代码能够一次将单个学生信息发送到一个文件中。如何在不退出并重新打开程序的情况下修改为一个接一个地发送更多记录。我是新手。请帮忙。
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <windows.h>
#include <sstream>
#include <algorithm>
#include <conio.h>
#include <stdio.h>
#include <cstdlib>
#include <iomanip>
#include <dos.h>
using namespace std;
string userName;
string passWord;
string selection;
int option;
struct patientinfo {
string PatientFname;
string PatientLname;
int Age;
int ContactNo;
string TreatmentType;
string AppDate;
string AppTime;
int eReciptId;
};
int num;
patientinfo emp[50];
void makeBooking()
{
ofstream outputFile;
outputFile.open("smt.bin", std::ofstream::in | std::ofstream::out | std::ofstream::app);
int i=num;
num+=1;
cout<< endl << endl << endl << endl << endl << endl
<< setw(30)<<"First Name : ";
cin>>emp[i].PatientFname;
outputFile <<emp[i].PatientFname <<",";
cout<< setw(30)<<"Last Name : ";
cin>>emp[i].PatientLname;
outputFile <<emp[i].PatientLname <<",";
cout<< setw(30)<<"Age : ";
cin>>emp[i].Age;
outputFile <<emp[i].Age <<",";
}
int main ()
{
makeBooking();
return 0;
}
考虑到您有50名学生想要向他们发送信息,您应该调用makeBooking函数50次。所以在中更改主菜单
int main ()
{
for (int i = 0; i < 50; i++) {
makeBooking();
}
return 0;
}
应该做到这一点。然而,一个更优雅的解决方案是将索引i作为函数中的参数发送。所以代码是:
patientinfo emp[50];
void makeBooking(int i)
{
ofstream outputFile;
outputFile.open("smt.bin", std::ofstream::in | std::ofstream::out | std::ofstream::app);
// int i=num; you don't need these anymore
// num+=1;
cout<< endl << endl << endl << endl << endl << endl
<< setw(30)<<"First Name : ";
cin>>emp[i].PatientFname;
outputFile <<emp[i].PatientFname <<",";
cout<< setw(30)<<"Last Name : ";
cin>>emp[i].PatientLname;
outputFile <<emp[i].PatientLname <<",";
cout<< setw(30)<<"Age : ";
cin>>emp[i].Age;
outputFile <<emp[i].Age <<",";
}
int main ()
{
char response;
for (int i = 0; i < 50;) {
cout << "Do you want to add another person?";
cin >> response;
if (response == 'y')
makeBooking(i++);
else if (respinse == 'n')
break;
else
cout << "Undefined response";
}
return 0;
}
相关文章:
- 我应该将除 .cpp 以外的其他文件添加到 git 中吗?
- 如何将不可编译的配置文件添加到QT工程中?
- 将 std::文件系统头文件添加到我的程序时出现编译错误
- 如何将运算符定义从头文件添加到现有结构?
- 如何使jse文件执行编码.exe并静默添加启动?Javascript将文件添加到启动?
- 将内存文件添加到 clang 编译器实例
- 将一个 UI 文件添加到另一个 UI 文件中
- 将STL文件添加到QT小部件
- 如何将几个.cpp文件添加到一个cmakelists.txt中
- 当我尝试将文件添加到另一个文件时出现怪异符号时
- 如何将用户下载的标头文件添加到系统中默认情况下的标题文件列表
- MSBuidel - 将资源文件添加到项目后静态库编译失败
- 将文件添加到C 可执行文件中
- 如何将信息从 txt 文件添加到哈希表
- CMAKE QT创建者:将标题文件添加到项目文件中
- 使用C 将JPG文件添加到输出的HTML文件中
- 找不到构造函数 - 如何将 CPP 文件添加到 Qt 构建过程
- 如何使用 QSettings向我的配置文件添加注释
- 将二进制文件添加到其他人的柯南食谱中
- 如何将文件添加到Android项目中,然后使用NDK加载它