0

I have a simple question. I am trying to store a list of numbers into an array using a while loop.

For example, let's say the size of the array is 5.

If I input: 1 2 3 4 5 and press enter, there won't be any problem

However, if the size of the array is 10 and I input: 1 2 3 4 5 6 7 8 9 10 then it doesn't work and skip the lines afterwards.

I searched but can't find any answers. Is it just not possible to enter too long a list of numbers in one line separated by spaces using cin? Do I have to do it like 1 [enter] 2 [enter]...10 [enter]?

Any help is appreciated.

    int n=1,key,i; 
    int arra [n];


     cout << "Please enter the size of array (no more than 100): ";
     cin >> n;
     while (n>100)
     {
         cout << "Please enter a number no more than 100: ";
         cin >> n;
     }

     cout << "Please enter " << n << " numbers separated by a space and ended with Enter key: \n";

     for (i=0;i<n;i++) // store numbers into array

         cin >> arra[i];

     cout << "Please enter the key number that you want to find out: ";
     cin >> key;

     if (search(arra,n,key)==1)
        cout << "Yes, the key is in the array. \n";
     else
        cout << "No, the key is not in the array. \n";
Arvin Wong
  • 133
  • 1
  • 1
  • 9
  • i think you should look at your array size.. n is 1 when u r declaring array ,I m suspicious its even storing 5 elements like you said. – Hummingbird Mar 18 '16 at 05:17
  • @Proyyadeep Moulik, yes exactly.. i wanted him to write that himself though.. :P – Hummingbird Mar 18 '16 at 05:27
  • This is no way right as far as I know. Your array is declared on the stack and that means it cannot be resized by changing the array size directly like that. You should either create the array on the heap or better go with a vector if stack is your preferred location. – ubuntugod Mar 18 '16 at 05:28
  • Just use a `std::vector`. If you are using C++, why not use the features it provides? – Nacho Mar 18 '16 at 05:29

3 Answers3

1

The fault is, you assign n's value to the array's size before taking the input.

int n=1;
int arra[n];
//arra's size is 1

You should assign the size after taking the input.

while(n>100){
    cout <<"Enter a number less than 100\n";
    cin >> n;
}
//now declare the array
int arra[n];

So now, arra[] has the size entered by user.

  • Did you ran this code i don't think it's going to work. Array size cannot be changed in runtime. – Amit Hasan Mar 18 '16 at 05:34
  • @AmitHasan Yes sir I did run the code and it worked perfectly :) –  Mar 18 '16 at 05:36
  • @ProgyadeepMoulik I see. But believe me this not standard behavior. The only correct way of doing this is dynamic memory allocation. – Amit Hasan Mar 18 '16 at 05:39
  • @AmitHasan It may surely be sir. But as Nacho said: it's allowed by the compiler. I'm yet to learn a lot about C++ and maybe when I do, I can say whether it should be encouraged or not. :-) And thank you for pointing out this fact too. –  Mar 18 '16 at 05:51
  • 1
    This may work since VLA (Variable Length Arrays) are supported from C99. But note that it is never a recommended way. Although there was another meaningful way to solve the issue by using `alloca`, I didn't suggest it because it is not protable I believe. The standards comittee said `type semantics of C variable-length arrays are probably not acceptable to C++` : [VLA Proposal](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2648.html) – ubuntugod Mar 18 '16 at 06:11
  • 1
    You’re right. C++ gives a lot of freedom in terms of memory access. Not doing in the right way may sometime be dangerous. If you do a google search “set array size at runtime in c++” you will see all solution uses dynamic memory allocation. I just want you to be aware of this. Best of luck. – Amit Hasan Mar 18 '16 at 06:18
  • in c++ : 1)malloc becomes new 2) realloc becomes vector(push back if you don't know the number of input) – Akash Sinha Mar 18 '16 at 09:16
  • Using runtime sizes for C-style arrays is **not** part of C++. C does it, but it's been explicitly rejected for C++, so this solution depends on a particular family of compilers and is not portable. – Pete Becker Mar 18 '16 at 14:58
0

Array length should always be constant. int arra[n] won’t work because n is a variable. Set int arra[50] or something based on your requirement. Setting a bit higher than necessary is ok. However if you want an array of size that can be set in run time requires dynamic memory allocation. If you are interested in dynamic memory allocation then you have to learn new and delete of c++.

Amit Hasan
  • 1,430
  • 1
  • 15
  • 33
  • 1
    @ProgyadeepMoulik He is right, but [sometimes it is allowed by the compiler](http://stackoverflow.com/q/15013077/5583153). That's why you could compile and run your code. – Nacho Mar 18 '16 at 05:39
  • 1
    @Nacho but should it be encouraged? This could create greater problem for him later. – Amit Hasan Mar 18 '16 at 05:42
  • 1
    Indeed it should not, that's why i recommended `std::vector` in my comment to OP question. – Nacho Mar 18 '16 at 14:02
0

you are allocating memory to the array statically. try to use vector :

vector<int> arra;

and while entering the values, simply use in build function :

int input;
cin >> input;
arra.push_back(input);

this way, you won't have to set a limit of 100 as well since it dynamically allocates memory for each new input.