c++ - 无法设置用户可以输入的数字范围

标签 c++ arrays loops

我正在编写一个程序,除了下面的函数外,一切正常。我很好奇怎样做才能使第一个循环的用户输入只能是 1 - 69,而第二个循环只能是 1 - 26。

我正打算执行一个 do/while 循环,但出现如下所述的错误。

//***********************************************
//Case 4 lets you input your own lottery numbers*
//***********************************************
void case4()
{
    cout << numberprint << endl;
    tickettop();
    int array_pick[4];
    int pballp;
    for (int i = 0; i < 5; i++)
    {
        cout << evalue << i + 1 << space1;
        do 
       {
        cin >> array_pick[i];
        } while (array_pick > 0 && array_pick <= 69); //Here is where I get an error for array_pick <= 69 (operand types are incompatible ( int * and int))
    }
    for (int i = 0; i < 1; i++)
    {
        cout << eball;
        cin >> pballp;
    }
    cout << endl << endl;
    ofs << endl;
    ticketbottom();
    ofs << bar << box << bar << endl;
}

最佳答案

int lottonumber;

ask: //goto label
cin >> lottonumber; //prompt value in console
//if lower than 1 or higher than 69, goto label
if (lottonumber < 1 || lottonumber > 69)goto ask; 

人们不喜欢 goto 标签,但在这种情况下这不是问题。 While 循环和 for 循环的存在是为了使代码更易于理解和组织。 goto 标签不会使您的程序变慢或“变坏”,它只会使它更难组织。或者在这种情况下,更容易。

关于c++ - 无法设置用户可以输入的数字范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39984034/

相关文章:

c++ - 使用 '\uxxxx' 格式定义的字符显示错误的字符

c++ - 相同的内容,不同的 MD5 - 文件和字符串

javascript - 访问 json 数组时在 javascript 中未定义

c - 在我的 C 程序中,为什么我的跟踪循环和实际输出不同?

java - 有没有办法输入一个unicode值并输出其各自的字符?

c++ - pragma comment(lib) 不起作用

c++ - 重载 = 运算符

java - 尝试理解 true/false 格式的 set 方法的数据验证

javascript - 如何从数组中删除所有不具有特定属性的对象?

java - 为什么我的 arraylist 的一个迭代工作,而另一个不工作?