2015-08-15 42 views
-2

我想做一個插入函數,我想傳遞一個對象數組給它。結構數組的插入函數

該程序工作正常,但是當我添加插入函數時,它停止正常工作,因爲我不知道如何在主函數內調用它時傳遞插入函數的參數。

請幫我寫出正確的插入函數調用,如果你有任何重要的修改或對代碼的評論寫它請。

下面是代碼:

#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(); 
} 
+0

這不是你應該如何在SO上提出你的問題。說出你的功能應該做什麼。說明它不起作用的方式,並在適用的情況下給出錯誤代碼。縮進你的代碼,使其可讀,並嘗試只給出相關的代碼位 - 而不是整個程序......你將得到更好的迴應和更多的幫助。 –

+2

看不出這會怎樣幫助其他人。另外我不明白你爲什麼使用C字符串和C庫函數,爲什麼你聲明'x','m','a','i'(可怕的名字)和C文件句柄'myfile' ..沒有你曾經使用過的。最後,你不用任何參數調用函數,所以難怪它不起作用。我建議在繼續之前再次閱讀你的C++書籍以學習語言,因爲猜測編程不起作用。 –

回答

0

如果你想工人的數組傳遞給你的函數,那麼第一個想到你必須做的是什麼地方在你的代碼申報工人的數組。在main似乎顯而易見的地方。一旦你聲明瞭這個數組,很容易將它傳遞給函數。

int main() 
{ 
... 
workers w[20]; 
insertfunction(w); 
} 

也是從這個代碼

struct workers 
{ 
int no; 
char name[40]; 
int age; 
int tel; 
char designationdate[14]; 
char section[14]; 
float monthlysalary; 
}w; 
^ delete this 

它聲明瞭一個名爲W¯¯全局變量刪除w。我看不出爲什麼你想要那樣。

其他一些技巧

FILE *myfile; 

int k=0; 
struct x; 
char m;// this variable belong to the time function . 

這些變量沒有被你的代碼中使用
int a,i; 

,刪除這些行。

George Simms所說的也是正確的。

+0

Sam Redway感謝您的好建議,軌道上的Lightness Races非常感謝你,我將學習更多關於C++的知識,George Simms你是完全正確的我做了你所說的並且它已經變得非常出色,而且約翰非常感謝你。現在它是正確的..感謝你們所有人。 – grandx

1

首先,for循環缺少大括號或縮進,所以它會嘗試在下一個分號後結束循環。

這意味着在

cin>>w[i].no; 

i將超出範圍。

另外insertfunction被聲明爲有參數但沒有任何被調用。