我最近開始在線學習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
你沒有問過,但我認爲你應該知道這並不是正確的密碼方式。關於代碼本身......瞭解如何不會像這樣陷入混亂。通過編寫一小段代碼來開始一個項目。得到這一點,編譯和做正確的事情。 (在這一點上「正確的東西」不會太多。)再加一點,編譯和測試。等等等等。一旦你掌握了它,「錯誤」將很容易找到,因爲它將是你改變的最後一件事。 –
我肯定會按照建議的方法重新編寫代碼。感謝您的建議。 – binu23