初学C++之登录界面

292 阅读1分钟
原文链接: www.jianshu.com

语言:C++ 知识:逻辑符,if,else用法,控制台颜色修改

逻辑符:&&,||,!
&&: 同时满足
||:只需一个满足
!:与条件相反

if (用户名 == "XXX"&&密码 == "1234567890")
    {
        cout << "\n***欢迎登陆,祝你游戏愉快!***\n";
        success = true;
    }

if,else用法 进行判断,与C语言一样

if (用户名 == "XXX"&&密码 == "1234567890")
    {
        cout << "\n***欢迎登陆,祝你游戏愉快!***\n";
        success = true;
    }
    else
    {
        cout << "\n***用户名或密码错误!***";
        success = false;
    }

控制台颜色
头文件:#include<stdlib.h>
选择颜色,用system("color 0A");
其中color后面的0是背景色代号,A是前景色代号。各颜色代码如下:
0=黑色
1=蓝色
2=绿色
3=湖蓝色
4=红色
5=紫色
6=黄色
7=白色
8=灰色
9=淡蓝色
A=淡绿色
B=淡浅绿色
C=淡红色
D=淡紫色
E=淡黄色
F=亮白色
完整代码如下
// 游戏登录界面.cpp: 定义控制台应用程序的入口点。
//

#include"stdafx.h"
#include <iostream>
#include<string>
#include<stdlib.h>
using namespace std;
int main()
 {
system("color 3f");
string 用户名;
string 密码;
bool success;
cout << "\t游戏登陆界面\n";
do
{
    cout << "\n用户名:   ";
    cin >> 用户名;

    cout << "\n密码:   ";
    cin >> 密码;

    if (用户名 == "XXX"&&密码 == "1234567890")
    {
        cout << "\n***欢迎登陆,祝你游戏愉快!***\n";
        success = true;
    }
    else
    {
        cout << "\n***用户名或密码错误!***";
        success = false;
    }
} while (!success);
return 0;
  }

效果如图


图片.png