3 Replies - 697 Views - Last Post: 19 January 2011 - 06:04 AM Rate Topic: -----

#1 solomon201  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 19-November 07

Password help

Posted 18 January 2011 - 12:52 AM

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.

#include<vector>
#include<iostream>
#include<string>
#include<conio.h>
using namespace std;
string getpass()
{
	
    vector<char> pass;
    char ch;
    ch=getch();
    while(!isspace(ch)&&pass.size()<20)
    {
       if(ch==8)
       {
       if(!pass.empty())
       pass.pop_back();
       }
       else{
       cout<<"*";
       pass.push_back(ch);
       }
       ch=getch();
       }
       char check[128];
       for(int r=0;r<pass.size();r++)
       check[r]=pass[r];
       string rvalue;
       int limit=pass.size();
       check[pass.size()]='\r';
       rvalue=check;
       return rvalue;
}


This post has been edited by no2pencil: 18 January 2011 - 12:56 AM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Password help

#2 Munawwar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 162
  • View blog
  • Posts: 457
  • Joined: 20-January 10

Re: Password help

Posted 18 January 2011 - 06:58 AM

check isn't null terminated. That is, it should end with a '\0' (ASCII 0).
check[pass.size()]='\r'; //This is the last character in your code


Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Password help

Posted 18 January 2011 - 09:19 AM

I think Munawwar is probably correct, however why are you using the char array at all?


You start by using a vector, then copy the vector into an array and then convert that to a string... really seems like an odd way around.

You can convert a vector<char> into a string pretty easily.

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main() {
    vector<char> vect;
    vect.push_back('H');
    vect.push_back('E');
    vect.push_back('L');
    vect.push_back('L');
    vect.push_back('O');
    
    string str(vect.begin(), vect.end());
    
    cout << str << endl;
    
    vect.push_back(' ');
    vect.push_back('W');
    vect.push_back('O');
    vect.push_back('R');
    vect.push_back('L');
    vect.push_back('D');
    
    str = string(vect.begin(), vect.end());

    cout << str << endl;

    return 0;
}


so there is no need to use this "check" array to begin with.
Was This Post Helpful? 2
  • +
  • -

#4 solomon201  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 19-November 07

Re: Password help

Posted 19 January 2011 - 06:04 AM

Thanks NickDMax I got it my program is running very ok now...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1