C++中怎么把数据信息写到一个文件中,请高手指点程序如下:
#include<iostream>#include<fstream>usingnamespacestd;charname[10][80];//thisarrayhold...
#include <iostream>
#include<fstream>
using namespace std;
char name[10][80]; // this array holds employee names
char phone[10][20]; // their phone numbers
float hours[10]; // hours worked per week
float wage[10]; // wage
int i;
int menu();
void enter(), report(),save();
int main()
{
int choice;
do {
choice = menu(); // get selection
switch(choice) {
case 0: break;
case 1: enter();
break;
case 2: report();
break;
default: cout << "Try again.\n\n";
}
} while(choice != 0);
return 0;
}
// Return a user's selection.
int menu()
{
int choice;
cout << "0. Quit\n";
cout << "1. Enter information\n";
cout << "2. Report information\n";
cout << "\nChoose one: ";
cin >> choice;
return choice;
cin.get();
}
// Enter information.
void enter()
{
char temp[80];
for(i=0; i<3; i++) {
cout << "Enter last name: ";
cin >> name[i];
cout << "Enter phone number: ";
cin >> phone[i];
cout << "Enter number of hours worked: ";
cin >> hours[i];
cout << "Enter wage: ";
cin >> wage[i];
save();
}
cout<<"输入完毕!";
}
// Display report.
void report()
{
int i;
for(i=0; i<10; i++) {
cout << name[i] << ' ' << phone[i] << '\n';
cout << "Pay for the week: " << wage[i] * hours[i];
cout << '\n';
}
}
void save()
{
fstream outfile;
outfile.open("information");
if(!outfile)
{
cout<<" Can't open.\n";
}
outfile<<name[i]<<" "<<phone[i]<<" "<<hours[i]<<" "<<wage[i]<<"\n";
outfile.close();
}
上面是程序,怎么把人员信息写入到一个文件中? 展开
#include<fstream>
using namespace std;
char name[10][80]; // this array holds employee names
char phone[10][20]; // their phone numbers
float hours[10]; // hours worked per week
float wage[10]; // wage
int i;
int menu();
void enter(), report(),save();
int main()
{
int choice;
do {
choice = menu(); // get selection
switch(choice) {
case 0: break;
case 1: enter();
break;
case 2: report();
break;
default: cout << "Try again.\n\n";
}
} while(choice != 0);
return 0;
}
// Return a user's selection.
int menu()
{
int choice;
cout << "0. Quit\n";
cout << "1. Enter information\n";
cout << "2. Report information\n";
cout << "\nChoose one: ";
cin >> choice;
return choice;
cin.get();
}
// Enter information.
void enter()
{
char temp[80];
for(i=0; i<3; i++) {
cout << "Enter last name: ";
cin >> name[i];
cout << "Enter phone number: ";
cin >> phone[i];
cout << "Enter number of hours worked: ";
cin >> hours[i];
cout << "Enter wage: ";
cin >> wage[i];
save();
}
cout<<"输入完毕!";
}
// Display report.
void report()
{
int i;
for(i=0; i<10; i++) {
cout << name[i] << ' ' << phone[i] << '\n';
cout << "Pay for the week: " << wage[i] * hours[i];
cout << '\n';
}
}
void save()
{
fstream outfile;
outfile.open("information");
if(!outfile)
{
cout<<" Can't open.\n";
}
outfile<<name[i]<<" "<<phone[i]<<" "<<hours[i]<<" "<<wage[i]<<"\n";
outfile.close();
}
上面是程序,怎么把人员信息写入到一个文件中? 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询