我想做一個插入函數,我想傳遞一個對象數組給它。結構數組的插入函數
該程序工作正常,但是當我添加插入函數時,它停止正常工作,因爲我不知道如何在主函數內調用它時傳遞插入函數的參數。
請幫我寫出正確的插入函數調用,如果你有任何重要的修改或對代碼的評論寫它請。
下面是代碼:
#include<iostream>
#include<cstring>
#include<string>
#include<cstdlib>
#include<stdio.h>
#include<time.h>
void insertfunction();
using namespace std;
struct workers {
int no;
char name[40];
int age;
int tel;
char designationdate[14];
char section[14];
float monthlysalary;
} w;
const std::string currentDateTime() {
time_t now = time(0);
struct tm tstruct;
char m,buf[80];
tstruct = *localtime(&now);
strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);
return buf;
}
//next is the insertion function .
void insertfunction(workers w[20]) {
int i;
cout << "\n Enter the information of the worker who you want to add : ";
for(int i = 0; i < 20; i++)
cout << "\n Enter worker no : ";
cin >> w[i].no;
cout << "\n Enter worker name : ";
cin >> w[i].name;
cout << "\n Enter worker age : ";
cin >> w[i].age;
cout << "\n Enter worker telephone : ";
cin >> w[i].tel;
cout << "\n Enter worker designation date : ";
cin >> w[i].designationdate;
cout << "\n Enter worker section : ";
cin >> w[i].section;
cout << "\n Enter worker monthly salary : ";
cin >> w[i].monthlysalary;
}
int main(int argc, char* argv[]) {
FILE* myfile;
char pass1[10] = "ibraheem";
char pass2[10];
char pass3[10] = "22";
char pass4[10];
int k = 0;
struct x;
char m;// this variable belong to the time function .
do {
cout << "\n\n enter your name : ";
cin >> pass2;
} while((strcmp(pass1,pass2)) != 0);
do {
cout << "\n\n enter the password : ";
cin >> pass4;
} while((strcmp(pass3,pass4)) != 0);
int a,i;
cout << "\n\n greenland bakeries group";
cout << "\n\n sedra department \n\n";
std::cout << "current Date Time is = " << currentDateTime() << std::endl;
cout<<"\n\n\n ";
cout<<"\n\n\n Add a worker record : ";
//next is the calling of the insertion function :
insertfunction();
}
這不是你應該如何在SO上提出你的問題。說出你的功能應該做什麼。說明它不起作用的方式,並在適用的情況下給出錯誤代碼。縮進你的代碼,使其可讀,並嘗試只給出相關的代碼位 - 而不是整個程序......你將得到更好的迴應和更多的幫助。 –
看不出這會怎樣幫助其他人。另外我不明白你爲什麼使用C字符串和C庫函數,爲什麼你聲明'x','m','a','i'(可怕的名字)和C文件句柄'myfile' ..沒有你曾經使用過的。最後,你不用任何參數調用函數,所以難怪它不起作用。我建議在繼續之前再次閱讀你的C++書籍以學習語言,因爲猜測編程不起作用。 –