佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 791|回复: 4

C++ 密码的问题...

[复制链接]
发表于 23-11-2005 01:09 AM | 显示全部楼层 |阅读模式
我的program 可以输入密码了,但是如果要更换我应该怎样做?
我现在是read密码from password.txt, 但是如果要换我要怎样写进password.txt 来覆盖原本的密码?
我的coding 如下,请各位大大帮忙,谢谢!

//This Peoject is about system banking.
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<cstring>
#include<conio.h>
#include<iomanip>
using namespace std;


double storemoney(double);
double takeoutmoney(double);
double checking(double);
int password(int);
int password2(int);
int main()
{
        int a;
        int sm;
        int tom;
        int cm;
        int passing;
        int newpass;
       
        password(passing);
        cout<<"\n\n\n\n";
        cout<<"ATM BANK OF EVOLUTION\n\n\n";
        cout<<"1.SAVING\n";
        cout<<"2.DRAWING\n";
        cout<<"3.CHECKING ACCOUNT\n";
        cout<<"4.CHANGE PASSWORD\n";
        cout<<"5.Quit\n\n";
        cout<<"Please Enter A Number\n";
        cin>>a;

        if (a==1)
                storemoney(sm);
               
        else if        (a==2)
                takeoutmoney(tom);
        else if (a==3)
                checking(cm);
        else if (a==4)
                password2(newpass);
        else
                cout<<"Thank You\n"<<"Please Come Again\n";


        return 0;
}

//function to store money
double storemoney(double sm)
{
        double earn;
        ifstream infile;
        ofstream outacc;
        ofstream outfile;
        double accmoney;
        char confirm;


infile.open("account.dat");
        if(infile)
        {
       
        while(infile >> accmoney )
                {
                         
                        storeagain:cout<<"How much you want to store?\n";
                        cin>>earn;
                        cin.ignore();
                        if(earn<=0)
                                {                       
                                        cout<<"Invalid Input\n";
                                        goto again;
                                }                                                       
                        else if(earn>0)
                        {
                                cout<<"You have store "<<earn<<" in the bank\n";
                                accmoney = (accmoney + earn)*1.02;
                                blmoney:cout<<"Your balance in the account is ";
                                                cout.setf(ios::fixed);
                                                cout.setf(ios::showpoint);
                                                cout<<setprecision(2)<<accmoney<<endl;
                        }
                        else
                        {       
                                        again:cout<<"Do You Want to Store Again?(Y/N)\n";
                                        cin>>confirm;
                       
                                        if(confirm=='y'||confirm=='Y')
                                                goto storeagain;
                                        else if(confirm=='n'||confirm=='N')
                                                goto blmoney;
                                        else
                                                cout<<"Invalid Input\n";
                               
                        }
                }
               
                infile.close();

                outacc.open("account.dat");
                outacc<<accmoney;
                outacc.close();

                outfile.open("result.dat");
                if(!outfile)
                {
                        cerr<<"result.dat could not be opened";
                }
                else
                {
                        outfile<<"The account balance is "<<accmoney<<""<<endl;
                        setprecision(2);
                        outfile.close();
                }
        }
        else
        {
                cerr<<"Unable to open the account data"<<endl;
        }

        return 0;
}

//function to take out money from bank
double takeoutmoney(double tom)
{
        double outmoney;
        ifstream infile;
        ofstream outacc;
        ofstream outfile;
        double accmoney;
        char confirm2;


infile.open("account.dat");
        if(infile)
        {
       
        while(infile >> accmoney )
                {
                         
                        takeagain:cout<<"How much you want to take out?\n";
                        cin>>outmoney;
                        cin.ignore();
                        if(outmoney<=0)
                                {                       
                                        cout<<"Invalid Input\n";
                                        goto again;
                                }                                                       
                        else if(outmoney>0)
                        {
                                cout<<"You take out "<<outmoney<<" in the bank\n";
                                accmoney = accmoney - (outmoney*1.02);
                                blmoney:cout<<"Your balance in the account is ";
                                cout.setf(ios::fixed);
                                cout.setf(ios::showpoint);
                                cout<<setprecision(2)<<accmoney<<endl;
                        }
                        else
                        {       
                                        again:cout<<"Do You Want To Take Money Again?(Y/N)\n";
                                        cin>>confirm2;
                       
                                        if(confirm2=='y'||confirm2=='Y')
                                                goto takeagain;
                                        else if(confirm2=='n'||confirm2=='N')
                                                goto blmoney;
                                        else
                                                cout<<"Invalid Input\n";
                                                goto again;
                               
                        }
                }
               
                infile.close();

                outacc.open("account.dat");
                outacc<<accmoney;
                outacc.close();

                outfile.open("result.dat");
                if(!outfile)
                {
                        cerr<<"result.dat could not be opened";
                }
                else
                {
                        outfile<<"The account balance is ";
                        cout.setf(ios::fixed);
                        cout.setf(ios::showpoint);
                        cout<<setprecision(2)<<accmoney<<endl;
                        outfile.close();
                }
        }
        else
        {
                cerr<<"Unable to open the account data"<<endl;
        }

        return 0;
}


//function to checking account
double checking(double cm)
{
        double accmoney;
        fstream infile;

        infile.open("account.dat");
               
                while(infile>>accmoney)
                        cout<<"Your balance in the bank is ";
                        cout.setf(ios::fixed);
                        cout.setf(ios::showpoint);
                        cout<<setprecision(2)<<accmoney<<endl;
                        infile.close();
        return 0;
}


//function to checking password
int password(int passing)
{
        int ch;
        int i=0;
        char user_in[50];
        char read[50];
        ifstream read_pass("pass.txt");
        if(read_pass.fail())
                cerr<<"ERROR";
passagain:cout<<"Enter the correct password to access the program\n";
        fflush(stdout);
  
        while ((ch = getch()) != EOF && ch != '\n' && ch != '\r') //&&i<10)
        {
                if (ch == '\b' && i > 0)
                {
                        cout<<"\b \b";
                        fflush(stdout);
                        i--;
                        user_in[i] = '\0';
                }
                else if (isalnum(ch))
                {
                        putchar('*');
                        user_in[i++] = (char)ch;
                }
        }

  user_in[i] = '\0';

        read_pass.getline(read, 50);
        if(strcmp(user_in,read)){
                cout<<"\nAccess denied\n\n";
                goto passagain;
        }
        return 0;
}

//function to change password...
int password2(int newpass)
{
        int ch2;
        int n=0;
        char user_in[50];
        char read[50];
        ifstream read_pass("pass.txt");
        if(read_pass.fail())
                cerr<<"ERROR";
        cout<<"Please Enter A new password\n";
        fflush(stdout);

        while((ch2=getch())!=EOF&&ch2!='\n'&&ch2!='\r')
        {
                if (ch2=='\b'&&n>0)
                {
                        cout<<"\b \b";
                        fflush(stdout);
                        n--;
                        user_in[n]='\0';
                }
                else if (isalnum(ch2))
                {
                        putchar('*');
                        user_in[n++]=(char)ch2;
                }
               
        }
        cout<<"\n";
        cout<<"You Successful To Change The Password"<<endl;
        main();
        return 0;
}
回复

使用道具 举报


ADVERTISEMENT

发表于 23-11-2005 03:38 AM | 显示全部楼层
原帖由 evo9 于 23-11-2005 01:09 AM 发表
我的program 可以输入密码了,但是如果要更换我应该怎样做?
我现在是read密码from password.txt, 但是如果要换我要怎样写进password.txt 来覆盖原本的密码?...

要更改的密码已经收在user_in里了。
再加段code,将写进pass.txt就行了。
ifstream read_pass("pass.txt");
     if(read_pass.fail())
        cerr<<"ERROR";
        cout<<"Please Enter A new password\n";
fflush(stdout);

这一段code是用来读file的,在 function int password2(int newpass)里没用到,可删掉。改成一段写file的code即可。


ofstream write_pass("pass.txt",ios::out);
     if(write_pass.fail())
        cerr<<"ERROR";
        cout<<"Please Enter A new password\n";
fflush(stdout);

改成这段code,是用来打开pass.txt。准备将user_in写进去。


之后在将新密码储存在user_in后,加入:

write_pass<<user_in<<endl;

就将密码写进pass.txt里了。
回复

使用道具 举报

 楼主| 发表于 23-11-2005 02:04 PM | 显示全部楼层
//function to change password...
int password2(int newpass)
{
        int ch2;
        int n=0;
        char user_in[50];
        char read[50];
        ofstream write_pass("pass.txt",ios:ut);
     if(write_pass.fail())
        cerr<<"ERROR";
        cout<<"lease Enter A new password\n";
        fflush(stdout);

        while((ch2=getch())!=EOF&&ch2!='\n'&&ch2!='\r')
        {
                if (ch2=='\b'&&n>0)
                {
                        cout<<"\b \b";
                        fflush(stdout);
                        n--;
                        user_in[n]='\0';
                        write_pass<<user_in<<endl;
                }
                else if (isalnum(ch2))
                {
                        putchar('*');
                        user_in[n++]=(char)ch2;
                        write_pass<<user_in<<endl;
                }
               
        }
        cout<<"\n";
        cout<<"You Successful To Change The Password"<<endl;
        main();
        return 0;
}

我改了,可是还是不能....
我把密码改称david,
我的pass.txt 就自动变成
d&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;
da&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;
dav&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;
davi&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;
david&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;&#204;

谢谢,楼上的帮忙
请高手再指点...
回复

使用道具 举报

发表于 23-11-2005 05:26 PM | 显示全部楼层

RE 原帖由 [i]evo9[/i] 于 23-11-2005 02:04 PM 发表

//function to change password...
int password2(int newpass) //int newpass 可以删掉,因为没用到。{
        int ch2;
        int n=0;
        char user_in[50];
        char read[50]; //char read[50]; 可以删掉,因为没用到。
        ofstream write_pass("pass.txt",ios:ut);
     if(write_pass.fail())
        cerr<<"ERROR";
        cout<<"Please Enter A new password\n";
        fflush(stdout);

        while((ch2=getch())!=EOF&&ch2!='\n'&&ch2!='\r')
        {
                if (ch2=='\b'&&n>0)
                {
                        cout<<"\b \b";
                        fflush(stdout);
                        n--;
                        user_in[n]='\0';//这段code放错地方了应该放在 这里
                }
                else if (isalnum(ch2))
                {
                        putchar('*');
                        user_in[n++]=(char)ch2;
                        write_pass<<user_in<<endl;//这段code放错地方了应该放在 这里               }
               
        }
        user_in[n]='\0';
        write_pass<<user_in<<endl;

        cout<<"\n";
        cout<<"You Successful To Change The Password"<<endl;
        main();
        return 0;
}
回复

使用道具 举报

 楼主| 发表于 23-11-2005 06:21 PM | 显示全部楼层
谢谢搂上的大大,现在整个program 没有error, compile 时候没有error 和warning,
但是build的时候就有两个error....

main2.obj : error LNK2001: unresolved external symbol "int __cdecl password(int)" (?password@@YAHH@Z)
Debug/main2.exe : fatal error LNK1120: 1 unresolved externals

请问是什么意思?
请各位大大帮忙....谢谢!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 14-8-2025 06:22 AM , Processed in 0.114912 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表