我想将任意数字输入数组b[]
,数量为numCase
次。
#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.
}
}
答案 0 :(得分:2)
您正在填充0
N
的{{1}}条目,然后打印您尚未填写的条目b
。
答案 1 :(得分:1)
变量c
应该初始化为零。
} while (c != numCase);
c = 0;
答案 2 :(得分:0)
cout&lt;&lt; b [c]; //在此语句中c已经到达numCase,你没有分配任何值
答案 3 :(得分:0)
如果要显示存储在数组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];}}
答案 4 :(得分:0)
我认为这是你可能正在寻找的东西:
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];
}
}
希望有所帮助