Click here to Skip to main content
14,211,651 members
Rate this:
Please Sign up or sign in to vote.
See more:
i want to take in a variable int array..normally i would declare a char array and use gets to input the array but it wont work for int array even though i can use the char array for inputting int but still is there any alternative..thanx

What I have tried:

i have searched internet but couldn't find the answer.
Posted
Updated 12-Mar-17 23:29pm
Comments
Richard MacCutchan 13-Mar-17 4:21am
   
No, there is no equivalent. You would need to create a loop to input values one by one and store them in the array.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

As suggested, your code have to perform a loop.
I would use a vector, instead of an array, e.g.
 #include <iostream>
 #include <vector>
 using namespace std;

int main()
{
  vector<int> v;

  for (;;)
  {
    int i;
    cin >> i;
    if ( cin.eof()) break;
    v.push_back(i);
  }


  for (const auto & x : v)
    cout << x << " ";
  cout << endl;
}
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.190619.2 | Last Updated 13 Mar 2017
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100