0

I am pretty new to C and I am working on an assignment for a class. This is only the first function of it but the part of the program I'm working on now is supposed to take a phone number and convert the last 7 digits to letters (like a flip phone). We would need to check which are actual words from the combinations of the letters, given a dictionary text doc, by sending the output to another function.

This first function just converts it to letters and spits out possible combinations. The problem I am having with it is that it prints (in the function) as it should but the return output is weird ascii.

const char *generate(char chararr[]) {
static char *dial[] = { "", "", "ABC", "DEF", "GHI", "JKL", "MNO", "PQRS", "TUV", "WXYZ", '\0'}; //pointers to strings
char *three, *four, *five, *six, *seven, *eight, *nine; //pointers to chars

char number[12] = {chararr[0], chararr[1], chararr[2], chararr[4],  //convertes pho-one-number to phonenumber
    chararr[5], chararr[6], chararr[8], chararr[9], chararr[10], chararr[11], '\0'};

const char *output1;//stores output for return value

for (three = dial[ number[ 3 ] - '0' ]; *three; three++ ) { 
    for (four = dial[ number[ 4 ] - '0' ]; *four; four++ ) { 
        for (five = dial[ number[ 5 ] - '0' ]; *five; five++ ) {
            for (six = dial[ number[ 6 ] - '0' ]; *six; six++ ) {
                for (seven = dial[ number[ 7 ] - '0' ]; *seven; seven++ ) {
                    for (eight = dial[ number[ 8 ] - '0' ]; *eight; eight++ ) {
                        for (nine = dial[ number[ 9 ] - '0' ]; *nine; nine++ ) {

                            char word[8]; //stores the word
                            word[0] = *three;
                            word[1] = *four;
                            word[2] = *five;
                            word[3] = *six;
                            word[4] = *seven;
                            word[5] = *eight;
                            word[6] = *nine;
                            word[7] = '\0';

                            output1 = word; //stores word in output1
            
                            printf("%s\n", output1); //<--------------printing the output
                            return output1; //<----------------- returning the output
                           

                        }
                    }
                }
            }
        }
    }
}


}

Also for reference here is main:

int main() {
    char num[13];

    printf("Enter a phone number: "); //enter and store number
    scanf("%s", &num);

    if (sizeof(num) < 13 || sizeof(num) > 13 || (num[3] != '-' || num[7] != '-')) {
        printf("Invalid Format\n");
        exit;
    }
    
    else {
      //  generate(num);
        printf("%s\n", generate(num));
    }
    
    //printf("%s\n", num);
    //generate(chararr);
    

    return 0;
}

Actual results:

Enter a phone number: 234-456-2344
GJMADGG
□□□

Expected results:

Enter a phone number: 234-456-2344
GJMADGG
GJMADGG
Matt
  • 190
  • 1
  • 13
  • 1
    `output` points to a local variable. That variable goes away when the function returns. – Barmar Nov 13 '20 at 22:27
  • Apart from that, `sizeof(num)` will always return 13, if you want to check the validity of the input, you may have better luck with strlen. –  Nov 13 '20 at 22:30
  • As a general rule, if you have to write more than 2 or 3 loops within loops (you have 7), then you're almost certainly missing an opportunity to simplify your code. – jarmod Nov 13 '20 at 22:32
  • 1
    @jarmod yea I thought that was kind of odd. *dial[] and those for loops were given to us as starter code by our professor – Matt Nov 13 '20 at 22:34
  • 1
    ....I hope it's a refactoring assignment – Rogue Nov 14 '20 at 02:13
  • @Rogue If i knew how to optimize/simplify this then I would lol – Matt Nov 14 '20 at 04:50

0 Answers0