-3

I have made a char array, where I basically enter the number of rows and columns, and enter in the letters for each row and column, such as x and y. Basically, like an normal array but with letters. After I enter the letters and finish with the array, I want to enter 2 numbers to specify a specific char in the array. For example, my array is 3x3. I fill my array with the characters x and y. After I do that, I enter 2 2. At row 2 and column 2, the letter there is y. So I want to print out y.

Here is my code.

#include <iostream>

using namespace std;

int main()
{
  int x,y;
  cin>>x>>y;
  char n[x][y];
  for(int i=0;i<x;i++)
  {
      for(int j=0;j<y;j++)
      {
          cin>>n[i][j];
      }
  }
  int a,b;
  cin>>a>>b;
  cout<<n[a][b];
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
hazey2
  • 1
  • 2
  • `int x,y; cin >> x>>y; char n[x][y];` is not standard C++. Use `std::vector` – Jason May 15 '22 at 14:06
  • Im not using standard c++. – hazey2 May 15 '22 at 14:11
  • 2
    Why did you tag `c++` then? And what are you using? And what exactly is your question. The question is unclear on whether you're getting an error or want to do something else. – Jason May 15 '22 at 14:13
  • @AnoopRana Am using c++17 and I dont have an error I just dont know how to solve a problem – hazey2 May 15 '22 at 14:21
  • @hazey2 If you're using language extensions, then you must say what language extensions of which compiler you are using in the question. Otherwise we cannot know. – eerorika May 15 '22 at 14:22
  • @hazey2 C++17 is a standard C++ version. I think you are not aware of that. Also, please specify the exact problem you're facing like the current program doesn't give any output etc. – Jason May 15 '22 at 14:22
  • @eerorika I am using gdb online – hazey2 May 15 '22 at 14:30
  • @AnoopRana The code works and prints just it doesn't work the way I want it to. I want it to lets say print 3 it prints 9 like that basically my code is wrong. – hazey2 May 15 '22 at 14:31
  • @AnoopRana Wait so I am writing the array wrong? My professor taught me this way and youtube and it always works so why is it wrong? Is it slower than the vector way or does it sometimes not work. I am not trying to be rude just to learn. – hazey2 May 15 '22 at 14:34
  • @hazey2 `so why is it wrong` It's wrong because the C++ language rules say that it's wrong. `My professor taught me this way` Either your professor taught you wrong, or you misunderstood. `does it sometimes not work` It doesn't work if you don't use a compiler that extends the language. It also won't work if the element type has constructors for example. – eerorika May 15 '22 at 14:36
  • @hazey2 It's not valid because in standard C++, the size of an array must be a compile time constant and `x` and `y` are not. See in my answer below i have explained this. I have also given a [demo](https://onlinegdb.com/jhPgIn-lsE) which you can try out. Also, it would be better to learning using a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Jason May 15 '22 at 14:36

1 Answers1

1

The main problem with your given program is that in Standard C++ the size of an array must be a compile time constant. This means that the following is not valid:

int x,y;
cin>>x>>y;
char n[x][y];//not standard C++ because x and y are not constant expressions

To solve this you can use a std::vector<char> as shown below:

#include <iostream>
#include<vector>


int main()
{
  int x,y;
  std::cin>>x>>y;
  //create a 2D vector
  std::vector<std::vector<char>> n(x, std::vector<char>(y)); 
  
  //iterate through rows 
  for(auto& row: n)
  {
      //iterate through each element in the row
      for(auto& col: row)
      {
          std::cout<<"enter element: "<<std::endl;
          std::cin>>col; //take input from user
      }
  }
  int a,b;
  std::cout<<"a and b"<<std::endl;
  std::cin>>a>>b;
  std::cout<<"element at position: "<<a<<" "<<b <<" is: "<<n[a][b];
}

Working demo

Jason
  • 36,170
  • 5
  • 26
  • 60