PDA

View Full Version : سوال: ورود رشته و نمایش به صورت رمز



محبوبه بیگی
دوشنبه 22 مهر 1392, 21:24 عصر
سلام
میشه راهنماییم کنید
میخوام یک برنامه بنویسم که یک رمز رو از کاربر بگیره و به صورت ستاره نمایش بده بعد اگه با رشته موردنظرم مساوی بود یه پیغام نشون بده

mam_65
دوشنبه 22 مهر 1392, 21:32 عصر
سلام
میشه راهنماییم کنید
میخوام یک برنامه بنویسم که یک رمز رو از کاربر بگیره و به صورت ستاره نمایش بده بعد اگه با رشته موردنظرم مساوی بود یه پیغام نشون بده

#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
cout << "Please enter the password";
char userData[71]; // I've allocated 71 characters to this string, which matches the amount of characters you accept in your getline call (plus one more for the required end of string character '\0').
do
{
cin.getline(userData, 70);
//says the data back
cout << userData;

// Notice, I've moved this block inside the loop, as it needs to be tested after each password entry rather than after the user has input an empty line.
if (!strcmp(userData, "This is my password")) // I've changed this statement to use the strcmp function to actually compare the values of the entire strings together. The result of this function is the total number of differences found, so if it returns 0 that means the strings are the same. Also note, I am using double quotes instead of single for the password value.
{
cout << "Welcome back";
break; // I've added a break here to break out of the loop when the user inputs the correct password.
}
else
{
// Removed the infinite loop, as it has no purpose other than locking up the program.
cout << "******** INTRUDER ALERT *********";
}

}while (strlen(userData) != 0); // I'm not too sure about this, but I think if you enter a blank line it does not contain the '\n' within itself. Instead, I've opted to use the strlen() function which tells you how many letters are in the given string.

return 0;
}

vasilopita
سه شنبه 23 مهر 1392, 09:59 صبح
#include <iostream>
#include <conio.h>
using namespace std;

void main()
{
while(1)
{
char chr='\0',pass[20]="\0";
int i=0;
chr=getch();
while(chr!='\r')
{
cout<<"*";
pass[i]=chr;
i++;
chr=getch();
}
if(strcmp(pass,"my pass")==0) //set your password here!
break; //if pass is correct break
cout<<"\nWrong Password!";
_sleep(1000);
system("cls");
}
}

محبوبه بیگی
سه شنبه 23 مهر 1392, 18:44 عصر
:تشویق:سلام مرسی خیلی عالی بود
میشه یه توضیح کلی بدید که چی شد؟

محبوبه بیگی
سه شنبه 23 مهر 1392, 18:45 عصر
#include <iostream>
#include <conio.h>
using namespace std;

void main()
{
while(1)
{
char chr='\0',pass[20]="\0";
int i=0;
chr=getch();
while(chr!='\r')
{
cout<<"*";
pass[i]=chr;
i++;
chr=getch();
}
if(strcmp(pass,"my pass")==0) //set your password here!
break; //if pass is correct break
cout<<"\nWrong Password!";
_sleep(1000);
system("cls");
}
}

سلام مرسی خیلی عالی بود
میشه یه توضیح کلی بدید که چی شد؟

vasilopita
سه شنبه 23 مهر 1392, 23:41 عصر
خوب من یک کاراکتر تعریف کردم و یه آرایه از کارکتر. تو یه حلقه ی بی نهایت میاد کاراکتر هایی رو که کار وارد می کنه بترتیب می گیره. تابع getch کاراکتر وارد شده رو نشون نمی ده. بعد تو یه حلقه ی دیگه میاد بررسی می کنه که اگه کاراکتر وارد شده اینتر نباشه وارد حلقه بشه و تو ضفحه بجاش ستاره چاپ کنه. بعد از اون کاراکتر رو بترتیب تو خونه های آرایه میریزه. تا زمانی که کاربر اینتر رو نزده این فرایند ادامه داره. اگه کاربر اینتر رو بزنه دیگه وارد حلقه نمی شه. حالا میاد آرایه رو با پسورد مقایسه می کنه اگه درست بود که از حلقه خارج میشه اما اگه غلط بود چاپ می کنه غلطه و بعد از یک ثانیه صفحه رو پاک می کنه و دباره می پره اول حلقه.

اگه دقت کنید خیلی سخت نیست
موفق باشید

محبوبه بیگی
جمعه 10 آبان 1392, 12:32 عصر
سلام ممنون از راهنماییتون
اما یک سوال اگه کاربر بخواد رمزشو پاک کنه و backspaceرد از صفحه کلید میزنه بازم ستاره تایپ میشه

jahadgar91
دوشنبه 18 آذر 1392, 17:20 عصر
با Borland C++