简体   繁体   中英

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. You can also do the same thing using any looping mechanism like do while loops.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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