安全 [是/否];[1/2/3/等]功能
Safe [Y/N]; [1/2/3/etc.] function
我试图介绍一个"游戏",在它的函数中,我做了一些是/否,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++ 中,有两个关键字 break
和 continue
可以减少循环中的代码。
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 从不同线程使用int64的不同字节安全吗
- XLL功能的线程安全
- 如何实现功能线程安全
- 安全 [是/否];[1/2/3/等]功能
- 在称为函数内更改功能指针(STD ::函数)安全吗?
- 功能的向后兼容性和安全版本
- 如何分析、查找和修复C++功能中的安全漏洞?
- Visual Studio 2015 中的安全功能
- 致电openssl beio/ec_key/evp_key/.._ nullptr上的免费功能是安全的吗?
- 通过网络将指针发送到静态功能是安全的吗?
- 使功能异常安全
- 如何使用 Windows 本机 Wifi 功能连接到请求网络安全密钥的 Wifi
- boost套接字读写功能是否线程安全
- 使用 C++14 的自动功能类型返回扣除代替 std::common_type 是否始终安全?
- 如何摆脱不安全的功能(sprintf,..)
- 功能与安全/静态与动态实例化
- 洛基的长寿功能对我来说似乎不安全,是这样吗?
- 向下投射具有附加功能的派生类的共享指针-这样安全吗
- 朋友功能的安全风险是什么