我最近开始在线学习c ++并开始研究这段代码,但需要一些帮助来解决编译器给我带来的错误。 基本上我只是想从用户那里获得一个密码,如果它是第一次使用'flag'变量并将加密形式的密码写入文本文件。 如果用户第一次没有登录并且保存了密码,则读取加密文本,对其进行解密,并检查它是否等于用户输入的密码。
#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
答案 0 :(得分:1)
您的编译器输出似乎与您的来源不匹配,例如根据编译器,encryptpass
的返回类型为void
。
第一个错误是因为char empass[10]
是一个大小为10的char
数组的声明,但是你正在使用它像char值。所以首先在for循环之前声明它:
char empass[10];
然后在for循环中,您将使用
设置编码密码的第i个元素empass[i] = ++pass[i];
但是,您的代码也在修改传递给函数的密码:++pass[i]
将pass[i]
增加1。这真的是你想要的吗?