我不确定流是如何工作的,因为我对 c++ 还很陌生。我有一个程序,它假设在文件中添加、删除、查看和更新记录。我已经在文件中添加了一些东西,但是其他的有问题。您的帮助将不胜感激。这是我的 driver.cpp 的一部分
int main()
{
Mainmenu();
return 0;
}
void Mainmenu()
{
int option;
cout << "1: Clinic Visit\t\t";
cout << "2: Removal\t\t";
cout << "3: Exit Visit\t\t\n";
cout << "Select an option\n\n\n";
cin >> option;
system("cls");
switch (option)
{
case 1:
Showclinic();
break;
case 2: //RemAnimal();
break;
case 3:
exit(0);
break;
default:
cout << "unknown option";
}
}
void Showclinic()
{
int x;
Intervention inte;
cout << "1: ADD\n\n";
cout << "2: UPDATE\n\n";
cout << "3: VIEW\n\n";
cout << "4: VIEW ALL\n\n";
cout << "5: DELETE\n\n";
cout << "6: Exit\n\n";
cout << "Enter Choice\n";
cin >> x;
switch (x)
{
case 1:
system("cls");
cout << "CURRENTLY ADDING DATA\n";
addVisit("JSPCA.txt", inte);
break;
case 2:
cout << "UPDATE";
system("cls");
break;
case 3:
cout << "VIEW";
system("cls");
break;
case 4:system("cls");
cout << "VIEW ALL RECORDS ON FILE\n";
ViewAll("JPSCA.txt",inte);
system("pause");
break;
case 5:system("cls");
cout << "DELETE";
system("cls");
break;
case 6:
cout << "EXIT";
exit(0);
break;
default:
cout << "value of x unknown";
}
}
std::istream &operator>>(std::istream &is, int i)
{
}
//***********************************************************
std::istream &operator>>(std::istream &is, Intervention &i) {
is >> i.getClient().getInterventionNo();
is >> i.getClient().getClinic();
is >> i.getClient().getFname();
is >> i.getClient().getLname();
is >> i.getClient().getPayment();
is >> i.getAddress().getStreetNo();
is >> i.getAddress().getStreetName();
is >> i.getAddress().getTown();
is >> i.getTelNo().getAreaCode();
is >> i.getTelNo().getExchange();
is >> i.getTelNo().getLine();
is >> i.getAnimal().getBreed();
is >> i.getAnimal().getType();
is >> i.getAnimal().getGender();;
is >> i.getAnimal().getAge();;
is >> i.getAnimal().getProblem();;
is >> i.getDate().getDay();
is >> i.getDate().getMonth();;
is >> i.getDate().getYear();
is >> i.getTime().getHour();
is >> i.getTime().getMin();
//is >> endl;
return is;
}
std::ostream &operator>>(std::ostream &os, Intervention &const i) {
os << i.getClient().getInterventionNo();
os << i.getClient().getClinic() << "";
os << i.getClient().getFname() << "";
os << i.getClient().getLname() << "";
os << i.getClient().getPayment() << "";
os << i.getAddress().getStreetNo() << "";
os << i.getAddress().getStreetName() << "";
os << i.getAddress().getTown() << "";
os << i.getTelNo().getAreaCode() << "";
os << i.getTelNo().getExchange() << "";
os << i.getTelNo().getLine() << "";
os << i.getAnimal().getBreed() << "";
os << i.getAnimal().getType() << "";
os << i.getAnimal().getGender() << "";
os << i.getAnimal().getAge() << "";
os << i.getAnimal().getProblem() << "";
os << i.getDate().getDay() << "";
os << i.getDate().getMonth() << "";
os << i.getDate().getYear() << "";
os << i.getTime().getHour() << "";
os << i.getTime().getMin() << "";
os << endl;
return os;
}
void addVisit(string name, Intervention inte)
{
int hour, min, day, month, yr, snum, age;
string fname, lname, interNo, problem, clinic, area, ex, li, type, breed,sname,town, pay;
cout << "Enter new intervention number\n";
cin >> interNo;
// user enters information, which is copied into file
cout << "Enter name of clinic(Winchester or Caymanas)\n ";
cin >> clinic;
cout << "Enter lastname, firstname\n ";
cin >> lname;
cin >> fname;
cout << "Please Address(Street#, Street Name, Town)\n";
cin >> snum >> sname >> town;
cout << "Enter Contact#:(area exchange line)\n";
cin >> area >> ex >> li;
cout << "Enter Animal Type, Breed,Gender and Age\n";
cin >> type >> breed >> gender >> age;
cout << "Please specify problem\n";
cin >> problem;
cout <<( "Please enter letter for payment type:\n");
cout << ("Full=F\n");
cout << ("Contribution=C\n");
cout << ("Can't Pay=CP\n");
cin >> pay;
cout << "Enter date in format month dd mm yyyy\n";
cin >> day >> month >> yr;
cout << "Enter the time(hour minute\n)";
cin >> hour >> min;
// set record InterventionNo, Client,animal, date and time values
//inte.setInterventionNo(input);
inte.setClient(Client(fname, lname, pay,interNo,clinic));
inte.setAddress(Address(snum, sname, town));
inte.setTelNo(TelNo(area, ex, li));
inte.setAnimal(Animal(type, breed, gender, age, problem));
inte.setDate(Date(day, month, yr));
inte.setTime(Time(hour, min));
ofstream clientfile(name, ios::app);
if (clientfile)
{
clientfile << inte.getClient().getInterventionNo() << "";
clientfile << inte.getClient().getClinic() << "";
clientfile << inte.getClient().getFname() << "";
clientfile << inte.getClient().getLname() << "";
clientfile << inte.getClient().getPayment() << "";
clientfile << inte.getAddress().getStreetNo() << "";
clientfile << inte.getAddress().getStreetName() << "";
clientfile << inte.getAddress().getTown() << "";
clientfile << inte.getTelNo().getAreaCode() << "";
clientfile << inte.getTelNo().getExchange() << "";
clientfile << inte.getTelNo().getLine() << "";
clientfile << inte.getAnimal().getBreed() << "";
clientfile << inte.getAnimal().getType() << "";
clientfile << inte.getAnimal().getGender() << "";
clientfile << inte.getAnimal().getAge() << "";
clientfile << inte.getAnimal().getProblem() << "";
clientfile << inte.getDate().getDay() << "";
clientfile << inte.getDate().getMonth() << "";
clientfile << inte.getDate().getYear() << "";
clientfile << inte.getTime().getHour() << "";
clientfile << inte.getTime().getMin() << "";
clientfile << endl;
clientfile.close();
}//end if
else{
cerr << "Intervention #" << interNo << " already contains information. " << endl;
}
}
//**********************************************************************
void ViewAll(string name, Intervention inte)
{
std::ifstream in(name);
std::copy(std::istream_iterator<Intervention>(in),
std::istream_iterator<Intervention>(),
std::ostream_iterator<Intervention>(std::cout, "\n"));
}