C++ 安全[是/否];[1/2/3/等]功能

C++ 安全[是/否];[1/2/3/等]功能,c++,input,types,while-loop,comparison,C++,Input,Types,While Loop,Comparison,我试图对“游戏”做一个介绍,在它的功能中我做了一些是/否,1/2/3的情况。 我对这一点还不熟悉,但这并没有那么困难,效果很好。处理无效输入时出现问题。这就是代码现在的样子: #include "Introduction.h" #include "GameConstants.h" #include "PlayerCharacter.h" #include <iostream> #include <windows.h> using namespace std; Intr

我试图对“游戏”做一个介绍,在它的功能中我做了一些是/否,1/2/3的情况。 我对这一点还不熟悉,但这并没有那么困难,效果很好。处理无效输入时出现问题。这就是代码现在的样子:

#include "Introduction.h"
#include "GameConstants.h"
#include "PlayerCharacter.h"
#include <iostream>
#include <windows.h>

using namespace std;

Introduction::Introduction()
{

}

/////////Function N.1///////////
void Introduction::presentation()
{
    char confirm;
    string enteredName;

    cout << constants.line() << "Welcome traveler! What is the name?" << endl;
    getline(cin,enteredName);// Gets the WHOLE LINE as the name.

    while (confirm != 'Y') //If the player doesn't confirm the name with 'Y' in will run again until it does.
    {
        cout << constants.xline() << "Your name is " << enteredName << " right? (Y/N)" << endl;
        cin >> confirm; //The player's answer
        cin.sync(); //Only takes the first character
        confirm = toupper(confirm); //Turns player message into CAPS for easier detection in the "if" statements

        if (confirm == 'N'){ //If not the correct name, gives another chance
            cout << constants.xline() << "Please, tell me your name again..." << endl;
            cin >> enteredName;
            cin.sync();}

        if ((confirm != 'Y')&&(confirm != 'N')){ //If an invalid input is entered, gives another chance. And insults you.
            cout << constants.xline() << "Fool Go ahead, just enter your name again." << endl;
            cin >> enteredName;
            cin.sync();}
        }

    if (confirm == 'Y'){ //When the answer is yes ('Y') /* Uneeded line */
        PC.setName(enteredName); //Saves the name
        cout << constants.xline() << "Excellent! I have a few more questions for you " << PC.name() << "..." << endl;
    }
}


//////////Function N.2///////////
void Introduction::difSelection(){
    int selectedDif = 0; //Variable to store selected difficulty whitin this function.

    Sleep(2500);

    cout << constants.xline() << "What kind of adventure do you want to take part in?" << endl;

    Sleep(2500); //Wait 2,5 s

    cout << "\n1= Easy\n2= Normal\n3= Hard" << endl;

    while(selectedDif != 1&&2&&3){ //Selected option must be 1/2/3 or will run again
        cin >> selectedDif; //Sets the user selected difficulty
        cin.sync(); //Gets only first character 
        if((selectedDif != 1||2||3)&&(!(selectedDif))){ //If the input isn't 1/2/3 AND is an invalid character, this will run. And it'll start again

            cout << constants.xline() << "Criminal scum. Go again." << endl;
            cin.clear();
            cin.ignore();
        }

        if(selectedDif != 1&&2&&3){ //If selected option isn't 1/2/3, this will run and will loop again. However I know this conflicts with the previous statement since this will run anyways.

        cout << constants.xline() << "Wrong input, please try again." << endl;
        } 
        else if(selectedDif == 1){
        constants.setDiff(1);
        constants.setStatPoints(15);
        } else if(selectedDif == 2){
        constants.setDiff(2);
        constants.setStatPoints(10);
        } else if (selectedDif == 3){
        constants.setDiff(3);
        constants.setStatPoints(5);}
    }

}
#包括“简介.h”
#包括“GameConstants.h”
#包括“PlayerCharacter.h”
#包括
#包括
使用名称空间std;
引言::引言()
{
}
/////////功能N.1///////////
无效介绍::演示文稿()
{
字符确认;
字符串输入名称;

cout要检查用户输入是否为int,可以使用
good()
函数

int val;
cin >> val;

if( cin.good() ) {
    // user input was a valid int
} else {
    // otherwise
}
至于范围检查,语法有点不同。 如果数字不等于1、2或3,则返回true:

selectedDif != 1 && selectedDif != 2 && selectedDif != 3
另一个较短的方法是使用:

selectedDif < 1 || selectedDif > 3
selectedDif<1 | | selectedDif>3

另外,在C++中,有两个关键字:“代码>破译< /代码>和<代码>继续<代码>,这将允许在循环中减少代码。< /p>一些建设性的批评1。在初始化之前,您正在检查确认。这是一个坏的实践。考虑一个while循环,而不是一个while循环,允许名字检查在BeFO之后发生。重新检查确认2的值。您的if块缩进不良。这也是“开关”控制结构的主要候选。3.PC是全局的吗?它在哪里定义?4.第二个函数到底有多失败?编译/执行时会发生什么情况?5.此外,如果(selectedDiff!=1&&2&&3)您也不能执行在C++中,你必须分别对每个值进行比较。