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

Safe [Y/N]; [1/2/3/etc.] function

本文关键字:功能 安全      更新时间:2023-10-16

我试图介绍一个"游戏",在它的函数中,我做了一些是/否,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= Easyn2= Normaln3= 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);}
    }
}

第一个函数完美运行,您可以键入"aaa"或"a a a"并正常工作。但是,我想知道是否有更简单的方法可以做到这一点。(对于初学者来说可以理解,3 天前刚开始,哈哈;如果它包含一些高级或鲜为人知的代码,现在更喜欢保持这种状态(。

现在,第二个,我真的不知道如何解决它。我需要一些东西,如果用户的输入是无效的字符类型,抛出某些消息,如果它是 int 类型,但超出范围,则另一条消息。当然,如果失败,请再次运行。进行了大量搜索,找不到满足此要求的任何内容。

要检查用户输入是否为 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

另一件事,在 c++ 中,有两个关键字 breakcontinue 可以减少循环中的代码。