为什么我的数组不存储任何输入值?

Why doesn't my array store any input values?

本文关键字:输入 任何 存储 我的 数组 为什么      更新时间:2023-10-16

我想按numCase的次数将任意数字输入到数组b[]中。

#include <iostream>
using namespace std;
//entry point
int main()
{

//Declarations
int b[20]; // array size 20 ( limit of inputs)
int c = 0;
int numCase;
int input;


 cout << "ENTER NUMBER OF CASES (MAXIMUM NUMBER OF 20):  n";
 cin >> numCase;

//checks that numCase is less than or equal to (20) and does not exceed
if (numCase < 21)
        {

// gets input number based on the numCase
do
{

cout << "ENTER A NUMBER (MAXIMUM OF 5 DIGITS): n";
                cin >> input;
                cout << "n";
                b[c] = input;
                c++;

} while (c != numCase);

cout << b[c] ;  //  this is my problem it OUTPUTS RANDOM VALUE, 
     //but i can  see on my watch list that b has the values of my input.
}
}

您正在填写b0N条目,然后打印未填写的N+1条目

变量c应该初始化为零。

} while (c != numCase);
c = 0;

count

我想这就是你要找的:

for (int i=0; i<numCase; i++)
{
    if(b[i] >= x) //x is a variable that u can set as a limit. eg. 700
    {
        cout<<"n"<<b[i];
    }
}

希望有所帮助

如果你想显示数组b中存储的所有数字[]然后你可以把你的代码写成

for(int i=0;i<=20;i++)
{ if(b[i]<101)   //This will exclude all the values which are greater than 101
   {cout<<"n"<<b[i];}}
相关文章: