0

I'm pretty new to c++ and can't seem to find correct way to code this. I have array of n digits, my code now:

int main()
{
    int n,i;
    cin >> n;
    int a[n];
    for (i=1;i<=n;i++)
    {
        cin >> a[i];
    }
    return 0;
}

This way every element of array has to be input in different line, is it possible to put all elements of a array in one line, with space between them.

letsintegreat
  • 3,328
  • 4
  • 18
  • 39
tagilas
  • 65
  • 8
  • You can put in one line with your above code – Yernar Apr 29 '18 at 14:29
  • If you had [a good book (or two)](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) then you would know that array indexes are *zero* based. And that C++ doesn't have [variable-length arrays](https://en.wikipedia.org/wiki/Variable-length_array) (use [`std::vector`](http://en.cppreference.com/w/cpp/container/vector) for that). – Some programmer dude Apr 29 '18 at 14:30
  • As for what you're asking about, then `std::cin` with `>>` reads *white-space delimited* numbers. Newline happens to be a white-space, so do normal space. – Some programmer dude Apr 29 '18 at 14:32
  • thanks @Someprogrammerdude – tagilas Apr 29 '18 at 14:41

4 Answers4

0

I am assuming your question is "what is the correct way to do this?"

I would do it this way:

#include <iostream>
#include <vector>

using std::cin;
using std::cout;
using std::endl;
using std::vector;

int main()
{
  int n;
  cin >> n;
  vector<int> v;
  int i = 0;
  int value;
  while (i++ < n && cin >> value)
  {
    v.push_back(value);
  }

  char const* sep = "";
  for (auto item : v)
  {
    cout << sep << item;
    sep = " ";
  }
  cout << endl;
}

Note that this code is making assumptions that the input is well formed. If you need something that is more robust in handling possibly malicious input, that would require extra effort. This code, as given, will give-up-trying-and-continue which may or may not be suitable for your purposes.

Eljay
  • 4,648
  • 3
  • 16
  • 27
0

The following code snippet of your program is a Variable Length Array(VLA) and this is only supported in C since ISO C99.

 cin >> n;
 int a[n];

And as previously pointed out, you can also use std::vector instead.

int main()
{
  int size;

  std::cin >> size;

  int *array = new int[size];

  delete [] array;

  return 0;
}

References:

http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html

How to create a dynamic array of integers

Clint
  • 6,011
  • 1
  • 21
  • 28
0

Without using stl container , one can implement like so:

#include <iostream>
#include <string>
#include "stdlib.h"     

void GetInput(int* inputs, int n)
{
    // store the entered numbers in a char[]
    std::string word;
    std::cout << "enter numbers (separate by space) ";
    std::getline(std::cin, word);

    char ch[100];
    strcpy_s(ch, word.c_str());
    char *temp = ch;

    // parse the char[] for integers
    for (int i = 0; strcmpi(temp, "") != 0 && i <= n; temp++,i++)   {
        *(inputs +i) = std::strtol(temp, &temp, 10);
    }

} 

int main()
{
    int n = 3;
    int inputs[10];
    GetInput(inputs,n);

    for (int j = 0; j < n; j++)
        std::cout << inputs[j] << " \n";
    return 0;
}

Output:

enter image description here

seccpur
  • 4,996
  • 2
  • 13
  • 21
0

I know this is an old question but I came across a similar thing in a challenge. Here is how I solved it;

#include <cmath> 
#include <cstdio>
#include <vector> 
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    int index_count;


    cin>>index_count;
    int arr[index_count] = {};

    for(int i = 0; i < index_count; i++){
        scanf("%d", &arr[i]);
    }
   
    return 0;
}
Viceodev
  • 31
  • 5