-1

I have Multidimensional array in my struct i want to work with this array using few methods, i think i should use double pointer but i have no idea how to do it

struct generator {
    char r[26][max];

        void set();
    void display();
};

void generator::set() {
    char *tab = new char[max];
    int k = 0;

    cin >> tab;

    while (tab[k] != '\0') {    
        r[0][k] = tab[k];
        k++;
}
void generator::display(){
    cout << r[0][1];    // should display first letter of string
}
NoobXDDD
  • 1
  • 3
  • 2
    The comment "should display first letter of string" is wrong, as you display the *second* element. Besides that (and the uncompilable code), what problem you have? What is your question? Please read [the help pages](http://stackoverflow.com/help), take [the SO tour](http://stackoverflow.com/tour), read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly learn how to create a [mcve]. – Some programmer dude Apr 04 '19 at 07:21
  • Possible duplicate of [Passing a 2D array to a C++ function](https://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function) – skratchi.at Apr 04 '19 at 07:27
  • 1
    Prefer `std::string` to raw `char*`. and possibly `std::vector` for `r`. – Jarod42 Apr 04 '19 at 07:28

1 Answers1

1

in

 cout << r[0][1];    // should display first letter of string

the comment is wrong, that displays the second letter, first letter is at index 0


Adding enough definitions and missing '}' to compile and execute :

#include <iostream>
using namespace std;

#define max 10

struct generator {
  char r[26][max];

  void set();
  void display();
};

void generator::set() {
  char *tab = new char[max];
  int k = 0;

  cin >> tab;

  while (tab[k] != '\0') {    
    r[0][k] = tab[k];
    k++;
  }
}

void generator::display(){
  cout << r[0][1];    // should display first letter of string
}

int main()
{
  generator g;
  g.set();
  g.display();
  cout << endl;
}

Compilation and execution :

/tmp % g++ -pedantic -Wall -Wextra g.cc
vxl15036 /tmp % ./a.out
aze
z
bruno
  • 31,755
  • 7
  • 21
  • 36
  • @NoobXDDD because you missed to flush the output , look my answer, I use `cout << endl;` in _main_ for that – bruno Apr 04 '19 at 07:23