2016-12-17 162 views
0

我最近開始在線學習C++,並開始編寫這段代碼,但需要一些編譯器向我發送的錯誤的幫助。 基本上我只是想從用戶那裏得到一個密碼,如果這是第一次使用'標誌'變量並將密碼以加密形式寫入文本文件。 如果用戶第一次沒有登錄並且保存了密碼,則讀取加密的文本,解密並檢查是否與用戶輸入的密碼相同。C++加密和解密源代碼

#include<iostream> 
#include<string.h> 
#include<fstream> 
using namespace std; 
char encryptpass(char *pass) //function to encrypt 
{ 

    for(int i=0; pass[i] != '\0'; ++i) 
    char enpass[10]= ++pass[i]; 
    return(enpass); 


} 
char decryptpass(char *str) // function to decrypt 
{ 
for(; str!='\0'; ++str) 
char depass[10]= --str; 
return(depass); 

} 

int main()    // main function 
{ 
int flag=0; 
if(flag=0) 
{ 
    cout<<"enter your password"; 
    char pass[10]; 
    cin>>pass; 
    fstream file("userpass.txt",ios::in | ios::out); 
    file<<enpass[10]; 
} 
else 
{ 
    cout<<"enter password"; 
    cin>>pass; 
    bool check=false; 
    static char str[10]; 
    file.seekg(ios::beg); 
    file >> str; 
    file.close(); 
    decryptpass(str); 

    if(pass=depass) // decrypted password is equal to input password ? 
    { 
     check=true;  // set boolen value to true 
    } 
else 
{ 
    cout<<"incorrect password"; 
} 
    return(0); 
} // end of main 

編譯器爲這些錯誤-----

warning : In function 'void encryptpass(char*)': 
line 9 error: array must be initialized with a brace-enclosed initializer 
line 9 warning: unused variable 'enpass' [-Wunused-variable] 
line 10 error: 'enpass' was not declared in this scope 
line 10 error: return-statement with a value, in function returning 'void' [-fpermissive] 
warning : In function 'void decryptpass(char*)': 
line 17error: array must be initialized with a brace-enclosed initializer 
line 17 warning: unused variable 'depass' [-Wunused-variable] 
line 18 error: 'depass' was not declared in this scope 
line 18 error: return-statement with a value, in function returning 'void' [-fpermissive] 
warning : In function 'int main()' 
line 25 warning: suggest parentheses around assignment used as truth value [-Wparentheses] 

line 31 error: 'enpass' was not declared in this scope 
line 36 error: 'pass' was not declared in this scope 
line 39 error: 'file' was not declared in this scope 
line 43 error: 'depass' was not declared in this scope 
+2

你沒有問過,但我認爲你應該知道這並不是正確的密碼方式。關於代碼本身......瞭解如何不會像這樣陷入混亂。通過編寫一小段代碼來開始一個項目。得到這一點,編譯和做正確的事情。 (在這一點上「正確的東西」不會太多。)再加一點,編譯和測試。等等等等。一旦你掌握了它,「錯誤」將很容易找到,因爲它將是你改變的最後一件事。 –

+0

我肯定會按照建議的方法重新編寫代碼。感謝您的建議。 – binu23

回答

1

您編譯器的輸出沒有出現,以配合您的來源,例如根據編譯器,encryptpass的返回類型爲void

第一個錯誤是因爲char empass[10]是大小爲10的char數組的聲明,但是您將它用作char左值。所以之前先聲明它的循環:

char empass[10]; 

然後在for循環,你將與

empass[i] = ++pass[i]; 

但設置編碼的密碼的第i個元素,你的代碼也修改密碼你傳遞給函數:++pass[i]增加pass[i]一個。那真的是你想要的嗎?

+0

我認爲是,在加密函數中,密碼會遞增,然後寫入文本文件。同時解密密碼將再次遞減到原始文本。是邏輯錯誤? – binu23

+0

編譯器輸出和代碼不匹配似乎發生,因爲我試圖通過更改函數的返回類型來解決錯誤。 – binu23

+0

邏輯沒有問題,但是你的代碼存在很多問題。 爲什麼要從舊版本的代碼發佈編譯器輸出?這是當我[編譯你發佈的代碼](http://ideone.com/Z0hs61)時發生的事情。 –