0

I came from C so I'm trying to understand C++ input.

I want to read lines of input like so:

3
0 0 1 0
1 4 0 1
0 1 2 3

where 3 is the number of input lines I'm going to read next. My code is

  #include<iostream>
  #include<string>
   
  int main() {
    int n, k = 8;
    std::cin >> n;
    
   char lines[n][k];
   for(int i = 0; i < n; i++) 
     std::cin.getline(lines[i], k);
 } 

I should be able to read the n lines (because of the zeroth iteration) but it only goes for n-1, so it finishes the reading at n-2. I don't understand why and how to deal with it and properly read input like this.

Also: how would I do if I wanted to read the same way for a std::string? Would I use std::string *lines[n];?

beatle
  • 31
  • 3
  • 1
    Where does the 6 come from? E.g. in ```char lines[n][6]```? – Jakub Cieślak Sep 14 '22 at 12:49
  • 1
    `int n; ... char lines[n][6];` -- This is not valid C++. Arrays in C++ must have their sizes denoted by a compile-time value, not a runtime value. Dynamic arrays in C++ are done by using `std::vector`. – PaulMcKenzie Sep 14 '22 at 12:51
  • As @PaulMcKenzie said, I was about to propose a solution with ```std::vector```, but I'm not sure I understand your approach to be honest. – Jakub Cieślak Sep 14 '22 at 12:53
  • I believe you should just start over and use `std::vector`. What you posted is not valid C++, even with your `std::string` example. – PaulMcKenzie Sep 14 '22 at 12:55
  • @JakubCieślak I have corrected it. The k = 8 is the expected length of the string (when I copied I simplified that bit of the code and forgot to change that) – beatle Sep 14 '22 at 12:55
  • I wonder when the da*n extensions that allows `T arr[nonconst]` to work will be nuked :D – Enlico Sep 14 '22 at 12:56
  • @beatle the length of the string is 7 according to what I see... anyway, this isn't legal in standard C++, you should check the answer below. – Jakub Cieślak Sep 14 '22 at 12:59
  • 1
    @Enlico -- *I wonder when the da*n extensions that allows T arr[nonconst] to work will be nuked* -- I also wished that gcc and clang would turn off the extensions by default, and only allow them using a compiler switch. What is happening now is that new programmers use the non-standard syntax thinking that they are writing valid C++ code. Then they write a mountain of code using this syntax, and then get disappointed when told that the code they wrote isn't valid. – PaulMcKenzie Sep 14 '22 at 12:59
  • You should avoid formatted input ```cin >> foo``` mixed with unformatted input ```cin.getline(foo, bar)``` – Hackjaku Sep 14 '22 at 13:10

1 Answers1

1

you are trying to create variadic length arrays, they are not a part of standard C++.

You should be using a combination of std::vector and std::string:

#include <iostream>
#include <string> //std::string, std::getline
#include <vector>

int main() {
    int n;
    std::cin >> n;
    std::cin.ignore();

    std::vector<std::string> lines(n);

    for (auto& line : lines) {
        std::getline(std::cin, line);
    }
}

The std::ignore is needed because when using std::cin an additional \n is added after pressing enter. The next std::getline will be aborted when seeing the \n. So the ignore removes the next additional character from std::cin. read more on why does std::getline() skip input after a formatted extraction?

Stack Danny
  • 7,754
  • 2
  • 26
  • 55