2017-04-05 64 views
0

我试着对“游戏”做一个介绍,在其功能中我做了一些是/否,1/2/3的情况。 即时通讯新的但它并不那么困难,完美工作。处理无效输入时出现问题。所以这是代码看起来像现在:安全[是/否]; [1/2/3/etc。]功能

#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);} 
    } 

} 

第一个功能完美的作品你可以输入“AAA”或“A A A”,并将努力。不过,我想知道是否有一个更简单的方法来做到这一点。 (对于初学者来说,可以理解,3天前刚开始lol;如果它包含一些先进的或不太知名的代码,现在就更喜欢这样)。

现在,第二个,我真的不知道如何解决它。我需要的东西,如果用户的输入是一个无效的字符类型,抛出某些消息,如果它是一个int类型,但超出范围,另一个消息。当然,如果失败,可以再次运行。做了很多搜索,找不到符合这个要求的任何东西。

+0

一些建设性的批评 1.你正在检查确认之前,它甚至被初始化这是不好的做法。考虑一个do-while循环,而不是while循环,以便在检查确认的值之前允许进行名称检查。 2.您的if-blocks的缩进程度很差。这也是“开关”控制结构的主要候选者。 3. PC是全球性的吗?它在哪里定义? 4.第二个函数到底有多失败?编译/执行时会发生什么? 5.另外,你不能在C++中使用(selectedDiff!= 1 && 2 && 3)。您必须分别与每个值进行比较。 –

回答

0

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

int val; 
cin >> val; 

if(cin.good()) { 
    // user input was a valid int 
} else { 
    // otherwise 
} 

至于范围检查,语法有点不同。

selectedDif != 1 && selectedDif != 2 && selectedDif != 3 

另一个短的方式将是使用::

selectedDif < 1 || selectedDif > 3 

另一件事,在C++中,有两个关键字break 此如果该号码不等于1也不2也不3返回true和continue这将允许减少循环中的代码。