注册 登录
编程论坛 C++教室

一个简单的引用问题

yxieguodong 发布于 2011-04-17 22:06, 798 次点击
我想实现密码输入时显示*,然后又能传回这个值
能通过编译,可是运行出错了,也不知道错在哪个地方


程序代码:
#include<iostream>
#include<string>
#include<conio.h>
using namespace std;
void mima(string& key)
{
    for(int i=0;i<15;i++)
    {
        char c;
        
        c = getch();
        if(c==13)
        {
            c='\0';
            break;
        }
        
        else cout << '*';
        key[i]=c;
    }
}

int main()
{
    string a;
    mima(a);
    cout<<a;
    return 0;
}


[ 本帖最后由 yxieguodong 于 2011-4-17 23:32 编辑 ]
17 回复
#2
qq10235692232011-04-17 23:11
谁能告诉我,C,C++有string这个数据类型还是没有?
#3
yxieguodong2011-04-17 23:26
回复 2楼 qq1023569223
好像是这么回事。那我怎么把这个值传回去呢,要用全局么
#4
pangding2011-04-17 23:30
C++ 里有 string 类,在 <string> 里。


[ 本帖最后由 pangding 于 2011-4-17 23:31 编辑 ]
#5
yxieguodong2011-04-17 23:47
4楼 pangding
然后呢
#6
pangding2011-04-18 00:08
回复 5楼 yxieguodong
没用过 TC 不太能解决你现在的问题。
#7
xishui7772011-04-18 02:11
#include<iostream>
#include<conio.h>
using namespace std;
void mima(char *key)
{
    int i;
    for(i=0;i<15;i++)
    {
        key[i]=getch();
        if(key[i]==13)
            break;
        else
            if(i==14)
            {
                cout<<endl<<"密码应小于15为数"<<endl;
                break;
            }
            else
                cout<<'*';
    }
   
    key[i]='\0';
    cout<<endl;
}

int main()
{
    char a[15];
    mima(a);
    cout<<a<<endl;
    return 0;
}
#8
xishui7772011-04-18 02:18
似似,看看是不是你想要的
#9
yxieguodong2011-04-18 12:53
以下是引用xishui777在2011-4-18 02:18:57的发言:

似似,看看是不是你想要的

对对,只是怎么把这个char数组和string比较呢
#10
yxieguodong2011-04-18 12:53
以下是引用xishui777在2011-4-18 02:18:57的发言:

似似,看看是不是你想要的

对对,只是怎么把这个char数组和string比较呢,重发了。。。要把char[]转换为string么
#11
xishui7772011-04-18 13:15
对于string 这个类型我不是很会,你问问高手吧
#12
yxieguodong2011-04-18 13:21
回复 10楼 yxieguodong
好像可以直接比较,运行通过了。两个char[]比较还出错了。。
#13
ucyan2011-04-18 18:53
程序代码:
#include <conio.h>
#include <cctype>
#include <iostream>
#include <string>
using namespace std;

int main(int argc, char* argv[])
{

 char c;

 int i=0;

 string pas;

 while ((c=getch())!='\r')
  {
   if (c=='\b') {
      putch(c);
      putch(' ');
      putch(c);
      if (!pas.empty()) pas=pas.erase(pas.size()-1,1);
   }
   else if (isprint(c)){   
    putch('*');
    pas+=c;
   }
  }

 cout<<endl;

 cout<<pas<<endl;               

 return 0;
}
可以退格的星号输出~
#14
bqrmt2011-04-18 20:50
七楼改的对
没有string类型
#15
寒风中的细雨2011-04-18 20:59
程序代码:
#include<iostream>
#include<string>
#include<conio.h>
using namespace std;

void mima(string& key)
{
    char c;

    for(int i=0;i<15;i++)
    {
        c = getch();

        if (c == 13)
        {
            c='\0';
            break;
        }
        else
        {
            cout << '*';
        }

        key = key + c;
    }
}

int main()
{
    string a;
   
    mima(a);

    cout << endl << a << endl;

    return 0;
}


只有本站会员才能查看附件,请 登录
#16
yxieguodong2011-04-18 23:42
回复 13楼 ucyan
学习了,谢谢大家了
#17
yxieguodong2011-04-19 00:36
以下是引用ucyan在2011-4-18 18:53:21的发言:

#include <conio.h>
#include <cctype>
#include <iostream>
#include <string>
using namespace std;

int main(int argc, char* argv[])
{
 char c;
 int i=0;
 string pas;
 while ((c=getch())!='\r')
  {
   if (c=='\b') {
      putch(c);
      putch(' ');
      putch(c);
      if (!pas.empty()) pas=pas.erase(pas.size()-1,1);
   }
   else if (isprint(c)){   
    putch('*');
    pas+=c;
   }
  }
 cout<<endl;
 cout<<pas<<endl;               
 return 0;
}
可以退格的星号输出~

我想通过调用怎么实现
#18
yxieguodong2011-04-19 00:36
以下是引用ucyan在2011-4-18 18:53:21的发言:

#include <conio.h>
#include <cctype>
#include <iostream>
#include <string>
using namespace std;

int main(int argc, char* argv[])
{
 char c;
 int i=0;
 string pas;
 while ((c=getch())!='\r')
  {
   if (c=='\b') {
      putch(c);
      putch(' ');
      putch(c);
      if (!pas.empty()) pas=pas.erase(pas.size()-1,1);
   }
   else if (isprint(c)){   
    putch('*');
    pas+=c;
   }
  }
 cout<<endl;
 cout<<pas<<endl;               
 return 0;
}
可以退格的星号输出~

我想通过调用怎么实现
1