【问题标题】:C++ Input Validation to avoid ability to break codeC++ 输入验证以避免破坏代码的能力
【发布时间】:2016-03-07 10:33:40
【问题描述】:

我编写了一个模拟钓鱼游戏的程序。该程序在一个运行循环中,只要用户愿意,就可以进行迭代。问题是输入的任何内容,除了 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
}
}

【问题讨论】:

  • 请多多关照 - 格式化您的代码...
  • 请看std::uniform_int_distribution,而不是rand。它将为您提供更好的随机性并预先加载所有使用困难。 (或者用英语,更难设置,但它就是一劳永逸)
  • 上次编辑让事情变得更糟。不要再乱用这段代码,写一个简单的小程序,除了从用户那里读取 1 或 0 之外什么都不做。玩一会儿。如果你不能让它发挥作用,你可以提出一个重点突出、完整的问题。
  • 请使用类似这样的方式来格式化代码:- prettyprinter.de 。很多人很难遵循代码。如果您正确缩进代码,您将获得更好的响应。

标签: c++ validation loops input


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • atoi 并不比cin 直接发送到int 好。这是正确的想法,但您希望 strtolstd::stoi 确保用户输入 1 或 0 而没有其他内容。
    猜你喜欢
    • 1970-01-01
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-07
    • 2021-03-16
    相关资源
    最近更新 更多