C ++输入验证,以避免破坏代码的能力

时间:2015-12-02 23:52:32

标签: c++ validation loops input

我写了一个模拟钓鱼游戏的程序。程序在一个运行循环内,只要用户愿意就可以迭代。问题是输入的任何内容(1或0除外)都会破坏我的代码。几个小时以来我一直在尝试不同的事情,我需要一些帮助!我的主要cpp文件代码包含在内。如果您的评论需要我的标题文件,请告诉我。

我已要求编辑代码。问题是除了整数之外的任何东西在输入时都会破坏我的代码。我不想要这个。我希望程序捕获输入并继续循环,直到用户输入正确的输入(1或0)或退出游戏。

Game game; // Game object game declared
cout<<"Welcome to Go Fish 2.0!"<<endl; // welcome message
cout<<"Would you like to play?"<<endl; // Prompts user to play
cout<<"1 to play, 0 to exit:"<<endl; // Provides user with input choices
cin>>choice; // user enters game play choice

if (choice == 1) // while user chooses to play game
play = true; // play boolean is set to true
else if (choice == 0) // while user chooses to end game
   play = false; // play boolean is false
while ( ! cin>>choice) { // while user entry is not valid
// display correct inpout choices again
    cin.clear();
    cin.ignore (100, '\n');
cout <<"You entered invalid data. Please enter the numerical value 1 if you want to play again or 0 if you dont."<<endl;
cin >> choice; // hopefully user enters valid input

if (choice == 1) { // if user chooses proper input after improper input
play = true;// play boolean is set to true
break;// break
}
}


total=0; // variable total is initialized to 0
while (play == true) { // while play boolean is set to true; user wants to play game

total1 += game.playgame(total); // total1 variable keeps a running total of the game
//game.playgame(total) uses the game object to call the playgame function that passes the variable total
// when game.playgame(total) is called this funciton essentially mobilizes game play of die rolling and point accumulation
//each time this function is called (as many times as user wants to play game), the running total is incremented

cout<< "Do you want to play again? (0 for no, 1 for yes)"<<endl;// asks user if they want to play again
cin >> choice;// user enters choice

if (choice == 1) // if user enters 1
{
play = true; // play is assigned to true
}


else if (choice == 0) // if user enters 0
{
play = false; // play is assigned to false
}
/*  
while ( ! cin>>choice) { // while user entry is not valid
// display correct inpout choices again
    cin.clear();
    cin.ignore (100, '\n');
cout <<"You entered invalid data. Please enter the numerical value 1 if you want to play again or 0 if you dont."<<endl;
cin >> choice; // hopefully user enters valid input

if (choice == 1) { // if user chooses proper input after improper input
play = true;// play boolean is set to true
break;// break
}
}


if (choice == 1) { // if user chooses proper input after improper input
play = true;// play boolean is set to true
break;// break
cout<<"My Total game points are "<<total1<<endl; // displays user's total points from whole game
if (total1>100) // if game total greater than 100
cout<<"Awesome! You're a great fisher!"<<endl; // congratulate user
if (total1<100)// if game total is less than 100
cout<<"Good job, but play again so you can fish some more."<<endl;// chastise user
system("pause");
}

}
return 0;// end main function
}
}

2 个答案:

答案 0 :(得分:1)

你应该首先从用户那里获取输入作为字符串/字符数组,然后尝试将其转换为int(有很多方法可以做到这一点 - Google是你的朋友;))但atoi(ASCII到int )可能是最快/最简单的。如果失败,请要求用户输入有效输入,重复直到得到1或0,等等。

答案 1 :(得分:0)

您希望输入多个地方。我无法理解你的代码。

根据你的描述你只需要一个地方来读取整数:一个循环读取选项并验证接收到正确的输入。那应该是相当直接的......

 for (int choice(0); true; ) {
     std::cout << "some message goes here\n";
     if (std::cin >> choice) {   // successfully read a value
         if (choice == 0) {      // the choice is to stop
             break;
         }
         else if (choice == 1) { // the choice is to carry on
             play_again();
         }
         else {                  // an invalid choice was made
             std::cout << "value " << choice << " is out of range\n"
         }
     }
     else if (std::cin.eof()) {  // input failed because nor more input
         break;
     }
     else {                      // a non-integer was entered
         std::cout << "a non-integer was entered\n";
         std::cin.clear(); // clear error state and ignore current line
         std::cin.ignore(std::numeric_limits<std::streamsize>::max();
     }
}