Reputation: 1
I want to input any number into array b[]
by number of numCase
times.
#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.
}
}
Upvotes: 0
Views: 95
Reputation: 165
If you want to display all the numbers stored in array b[] then you may write your code as
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];}}
Upvotes: 0
Reputation: 157
I think this is what you might be looking for:
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];
}
}
Hope it helps
Upvotes: 0
Reputation: 1361
cout << b[c] ; // in this statement c already reached to numCase where you have not assigned any value
Upvotes: 0
Reputation: 281855
You're filling entries 0
toN
of b
, and then printing entry N+1
, which you haven't filled in.
Upvotes: 2
Reputation: 17288
The variable c
should be initialised back to zero.
} while (c != numCase);
c = 0;
Upvotes: 1