[英]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.