Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 我希望确保用户在每次被要求输入值时都输入唯一的值 cout_Loops_For Loop_If Statement_Input - Fatal编程技术网

Loops 我希望确保用户在每次被要求输入值时都输入唯一的值 cout

Loops 我希望确保用户在每次被要求输入值时都输入唯一的值 cout,loops,for-loop,if-statement,input,Loops,For Loop,If Statement,Input,要完成很多事情。do-while循环在您的场景中似乎很有希望,因为您必须不断获取输入,直到您拥有8个介于0到8之间的唯一数字 步骤/算法: 为索引分配所需的内存,该索引是int[],现在就说int index[8] 有一个变量来存储成功插入到数组索引中的数值的计数器 运行do while循环,其中while条件是计数器>临时输入 检查给定输入是否是“代码> > 0”,一个天真的解决方案是从开始时扫描索引>代码>数组,直到每个输入之后计数“代码> i>代码>,并将新值与数组中的每个值进行比较,然后

要完成很多事情。
do-while
循环在您的场景中似乎很有希望,因为您必须不断获取输入,直到您拥有8个介于0到8之间的唯一数字

步骤/算法:
  • 索引
    分配所需的内存,该索引是
    int[]
    ,现在就说
    int index[8]

  • 有一个变量来存储成功插入到数组
    索引中的数值的计数器

  • 运行
    do while
    循环,其中while条件是
    计数器<8
  • do
    块中,分配一个临时
    int
    来存储用户的输入。让我们称之为
    int临时输入;cin>>临时输入

  • 检查给定输入是否是“代码> > 0”,一个天真的解决方案是从开始时扫描<代码>索引>代码>数组,直到每个输入之后计数“代码> i>代码>,并将新值与数组中的每个值进行比较,然后在C++中如何才能做到这一点?请检查下面的答案。这里有一个
    cout<<"Enter numbers from 0 to 8 in any order"<<endl;
    
    for(int i=0;i<=8;i++){
        cout<< "Input "<<i<<endl;
        cin>>index[i];
     }
    
    // arr : integer array of the values
    // l   : current length of the integer array
    // n   : the number that needs to be checked for existence
    int check_input(int *arr, int l, int n) {
        for (int i=0; i<l ; i++) {
            if (arr[i] == n) {
                // if number already exists; return true (1)
                return 1;
            }
        }
        return 0;
    }
    
    // Main
    int main() {
        cout << "Enter numbers from 0 to 8 in any order"<<endl;
        // Initialize required variables to store the inputs and counter.
        int index[8];
        int i=0;
        // Repeat until the size is 8. (0-7 = 8 values)
        do {
            int temporary_input;
            cin>>temporary_input;
            // Check if input is less than equal to 8 or greater than equal to 1
            if (temporary_input >= 1 && temporary_input <= 8) {
                // Check if the number given as input already exists
                if (check_input(index, i, temporary_input) == 0) {
                    // if number doesn't exist, assign it to index
                    index[i] = temporary_input;
                    i++;
                }   
            }
        } while (i < 8);
    
        // Print values
        for (int i=0; i<8; i++) {
            cout<<index[i]<<" ";
        }
        return 0;
    }
    
    2 2 1 8 3 4 7 7 5 6 5 4
    
    Enter numbers from 0 to 8 in any order
    2 1 8 3 4 7 5 6