level 5
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class Sign{
public:
char ch;
friend class DisplayUI;
void Input_pass(){
while((ch=getchar())!='\n'){
password.push_back(ch);
}
}
void Input_id(){
while((ch=getchar())!='\n'){
id.push_back(ch);}
}
bool Match(){
if(password[0]=='r')
return true;
else
return false;
}
void txt1(){
cout<<"请输入您的用户名:"<<endl;
}
void txt2(){
cout<<"请输入您的密码:"<<endl;
}
private:
vector<char> password;
vector<char> id;
};
class DisplayUI
{
public:
int flag;
int sigh(int m_flag);
DisplayUI()
{
Sign test2;//????
}
void lll(){
cout<<"请选择"<<endl<<"1.注册"<<endl<<"2.登录"<<endl;
}
private:
Sign test2;
};
int DisplayUI::sigh(int m_flag){
if(m_flag==1){
system("clear");
test2.txt1();
test2.Input_id();
test2.txt2();
test2.Input_pass();
system("clear");
cout<<"sucess"<<endl;
lll();
}
else{
system("clear");
test2.txt1();
test2.Input_id();
test2.txt2();
test2.Input_pass();
system("clear");
if(test2.Match())
cout<<"success";
else
{
test2.id.clear();
test2.password.clear();
return sigh(m_flag);
}
}
}
int main()
{
int m_flag;
DisplayUI test;
test.lll();
cin>>m_flag;
test.sigh(m_flag);
}
2022年11月04日 07点11分
2
level 5
#include<iostream>#
include#includeusing namespace std;
class Sign {public: char ch;//声明要输入的键盘上的字符 char cg; friend class DisplayUI;//DisplayUI类能使用id,pass的删除函数 void Input_pass() { //输入密码 while((ch=getchar())!='\n') { password.push_back(ch); }
} void Input_id() { //输入用户名 while((cg=getchar())!='\n') { id.push_back(ch); } } bool Match() { //判断密码是否正确,这里省略 if(password[0]=='r') return true; else return false;
} void txt1() { //文本1 cout<<"请输入您的用户名:"< password; vector id;};
class DisplayUI//显示
{public: int flag;//声明flag,选择1或2
int sigh(int m_flag);
DisplayUI() { Sign test2;//不知道怎么写
} void lll() { //开始界面 cout<<"请选择"<>m_flag;//输入选择,登录或注册,选择之后就把文本1和文本2都输出了,不知道为什么。 test.sigh(m_flag);//执行登录或注册
}
2022年11月04日 15点11分
4
level 5
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class Sign {
public:
char ch;//声明要输入的键盘上的字符
char cg;
friend class DisplayUI;//DisplayUI类能使用id,pass的删除函数
void Input_pass() { //输入密码
while((ch=getchar())!='\n') {
password.push_back(ch);
}
}
void Input_id() { //输入用户名
while((cg=getchar())!='\n') {
id.push_back(ch);
}
}
bool Match() { //判断密码是否正确,这里省略
if(password[0]=='r')
return true;
else
return false;
}
void txt1() { //文本1
cout<<"请输入您的用户名:"<<endl;
}
void txt2() { //文本2
cout<<"请输入您的密码:"<<endl;
}
private:
//声明用户名和密码的字符动态数组
vector<char> password;
vector<char> id;
};
class DisplayUI//显示
{
public:
int flag;//声明flag,选择1或2
int sigh(int m_flag);
DisplayUI()
{
Sign test2;//不知道怎么写
}
void lll() { //开始界面
cout<<"请选择"<<endl<<"1.注册"<<endl<<"2.登录"<<endl;
}
private:
Sign test2;//内嵌对象
};
int DisplayUI::sigh(int m_flag) {
if(m_flag==1) {
system("cls");//清屏
test2.txt1();//显示文本1
test2.Input_id();//执行输入用户名函数
test2.txt2();//显示文本2
test2.Input_pass();//执行输入密码函数
system("cls");//清屏
cout<<"sucess"<<endl;//注册成功
lll();//如何退出?
}
else {
system("cls");//清屏
test2.txt1();//同上
test2.Input_id();
test2.txt2();
test2.Input_pass();
system("cls");//清屏
if(test2.Match())//判断,貌似出了问题
cout<<"success";
else
{
test2.id.clear();//清除用户名数组内容
test2.password.clear();//清除密码数组内容
return sigh(m_flag);
}
}
}
int main()//类和对象学得不好,想借此例来了解一下。
{
int m_flag;
DisplayUI test;//显示对象
test.lll();//显示开始界面
cin>>m_flag;//输入选择,登录或注册,选择之后就把文本1和文本2都输出了,不知道为什么。
test.sigh(m_flag);//执行登录或注册
}
2022年11月04日 15点11分
6
level 7
我已经阅读玩您的代码了,出现了一下几个问题:
1.思路稍微有点混乱,请自行查看源代码,在验证用户时输入的数据是否将原本保存的信息冲掉?是否应当添加新变量?
2.我明白您想使用递归调用完成循环,但是要考虑到程序自身特性,这是一个验证账号密码的程序,且无本地数据库,因此应当在主循环中添加死循环,并且加入退出的判断条件。
(我想楼主原先就是因为每次打开程序所有变量初始化的原因所以无法完成)
3. 尽量将所有同一对象所有能够调用的函数写到同一类中,否则可能会出现public和private的混淆。
4. 所有变量尽可能赋初值。
5.建议改为使用string而非vector。
6.return 0; 没加,很重要,虽然有的时候不影响程序进行。
7.格式问题,好的格式可以清楚的看明白嵌套关系,让代码更以读,这不光是通过自动补全功能,还应自己修改,逐渐形成自己的代码风格。
8. 此段代码还未真正完成楼主所需的功能,大体已经完成修改,可以根据自身要求再优化
后附代码
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class Sign {
public:
char ch;//声明要输入的键盘上的字符
char cg;
friend class DisplayUI;//DisplayUI类能使用id,pass的删除函数
void Input_pass()
{ //输入密码
ch=getchar();
while((ch=getchar())!='\n')
{
password.push_back(ch);
}
}
void Input_id()
{ //输入用户名
cg=getchar();
while((cg=getchar())!='\n')
{
id.push_back(cg);
}
}
void Check_pass()
{ //输入用于验证的密码
ch=getchar();
temp_password.push_back(ch);
while((ch=getchar())!='\n')
{
temp_password.push_back(ch);
}
}
void Check_id()
{ //输入用于验证的用户名
cg=getchar();
temp_id.push_back(cg);
while((cg=getchar())!='\n') //其实用string可以直接cin效率更高,或者用cin>>cg
{
temp_id.push_back(cg);
}
}
bool Match()
{ //判断密码是否正确,这里省略
if(temp_password[1]=='r') return true;
else return false;
}
void txt1()
{ //文本1
cout<<"请输入您的用户名:"<<endl;
}
void txt2() { //文本2
cout<<"请输入您的密码:"<<endl;
}
private:
//声明用户名和密码的字符动态数组
vector<char> password;
vector<char> id;
vector<char> temp_password;//存储用于验证的密码
vector<char> temp_id;// 存储用于验证的id
};
class DisplayUI//显示
{
public:
int flag;//声明flag,选择1或2
void displayUI(int m_flag)
{
if(m_flag==1) {
system("cls");//清屏
test2.txt1();//显示文本1
test2.Input_id();//执行输入用户名函数
test2.txt2();//显示文本2
test2.Input_pass();//执行输入密码函数
system("cls");//清屏
cout<<"sucess"<<endl;//注册成功
}
else if (m_flag==2)//使用else if 否则会出现后面else无法使用的问题
{
system("cls");//清屏
test2.txt1();//同上
test2.Check_id();
test2.txt2();
test2.Check_pass();
system("cls");//清屏
if(test2.Match())//判断,貌似出了问题
cout<<"success";
else cout<<"failed"; //没有输对密码也应当有反馈
}
else
{
test2.id.clear();//清除用户名数组内容
test2.password.clear();//清除密码数组内容
}
}
void lll() { //开始界面
cout<<"请选择"<<endl<<"1.注册"<<endl<<"2.登录"<<endl<<"3.退出"<<endl;
}
private:
Sign test2;//内嵌对象
};
int main()//类和对象学得不好,想借此例来了解一下。
{
int m_flag;
DisplayUI test;//显示对象
while(true){//加入死循环,并添加退出条件
test.lll();//显示开始界面
cin>>m_flag;//输入选择,登录或注册,选择之后就把文本1和文本2都输出了,不知道为什么。
if(m_flag==3)
{
break;
}
test.displayUI(m_flag);//执行登录或注册
}
return 0;
}
2022年11月05日 02点11分
7
好吧,是我错怪你了,没想到贴吧直接删格式
2022年11月05日 02点11分
level 10
但是你对成员变量和局部变量混用了, 接受字符的ch cg这些不该定义在类里面
2022年11月05日 05点11分
11
level 8
你这代码习惯看得我头痛,先不说为啥跑不起来,起码先改得别人能看的下去再说吧。
1. 把实现和申明分开,头文件只写申明,源文件写具体实现;
2. 类里面把function和成员变量分开放,成员变量最好申明成私有,而且用下划线或者其他方式把它们的命名方式和function区分开;
3. 不要乱加空行,记得缩进;
4. 你可以不写注释,但起码把function和变量的名字写完整点,比如match,什么match什么?
2022年11月07日 06点11分
14