-2
#include <iostream>
 #include <string>
#include <vector>

using namespace std;

void converter(vector<char>keypad[],int num[], int index, string result, int size_of_num){
   if(index == size_of_num){
    cout << result << " ";
   return;
   }

  int digit = num[index];
  int size_of_keypad = keypad[digit].size();

  for(int i=0; i < size_of_keypad; i++){
    converter(keypad, num, index + 1, result + keypad[digit][i], size_of_num);
  }
  
}


int main(){
    vector <char> keypad[] = {
    {},{},{'a','b','c'},
        {'d','e','f'},
        {'g','h','i'},
        {'j','k','l'},
        {'m','n','o'},
        {'p','q','r','s'},
        {'t','u','v'},
        {'w','x','y','z'}};

    int numbers[] = {2,3,4};

    int size_of_num = sizeof(numbers);
    
    converter(keypad, numbers, 0, string (""), size_of_num );

    return 0;
}

I tried running this code, which was to transfrom phone numbers into words. I used vector in this problem with a reucrsive function. But I am not sure if its just the memory overflowing or just totally something else.

I tried and search online in order to solve this and I found the correct code, but everything they were doing was the same and the only thing that was differnt were the varible names. I just couldn't figure out whats wrong.

JustinW
  • 3
  • 2
  • You are mixing up "C" style arrays with C++ vectors that isn't good. Vectors can be nested so if you want 2D data use `std::vector>` or `std::vector`. But to convert a character to an int, use std::unordered_map – Pepijn Kramer Dec 16 '22 at 06:05
  • A vector of strings would be an easier data structure to use than an array of char vectors. `vector keypad = { "", "", "abc", "def" ... };` – john Dec 16 '22 at 07:24

1 Answers1

2

you calculate size wrong

int size_of_num = sizeof(numbers);

should be

int size_of_num = sizeof(numbers)/ sizeof(numbers[0])

or even

int size_of_num = std::size(numbers);
pm100
  • 48,078
  • 23
  • 82
  • 145