一些强制用户输入选择的代码

Some code to force a user to enter a choice

本文关键字:选择 代码 输入 用户      更新时间:2023-10-16

我对c++很陌生,我需要一些代码来强迫用户做出选择。第一个选择回调主函数,第二个选择调用return 0,否则程序在任何选择时都不会在主函数中调用return 0。选择Yy表示"是",nN表示"否",除了这些字母外,代码不接受任何字母。如果您能帮助我,我将不胜感激,谢谢。

    #include "person. h"
    #include <iostream>
    #include <string>
    using namespace std; 
    int main()
    {
person persons; 
persons. getInfo();
persons.showInfo();
cout << "do you want to continue, type Y or y for yes and n or N for no to exit" << endl; 
char choice; 
cin >> choice; 
if (choice == ("Y"||"y"))
{
int main();
}
if (choice == ("N"||"n"))
{
return 0;
}
else
{
 cout << "please make a choice"; cin >> choice; 
 } /* This is where I would like to iterate in other for the     user to make one of the choices provided above */

return 0;
 }

你可以这样写:

if(choice == "Y" || choice == "y")
    int main();
else
    return 0;

所以他们必须输入Y或Y来继续,然后其他任何东西都会用return 0;

终止程序