| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1979 人关注过本帖
标题:我在函数返回了一个指针,然后返回指针的内容丢失了,请各位大佬帮忙看看( ...
只看楼主 加入收藏
CNQSM
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2018-7-4
结帖率:0
收藏
已结贴  问题点数:20 回复次数:6 
我在函数返回了一个指针,然后返回指针的内容丢失了,请各位大佬帮忙看看(very thanks)
// 307.cpp : Defines the entry point for the console application.//
//#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
void Menu();
class account
{
        string user;
        string pass;
public:
        void reg();//注册
        void login();//登录
        void password();
private:
        void save();
        string** read();
};
void account::reg()
{  
    ofstream out ("account.dat", ios::app);
    //string disk[4][2];
    //string ** d1;
    string pass1;
    string pass2;
    cout<<"欢迎来到注册页面"<<endl;
    cout<<"请输入用户名"<<endl;   
    cin>>user;
    cout<<"请输入密码"<<endl;
    cin>>pass1;
    cout<<"请再输入一次密码"<<endl;
    cin>>pass2;
    string **disk=read();
    for ( int i = 0 ; i <sizeof(disk[0]); i++)
   {
      if(disk[i][0] != user )
      {
             cout<<"ok"<<endl;
             break;
      }
      else
      {   
      
               cout<<"用户名已经存在"<<endl;
               cout<<"请重新注册:"<<endl;
               reg();
      }
    }
if(pass1==pass2)
    {
      cout<<"注册成功"<<endl;
      save();
     
    }
    else
    {
       cout<<"输入的密码两次不相同"<<endl;
       reg();
    }
}
string** account::read()
{
    ifstream in("account.dat");
    int x, y;
    string disk[4][2];
    for (x = 0; !in.eof(); x++)
    {
        string line;
        getline(in,line);
        istringstream ss(line);
        for (y = 0; !ss.eof(); y++)
        {
            ss >> disk[x][y];   
        }
    }
    /*string *a ;
    a=&disk;*/

   return (string**)disk;
}
搜索更多相关主题的帖子: 指针 string void disk cout 
2018-07-04 15:33
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:10 
string** account::read()
{
    ……
    string disk[4][2];
    ……
    return (string**)disk;
}
你用的是什么编译器,难道这都不给你警告?
2018-07-04 15:50
CNQSM
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2018-7-4
收藏
得分:0 
VC6的编译器,没有警告,然后在运行程序的时候就终止工作。
2018-07-04 16:07
CNQSM
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2018-7-4
收藏
得分:0 
回复 2楼 rjsp
VC6的编译器,没有警告,然后在运行程序的时候就终止工作。
2018-07-04 16:07
CNQSM
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2018-7-4
收藏
得分:0 
// 307.cpp : Defines the entry point for the console application.//
//#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
void Menu();
class account
{
        string user;
        string pass;
public:
        void reg();//注册
        void login();//登录
        void password();
private:
        void save();
        string** read();
};
void account::reg()
{  
    ofstream out ("account.dat", ios::app);
    //string disk[4][2];
    //string ** d1;
    string pass1;
    string pass2;
    cout<<"欢迎来到注册页面"<<endl;
    cout<<"请输入用户名"<<endl;   
    cin>>user;
    cout<<"请输入密码"<<endl;
    cin>>pass1;
    cout<<"请再输入一次密码"<<endl;
    cin>>pass2;
    string **disk=read();
    for ( int i = 0 ; i <sizeof(disk[0]); i++)
   {
      if(disk[i][0] != user )
      {
             cout<<"ok"<<endl;
             break;
      }
      else
      {   
      
               cout<<"用户名已经存在"<<endl;
               cout<<"请重新注册:"<<endl;
               reg();
      }
    }
if(pass1==pass2)
    {
      cout<<"注册成功"<<endl;
      save();
     
    }
    else
    {
       cout<<"输入的密码两次不相同"<<endl;
       reg();
    }
}

void account::login()
{
    //string disk[4][2];
    cout<<"欢迎来到登录界面"<<endl;
    cout<<"请输入您的用户名:";
    cin>>user;
    cout<<"请输入您的密码:";
    cin>>pass;
    cout<<endl;
    string **disk=read();
    for(int i=0;i<sizeof(disk);i++)
    {
        if(disk[i][0]==user&&disk[i][1]==pass)
          {
               cout<<"登录成功!进入下一个菜单"<<endl;
               Menu();
          }
         else
         {
             cout<<"用户名注册错误或者密码错误"<<endl;
             cout<<"请重新登录"<<endl;   
             login();
         }
    }
  
}
string** account::read()
{
    ifstream in("account.dat");
    int x, y;
    string disk[4][2];
    for (x = 0; !in.eof(); x++)
    {
        string line;
        getline(in,line);
        istringstream ss(line);
        for (y = 0; !ss.eof(); y++)
        {
            ss >> disk[x][y];   
        }
    }
    /*string *a ;
    a=&disk;*/
   
   return (string**)disk;
}
void account::save()
{
  ofstream out ("account.dat",ios::out);
  out<<user<<" "<<pass<<endl;
  //out.close();
}
class book
{
  protected:
        string cname;//餐厅名字
        string num;//电话号码
        int month;
        int day;
        int quantity;//人数
        string remark;
  public:
        book(string pcname);
        void enter();
        void report();
};
book::book(string pcname):
cname(pcname)
{}
void book::enter()
{
  cout<<"请输入要预定"+cname+"的相关信息(电话号码、日期、人数、备注):"<<endl;
  cin>>num>>month>>day>>quantity>>remark;
}
void book::report()
{
  cout<<"以下是您预定餐厅的信息"<<endl;
  cout<<"餐厅名字:"<<cname<<endl;
  cout<<"您的电话:"<<num<<endl;
  cout<<"日期为:  "<<month<<"月"<<day<<"日"<<endl;
  cout<<"人数:    "<<quantity<<endl;
  cout<<"备注:    "<<remark<<endl;
}

int main()
{
 account acc1;
  int choose=-1;
  while(1)
  {
    cout<<"请选择"<<endl;
    cout<<"************************"<<endl;
    cout<<"    0.退出     "<<endl;
    cout<<"    1.注册      "<<endl;
    cout<<"    2.登录      "<<endl;
    cout<<"************************"<<endl;
    cin>>choose;
    switch (choose)
    {
        case 1:    acc1.reg();
                   continue;
        case 2:    acc1.login();
                   continue;
        case 0:    exit(0);      
        default:    cout<<"选择超出范围"<<endl;
        continue;
    }
  }
 
  Menu();
  return 0;
}
void Menu()
{
  while(1)
  {
    book a1("307");
    int choose=-1;
    cout<<"请选择"<<endl;
    cout<<"************************"<<endl;
    cout<<"      0. 退出     "<<endl;
    cout<<"      1.填写预定信息      "<<endl;
    cout<<"      2.您的预定信息      "<<endl;
    cout<<"************************"<<endl;
    cin>>choose;
    switch (choose)
    {
        case 1:    a1.enter();
                    continue;
        case 2:    a1.report();
                    break;
        case 0:    exit(0);      
        default:    cout<<"选择超出范围,请重新选择"<<endl;
                    continue;
    }
  }
}
2018-07-04 16:09
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
不建议你写这些无意义的程序(除了浪费生命)
如果是老师布置的作业,你应该考虑换个老师了

程序代码:
#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include <algorithm>

class account_management
{
public:
    bool open( const std::string& filename );
    bool reg( const std::string& username, const std::string& password );
    bool login( const std::string& username, const std::string& password ) const;

private:
    std::map<std::string,std::string> accounts_;
    std::ofstream filestream_;
};

using namespace std;

bool account_management::open( const std::string& filename )
{
    std::ifstream fin( filename.c_str() ); // 在C++中可以直接 filename,估计VC6不支持
    for( std::string username,password; fin>>username>>password; )
        accounts_.insert( std::make_pair(username,password) ); // 在C++中可以直接 emplace,估计VC6不支持
    fin.close();

    filestream_.open( filename.c_str(), std::ios_base::app ); // 在C++中可以直接 filename,估计VC6不支持
    return filestream_.is_open();
}

bool account_management::reg( const std::string& username, const std::string& password )
{
    if( username.empty() || username.find_first_of("\t\n\v\f\r ")!=std::string::npos ) // 不可为空,不可含空白字符
        return false;
    if( password.empty() || password.find_first_of("\t\n\v\f\r ")!=std::string::npos ) // 不可为空,不可含空白字符
        return false;
    if( accounts_.find(username) != accounts_.end() ) // 用户名必须唯一
        return false;
    if( !(filestream_<<username<<' '<<password<<std::endl) )
        return false;
    accounts_.insert( std::make_pair(username,password) ); // 在C++中可以直接 emplace,估计VC6不支持
    return true;
}

bool account_management::login( const std::string& username, const std::string& password ) const
{
    std::map<std::string,std::string>::const_iterator itor = accounts_.find(username);
    return itor!=accounts_.end() && itor->second==password;
}

// 以下为测试

int main( void )
{
    account_management mgt;
    if( !mgt.open("account.dat") )
    {
        cerr << "failed to open account.dat\n";
        return 1;
    }

    if( mgt.reg("a","a_psw") )
        cout << "[true] reg a\n";
    else
        cout << "[false] reg a\n";

    if( mgt.reg("b","b_psw") )
        cout << "[true] reg b\n";
    else
        cout << "[false] reg b\n";

    if( mgt.reg("b","b_psw") )
        cout << "[true] reg b\n";
    else
        cout << "[false] reg b\n";

    if( mgt.login("a","a_psw") )
        cout << "[true] login a\n";
    else
        cout << "[false] login a\n";


    return 0;
}

2018-07-05 09:51
Jonny0201
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:52
帖 子:488
专家分:2603
注 册:2016-11-7
收藏
得分:10 
不用看你的代码, 一看内容丢失就知道你返回的绝对是函数里面的变量
出了函数这个变量就变回收了
声明变量的时候声明为指针然后返回就可以了
2018-07-05 15:23
快速回复:我在函数返回了一个指针,然后返回指针的内容丢失了,请各位大佬帮忙看 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020252 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved