我试图对#34;游戏"进行介绍,并在其功能中我做了一些是/否,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);}
}
}
第一个功能完美无缺,您可以键入&#34; aaa&#34;或者&#34; a a a&#34;并将工作。但是,我想知道是否有更简单的方法。 (初学者可以理解,3天前刚开始大声笑;如果它包含一些高级或不太知名的代码,现在希望保持这样)。
现在,第二个,我真的不知道如何解决它。我需要一些东西,如果用户的输入是无效的字符类型,抛出某个消息,如果它是一个int类型,但超出范围,另一个消息。当然,如果失败则再次运行。经过大量搜索,无法找到符合此要求的任何内容。
答案 0 :(得分:0)
要检查用户输入是否为int,您可以使用good()
函数。
int val;
cin >> val;
if( cin.good() ) {
// user input was a valid int
} else {
// otherwise
}
对于范围检查,语法略有不同。 如果数字不等于1也不是2也不是3:
,则返回trueselectedDif != 1 && selectedDif != 2 && selectedDif != 3
另一种较短的方法是使用:
selectedDif < 1 || selectedDif > 3
另一件事,在c ++中,有两个关键字break
和continue
,它们可以减少循环中的代码。