I need to "decode" a string into numbers. it should convert
1=ij 2=abc 3=def
4=gh 5=kl 6=mn
7=prs 8=tuv 9=wxy
0=oqz
so i made a code where i use a dynamic string array which should get filled up according to the above table, but when i try to print the output string i don't get nothing. What's wrong? Any help appreciated. Here's the code:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string a;
    int b;
    while(1)
    {
            cin>>a;
            if(a!="-1")
            {
                    cin>>b;
                    string input;
                    string* arrconv = new string[30];
                    for(int i=0; i<b; i++)
                    {
                            cin>>input;
                            for(int j=0; j<input.length(); j++)
                            {
                                    if((input[j]=='i')||(input[j]=='j'))
                                         arrconv[i][j]='1';
                                    else if((input[j]=='a')||(input[j]=='b')||(input[j]=='c'))
                                         arrconv[i][j]='2';
                                    else if((input[j]=='d')||(input[j]=='e')||(input[j]=='f'))
                                         arrconv[i][j]='3';
                                    else if((input[j]=='g')||(input[j]=='h'))
                                         arrconv[i][j]='4';
                                    else if((input[j]=='g')||(input[j]=='h'))
                                         arrconv[i][j]='5';
                                    else if((input[j]=='m')||(input[j]=='n'))
                                         arrconv[i][j]='6';
                                    else if((input[j]=='p')||(input[j]=='r')||(input[j]=='s'))
                                         arrconv[i][j]='7';
                                    else if((input[j]=='t')||(input[j]=='u')||(input[j]=='v'))
                                         arrconv[i][j]='8';
                                    else if((input[j]=='w')||(input[j]=='x')||(input[j]=='y'))
                                         arrconv[i][j]='9';
                                    else if((input[j]=='o')||(input[j]=='q')||(input[j]=='z'))
                                         arrconv[i][j]='0';
                            }
                            string wtf=arrconv[i];
                            cout<<arrconv[i].length()<<endl<<wtf;
                            
                    }
            }
            system("pause");
    }
    return 0;
}

Recommended Answers

All 5 Replies

If you are going to use new on line 16 then you have to delete[] it sometime later. Without that your program has a huge memory leak. Avoid that whole issue by not using pointers, declare the array on the stack. 30 strings is not going to cause stack overflow.

'g' and 'h' if statements appears twice.

A switch statement might be easier to code and read instead of all those if statements.

your program has a lot of other problems with those if statements. You can't just add digits to the strings like you are doing because there is no memory available for them. Use the += operator and remove [j] notation. Like this: arrconv[i]+='1';

i just thought i can treat the string array as a 2d char array. anyways, your way works too. thanks a lot.

>>i just thought i can treat the string array as a 2d char array
You can not do that because it isn't a 2d char array.

isn't a c++ style string an array of chars?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.