0

I've newly started learning C++ and am stuck with this problem. I need to insert a (user inputted) number of elements in a single line with space separation. If the number of elements was known, I could just write cin >> var1 >> var2 >> ... >> varN;. But how do I do it with any number of elements (loop maybe)?

This is what I'm trying to do:

#include<bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;
    int arr[n];
    for (int i=0; i<n; i++) {
        //stuck here
    }
}

I could have written cin >> arr[i]; and proceeded, but that would require the user to press enter after every input, which I cannot do due to the question's restrictions. How do I write the code so that all my input for array elements can be given in a single line?

PS: I've seen several similar questions already answered on the site, but most of them involve implementations using vectors or are beyond my current level of understanding. A simpler solution will be appreciated.

RiverX15
  • 111
  • 4
  • 1
    So, you want each `arr[i]` to be read RIGHT AFTER the user type the space after? That is a bit strange. Does it make a difference in the final goal you want to achieve? Can you elaborate what are you doing on top of this (if possible)? – hackinghorn Nov 30 '22 at 08:59
  • 1
    Elements are space separated, so they will all be read after pressing Enter – hackinghorn Nov 30 '22 at 09:01
  • there is no language called C/C++. If you want to learn C++ you should read this [Why should I not `#include `?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and maybe this [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). And note that C and C++ are two different languages. – 463035818_is_not_an_ai Nov 30 '22 at 09:06
  • 2
    *'but that would require the user to press enter after every input'* That's completely incorrect, where did you get that idea? – john Nov 30 '22 at 09:07
  • 1
    VLAs are not part of the C++ standard and therefore should not be used. You're incorrect about the behaviour you described though, see https://godbolt.org/z/WqbfrnrMd – fabian Nov 30 '22 at 09:09
  • indeed, forgot this one [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – 463035818_is_not_an_ai Nov 30 '22 at 09:13

2 Answers2

4

cin>>arr[i] does not require the user to press enter after every input. You just need to give whitespace between the integer inputs. It will scan the array normally.

  • 1
    This is correct! Just put `cin >> arr[i];` in and test – hackinghorn Nov 30 '22 at 09:02
  • Ah, yes it works perfectly! I thought `cin` has newline as default separator for each input and thought there exists some alternative to change it to a whitespace. – RiverX15 Nov 30 '22 at 09:07
  • `cin` is a stream, there's no concept of a separator. It's the input operators and functions which might (or might not) define separators. – john Nov 30 '22 at 09:08
0

You would probably want to replace your array with a std::vector to make it resizable in run time. If you do like in your example the program will not compile because n is not known when the program is built.

Here is a simple example

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main() {
    int n = 0;
    cin >> n;
    vector<string> arr; // A vector (ie kind of a variable sized array)
    for (int i=0; i<n; i++) {
        string str;
        cin >> str; // Read a word
        arr.push_back(str); // Add a string to the vector
    }

    // To use the data
    for (int i = 0; i < arr.size(); ++i) {
        cout << arr.at(i) << "\n"; // Print value of vector
    }
    // or like this
    for (auto str: arr) {
        cout << str << "\n";
    }
}
Lasersköld
  • 2,028
  • 14
  • 20