0

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.
}

}

5 Answers 5

2

You're filling entries 0 toN of b, and then printing entry N+1, which you haven't filled in.

Sign up to request clarification or add additional context in comments.

2 Comments

what changes should i make?
Print entry of index one less than when printing b[c]
1

The variable c should be initialised back to zero.

} while (c != numCase);

c = 0;

6 Comments

I tried your suggestion i initialized c to 0 after the while loop but when i cout b[c] all i got was the first input. say i wanted 2 inputs ex. 20 and 700 my cout only displayed 20.
Yes - best use a for loop and increment a counter to display all array elements.
should i want to use if statements i.e. if ( b[counter] <101) and my inputs are 20 and 700. how do i make sure that 700 doesn't get inside that statement? i've tried it 700 does get inside
if (b[counter] <= 700) ?
the problem is that 700 is not less than 101 already, but since b[counter] also contains 20 it also runs 700 inside the statement, what can i do to make sure that doesn't happened?
|
0

cout << b[c] ; // in this statement c already reached to numCase where you have not assigned any value

Comments

0

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

Comments

0

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];}}

2 Comments

i've tried the for loop and it does display all my inputs. but should i want to use if statements i.e. if ( b[counter] <101) and my inputs are 20 and 700. how do i make sure that 700 doesn't get inside that statement? i've tried it 700 does get inside
I've edited my answer. I hope you might want to do this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.