繁体   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;
}

我想找到使用计数器输入的原始值,例如:

5 6 7 4

原始数组是:

5 6 7 4

如果我的理解是正确的并且您尝试继续计数直到找到用户输入的数字,您可以执行以下循环:

#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++

此代码使用for循环不断计数,直到找到正确的值。 你也可以使用任何循环机制来做同样的事情,比如do while循环。

暂无
暂无

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

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