简体   繁体   English

我想使用计数器来查找用户输入的原始号码,但我不知道如何

[英]I want to use a counter to find the original number that the user entered, but I can't figure out how

#include <iostream>
using namespace std;

const int MAX_SIZE  =100;
int myArray [MAX_SIZE] = {};

int main(){
    int userInfo;
    cout << " enter a non 0 integer value" << endl;
    cin >> userInfo;
    myArray[0] = userInfo;
     
    int count;

    return 0;
}

I want to find the original value that was entered using a counter, ex:我想找到使用计数器输入的原始值,例如:

5 6 7 4

The original array is:原始数组是:

5 6 7 4

If my understanding is correct and you are trying to keep counting until you find the number entered by the user, you can do the following loop:如果我的理解是正确的并且您尝试继续计数直到找到用户输入的数字,您可以执行以下循环:

#include <iostream>
using namespace std;

const int max = 100 //max value
int x;
cout << "Enter a positive value: ";
cin >> x;
for (int counter = 0; counter < max; counter++)
{
    if (counter == x)
    {
        cout << "The value you entered is: " << counter << endl;
        break; //exit the loop
    }
}
//you no longer need to use "return (0);" in current versions of c++

this code uses a for loop to keep counting till it finds the right value.此代码使用for循环不断计数,直到找到正确的值。 You can also do the same thing using any looping mechanism like do while loops.你也可以使用任何循环机制来做同样的事情,比如do while循环。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM