嗨,我正在登录并注销代码。 [英] Hi, I am making a sign in and sign out code.
问题描述
我已经弄明白了,主要的错误是在注册过程中检查相同的用户名是不行的。请帮助
我尝试过:
I've figured out everything, the main error is that the check for same username during sign up is not working. Please help
What I have tried:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include<stdlib.h>
#include<time.h>
#include <cstdlib>
#include "conio.h"
using namespace std;
int check(string username)
{
cin >> username;
string data1, data2;
ifstream infile;
infile.open("profile.txt", ios::app);
getline(infile, data1);
if (username == data1)
{
cout << "Username exits" << endl;
cout << "Enter new username: ";
cin >> username;
return check(username);
}
else
{
}
}
int main()
{
int z;
FIRST:
cout << "Press 1 to Create Account" << endl; cout << "Press 2 to Log In " << endl; cout << "your choice: ";
VALID: cin >> z;
switch (z)
{
case 1:
{
string name, pas, repas;
cout << "Enter Your username:";
cin >> name;
check(name);
cout << "Enter Password(Atleast 5 characters) :";
START1: cin >> pas;
if (pas.length() < 5)
{
cout << "Enter a password of atleast 5 characters : ";
goto START1;
}
else
cout << "Re-write Password to confirm:";
START2: cin >> repas;
if (repas != pas)
{
cout << "Re-Enter the Password !";
goto START2;
}
else
cout << "Profile Created With the User Name " << name << endl; // cout on the console
ofstream myfile;
myfile.open("profile.txt",ios::app);
myfile << name << endl;
myfile << pas << endl;
myfile.close();
_getch();
system("CLS");
goto FIRST;
}
case 2: // For logging in
{
system("CLS");
cout << "Welcome to Login screen\n\m";
char pass[32];
string name, pas;
START3:
cout << "Enter Username:";
cin >> name;
string data1, data2;
ifstream infile;
infile.open("profile.txt",ios::app);
getline(infile, data1);
if (name != data1)
{
cout << "Invalid User Name.Try Again\n";
goto START3;
}
else
cout << "User Name Found\n";
cout << "Enter Password : ";
int i = 0;
char a;
for (i = 0;;)
{
a = _getch();
if ((a >= 'a'&&a <= 'z') || (a >= 'A'&&a <= 'Z') || (a >= '0'&&a <= '9'))
{
pass[i] = a;
++i;
cout << "*";
}
if (a == '\b'&&i >= 1)
{
cout << "\b \b";
--i;
}
if (a == '\r')
{
pass[i] = '\0';
break;
}
}
getline(infile, data2);
START4: if (pass == data2)
{
cout << "You are Logged In";
_getch();
}
else
{
cout << "Invalid Password\nEnter Again:";
char pass[32];
cout << "Enter Password : ";
int i = 0;
char a;
for (i = 0;;)
{
a = _getch();
if ((a >= 'a'&&a <= 'z') || (a >= 'A'&&a <= 'Z') || (a >= '0'&&a <= '9'))
{
pass[i] = a;
++i;
cout << "*";
}
if (a == '\b'&&i >= 1)
{
cout << "\b \b";
--i;
}
if (a == '\r')
{
pass[i] = '\0';
break;
}
}
goto START4;
}
infile.close();
break;
}
default:
{
cout << "Please Enter Valid Number !";
goto VALID; }
}
}
推荐答案
int check(字符串用户名)
{
cin>>用户名;
字符串数据1,数据2;
ifstream infile;
infile.open(profile.txt,ios :: app); //< - 你要将什么附加到输入文件?
getline(infile,data1);
if(username == data1)
{
cout<< 用户名退出<< endl;
cout<< 输入新用户名:;
cin>>用户名;
返回检查(用户名); //< - 以递归方式调用函数(文件一次又一次打开)不是一个好主意 < br $>
}
其他
{
}
}
int check(string username)
{
cin >> username;
string data1, data2;
ifstream infile;
infile.open("profile.txt", ios::app); //<-- What are you going to append to a input file?
getline(infile, data1);
if (username == data1)
{
cout << "Username exits" << endl;
cout << "Enter new username: ";
cin >> username;
return check(username);//<-- it is NOT a good idea to call the function recursively (the file is open again and again)
}
else
{
}
}
我的建议:执行检查
功能只是为了检查,然后再问一遍(在调用代码中)用户输入用户名如果失败。
我的一般建议:不要使用转到
,使用 C ++
功能。
My advice: implement the check
function simply for checking, then ask again (in the calling code) the user to enter the username if it fails.
My general advice: don't use goto
, use the C++
features.
这篇关于嗨,我正在登录并注销代码。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!