简体   繁体   English

如何使用递归来循环我的代码(当用户输入无效输入时,它会再次提示它们)?

[英]How do I use recursion to loop my code (when user enters invalid input, it prompts them again)?

So I wrote this code in C++ format and I am trying to loop the code as to when the user enters an invalid input when prompted to enter a number between 1 and 10 it doesn't just terminate if invalid input, but asks the user to enter a valid input until correct a valid input is entered. 所以我用C ++格式编写了这段代码,我试图循环代码,当用户输入无效输入时,当提示输入1到10之间的数字时,它不会在无效输入时终止,而是要求用户输入有效输入,直到输入有效输入。 New to c++ so I'd appreciate the help c ++的新手,所以我很感激帮助

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cctype>

using namespace std;

//constants go here
const int MIN = 1;
const int MAX = 10;

const char YES = 'Y';
const char NO = 'N';

//Booleans go here
char wantToPlay();
bool getIntValue(int &userValue);
bool play(int userValue);

//Main routine
int main(){

printf("Welcome, this program will guess a number between 1 and 10");
printf(", if the program guesses the number correctly you'll get VICTORY ROYALE");
printf(", if you don't get it right you'll get BETTER LUCK NEXT TIME, Good luck :)\n");

char playOrNot = wantToPlay();

  //If user value is TRUE, program outputs "Victory Royale" and terminates
  //If user value is FALSE, program outputs "Better luck next time" and terminates
  //If user value is NOT VALID, program outputs "Not a good number" and prompts user again
  //If user enters "N" when prompted to answer Y or N
  int input = -1;
  switch (playOrNot){
  case YES:
    if(getIntValue(input)){
      srand (time(NULL));
      if(play(input)){
    cout << "Victory Royale!"<<endl;
    }else{
    cout<<"Better Luck Next Time!"<<endl;
    }
    }else{
      cout<<"not a good number\n";
    }
    break;
  case NO:
    cout << "sorry you hate my game\n";
    break;

  default:
    cout << "that was not valid\n";
    //If user enters value that is completely not valid
  }


  return 0;
}
char wantToPlay(){
  //Prompt user to enter Y for yes and N for no
  char answer = NO;
  cout << "Do you Want to Play? 'y'for" << " yes, 'n' for no" << endl;
  cin >> answer;
  answer = toupper(answer);
  return answer;
}


bool getIntValue(int &userValue){
  //Prompt user to enter a number between the Min(1) and Max(10)
  bool valid = false;
  cout <<"Enter a number between " << MIN << " and " << MAX <<endl;
  cin>>userValue;
  if(userValue >= MIN && userValue <= MAX){
    valid = true;
  }


  return valid;
}
bool play(int userValue){
  //Random tool to give a random between 1 and 10
  bool match = false;
  int random = (rand()%10)+1;
  if(userValue==random){
    match=true;
  }
  return match;
}
#include<iostream>
using namespace std;
int main()
{
    char s;
    cout<<"Enter valid number between 1 to 10"<<endl;
    cin>>s;
    while(!((s>='1') && (s<='10')))
    {
        cout<<"The number you have entered is not in range 1 - 10"<<endl;
        cout<<"Enter an valid number I.e. between 1 to 10"<<endl;
        cin>>s;
    }
    cout<<"You have successfully entered an valid number"<<endl;
    return;
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我请求号码但用户输入非号码时,如何防止失控的输入循环? - How do I prevent a runaway input loop when I request a number but the user enters a non-number? 如何让我的 checkLength 函数循环直到用户输入超过 5 个字符的密码? - How do I get my checkLength function to loop until the user enters in a password of more than 5 characters? 如何循环我的程序以不断询问字母,直到用户输入 Q 结束程序 - How do I loop my program to continuously ask for a letter until the user enters a Q to end the program 如何使用递归来撤销用户输入? - How can I use recursion to reverse the user input? 如果用户在结构中输入某个单词,如何从循环中中断? C ++ - How do I break from a loop if a user enters a certain word into a structure? C++ 我如何获得一个C ++程序来计算用户输入时的单词数? - How do I get a C++ program to count the number of words as a user enters input? 如何读取输入(数字)并在用户在数字末尾输入“0”时终止 - How to read input (a number) and terminate when the user enters '0' at the end of the number 如何在这个程序中添加一个循环以确保用户输入正数,如果负数让他们重新输入? - How to add a loop to this program to ensure user enters positive number, if negative make them retype it? 我如何让我的代码的一部分循环并重复一个问题,直到答案是有效的输入C ++ - How do I get part of my code to loop and repeat a question until the answer is a valid input C++ 如何在用户输入^ X之前读取输入 - How to read input until the user enters ^X
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM