Problem with password function

I have this header file below for getting a password in a console mode program. The problem i have is that when the getpass() returns the string rvalue, it returns some funny characters together with it. Pls I need help in correcting this. Is there a better way to implement this password function? Please all help is welcome.

view source
print?
01 #include<vector>
02 #include<iostream>
03 #include<string>
04 #include<conio.h>
05 using namespace std;
06 string getpass()
07 {
08
09 vector<char> pass;
10 char ch;
11 ch=getch();
12 while(!isspace(ch)&&pass.size()<20)
13 {
14 if(ch==8)
15 {
16 if(!pass.empty())
17 pass.pop_back();
18 }
19 else{
20 cout<<"*";
21 pass.push_back(ch);
22 }
23 ch=getch();
24 }
25 char check[128];
26 for(int r=0;r<pass.size();r++)
27 check[r]=pass[r];
28 string rvalue;
29 int limit=pass.size();
30 check[pass.size()]='\r';
31 rvalue=check;
32 return rvalue;
33 }
you're missing a '}' and don't put the line numbers ... use
[code]
[/code]

instead... makes things easier to read and copy from... (and puts line numbers for you)

plus, the code is really hard to read when you one line everything like that and makes people not want to help so much... well, unless they are crazy like me...

beyond that...


check[pass.size()]='\r'; did you mean '\0'?

on another note? have you looked into append() and erase() on strings... that would save you making a vector and a char array... plus, why can't your passwords have spaces?

Topic archived. No new replies allowed.