i dont know what is wrong with it (help plz)

This is a discussion on i dont know what is wrong with it (help plz) within the C++ Programming forums, part of the General Programming Boards category; // this code should generate all possible eight-letter combinations from an eight-digit phone number. The procedure doesn't check if the ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    7

    i dont know what is wrong with it (help plz)

    // this code should generate all possible eight-letter combinations from an eight-digit phone number. The procedure doesn't check if the phone number contains 0's or 1's.

    Code:
    #include<iostream>
    using namespace std;
    
    const char* letters[] = {
        "ABC",
        "DEF",
        "GHI",
        "JKL",
        "MNO",
        "PRS",
        "TUV",
        "WXY"
    };
    FILE* fp;
    char str1[15];
    
    void GenCombinations (
        const char* number,
        char str[]) 
    {
        if (*number == '\0')
        {
    	fwrite(str - 8, 1, 8, fp);
    	cout<<str<<endl;
    	fwrite("\r\n", 1, 2, fp);
    	return;
        }
        else if (*number == '-')
    	GenCombinations(number + 1, str);
        else {
    	const int group = *number - '0' - 2;
    	for (int i = 0; i < 3; i++)
    	{
    	    *str = letters[group][i];
    	    cout<<str<<endl;
    	    GenCombinations(number + 1, str + 1);
    	}
        }
    }
    int main(){
        char str2[]="42472888";
        strcat("\0",str2);
        GenCombinations(str2,str1);
         cout<<str1<<endl;
           system("PAUSE");
        return 0;
        }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    First, drop the strcat line.

  3. #3
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    What! Another thread of the same topic.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    revised code
    Code:
    #include<iostream>
    using namespace std;
    
    const char* letters[] = {
        "ABC",
        "DEF",
        "GHI",
        "JKL",
        "MNO",
        "PRS",
        "TUV",
        "WXY"
    };
    FILE* fp;
    char str1[15];
    
    void GenCombinations (
        const char* number,
        char str[]) 
    {
        if (*number == '\0')
    	{
    		fwrite(str - 8, 1, 8, fp);
    		cout<<str<<endl;
    		fwrite("\r\n", 1, 2, fp);
    		return;
        }
        else if (*number == '-')
    	{
    	GenCombinations(number + 1, str);
    	}
        else 
    	{
    		const int group = *number - '0' - 2;
    		char fullstr[3] = "";
    
    		for (int i = 0; i < 3; i++)
    		{
    			*str = letters[group][i];
    			strcat(fullstr,str);
    			
    		}
    		cout<<fullstr<<endl;
    
        }
    	GenCombinations(number + 1, str + 1);
    }
    
    int main()
    {
        char str2[]="42472888";
        //strcat("\0",str2);
        GenCombinations(str2,str1);
        cout<<str1<<endl;
        return 0;
        }

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    and you have an error with your fwrite code that's giving the memory addr problem - I'm working on that part now.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    7
    dropin the strcat does not help much it gives the same error masg.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    I'm not sure what you are trying to accomplish with the fwrite.

    If the output to the altered program isn't what you want, I recommend looking as the possible letter variations. 8 numbers each with 3 letter combination. Wouldn't that be 8^3 possibilities? Too early for math for me. With that, I'd write out a string of characters at a time, like:
    AMDIGMDW
    GKEWIOFA
    I'd rather see 8^3 in that style instead of
    A
    M
    D
    ...

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    7
    My idea is to write a C++ program that, given an eight-digit number, writes to a file every possible eight-letter word combination corresponding to that number. There are 6561 (38) such words. Avoid phone numbers with the digits 0 and 1.

    Digit Letter
    2 A B C
    3 D E F
    4 G H I
    5 J K L
    6 M N O
    7 P R S
    8 T U V
    9 W X Y

    Telephone keypad digits and letters

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    Concentrate on how to building a grid such as:
    11111111
    21111111
    31111111
    12111111
    13111111
    11211111
    11311111
    11121111
    11131111
    11112111
    11113111
    11111211
    11111311
    11111121
    11111131
    11111112
    11111113

    12211111
    12311111
    11221111
    11231111
    11122111
    11123111

    and iterating via loops. I understand trying to call the function within itself but it might be easier to come up with a grid build from some nested loops.

  10. #10
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    The program is almost done why cant you finish it? seems like you got that code from somewhere.

    File i/o
    http://www.cprogramming.com/tutorial/cfileio.html

  11. #11
    Registered User
    Join Date
    Sep 2005
    Posts
    7
    i know that there is somethin worng with the loopin or the func.. callin.
    loko. i am workin on it i just need someone to trease the code and fined out what i am missin.
    thanks anyway.

  12. #12
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Code:
     //*fp is pointing at nothing yet. You have to open a file before you can write
     //and str1 has the results not str
    		fwrite(str - 8, 1, 8, fp);
    
    		cout<<str<<endl;
    		fwrite("\r\n", 1, 2, fp);
    Code:
     int main()
    {
        char str2[]="42472888";
        strcat("\0",str2);  // What is this for?     GenCombinations(str2,str1);
        cout<<str1<<endl;
        return 0;
        }

  13. #13
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I would use iterative approach, with nested loops, personally, unless you must try to find a recursive approach because this is an assignment to learn about recursion.

    Here's a two level iterative version that should work, though it hasn't been tested:
    Code:
    char grp[2][4] = { "ABC", "DEF"}; //all possible groups of letters
    char result[3];
    int i, j; //control which letter to use from a given group of letters
    for(i = 0; i < 3; ++i) //controls which letter used from grp[0] = "ABC"
    {
      result[0] = grp[0][i];
      for(j = 0; j < 3; ++j) //controls which letter used from grp[1] = "DEF"
      {
    	result[1] = grp[1][j];
     
    	result[2] = '\0'; //change char array into string
    	cout << result << endl; //display the two char string
     
    	//write this string to file here if desired
      }
    }
    But, I'd need to use the digit from the phone number as an index to control which group of letters I want to use with which nested loop, since the digits of a phone number aren't constant. So the code could be generalized to look something like this:
    Code:
    int input[8] = 8 digit array of int representing the phone number
    char grp[8][4] = { "ABC", "DEF", etc; //expand to all 8 possible groups of letters
     
    char result[9];
    int i, j; //control which letter to use from a given group of letters
    for(i = 0; i < 3; ++i) //controls which letter used from the letters associated with the first digit of the phone number
    {
      result[0] = grp[input[0]][i];
      for(j = 0; j < 3; ++j) //controls which letter used from the letters associated with the second digit of the phone number
      {
      result[1] = grp[input[1]][j];
     
      //expand nesting of loops to 8 from 2 here
     
      //these lines still go at the end of the last nested loop just like they did for a 2 level version
      result[8] = '\0'; //change char array into string
      cout << result << endl; //display the 8 char string
     
      //write to file if desired after displaying on screen
    }
    BUT, the phone number input isn't as an array of ints, its an array of char, so I have to change each useful char in the phone number string to an int at some point. Since the ASCII value of the digit characters are all contiguous I can convert them to ints by subtracting the ASCII value of the first one, which I think is '0', from any of the others (that is, '0' - '0' = 0, '1' - '0' = 1, etc.)

    Code:
    char phoneNumber[9] = a phone number with 8 digits and a null terminator
    int input[8] = phone number using numbers rather than characters
    for(i = 0; i < 8; i++)
      input[i] = phoneNumber[i] - '0';
    Now, with any luck, I have the phone number as an array of ints and I have an algorhithm to turn each int into any of three char and combine it any combination with 7 other int/char combinations to lead to all possible 8 letter "words" available from an 8 number phone number. So far, so good, in theory at least. However, in looking at my phone key pad I see I need to make a few adjustments since 7 is PQRS and not PQR and 9 is WXYZ and not UVW, so, obviously, some numbers can be associated with 4 letters instead of 3, but that shouldn't be too difficult, maybe something like

    for(a = 0; a < strlen(grp[input[x]]); ++a)

    instead of a constant size of three every time.

    Have fun playing around with it if you use this approach.

    EDIT: Oops. Once I convert from phone number as an array of char to an array of ints I need to adjust the value of the int to be used as an index. Since the indexes of grp[x] range from 0 to 7 and the ints in the phone number range from 2 to 9 I can just subtract 2 from the original int to get the int as an index. This can be done as follows. Change this line:

    input[i] = phoneNumber[i] - '0';

    to

    input[i] = phoneNumber[i] - '0' - 2;

    now the int at input[i] will correspond to the group of letters to use at grp[input[i]]

    cout
    Last edited by elad; 09-07-2005 at 12:02 PM.
    You're only born perfect.

  14. #14
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    I used another method:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    const char* const letters[] = 
    {
        "ABC",
        "DEF",
        "GHI",
        "JKL",
        "MNO",
        "PRS",
        "TUV",
        "WXY"
    };
    
    #define DIGIT_COUNT        8    // 2
    #define TOTAL_COMBINATIONS 6561 // 9
    
    void MakeWord(size_t     value,
                  const char sNumber[DIGIT_COUNT + 1],
                  char       sWord[DIGIT_COUNT + 1])
    {
    	size_t i = DIGIT_COUNT - 1;
    
    	do
    	{
    		 Missing line 1
    		Missing line 2
    	} while (i-- > 0);
    
    	sWord[DIGIT_COUNT] = '\0';
    }
    
    int main(void)
    {
    	size_t      i;
    	const char* sNumber = "32456789";
    	char        sWord[DIGIT_COUNT + 1];
    
    	for (i = 0; i < TOTAL_COMBINATIONS; i++)
    	{
    		MakeWord(i, sNumber, sWord);
    		puts(sWord);
    	}
    
    	getchar();
    	return 0;
    }
    The missing lines are left for the reader to discover. As a clue, read this thread and think "base 3".

  15. #15
    Registered User
    Join Date
    Sep 2005
    Posts
    7
    elad: anonytmouse: thank you guys i really appreciate but your codes are a bit advanced for me havin some difficulties in undrestandin them
    anyway guys i reached this point hopin that u continue with me to overcome it .

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can any1 plz make this assignment
    By jean in forum C Programming
    Replies: 17
    Last Post: 05-13-2009, 09:19 PM
  2. [Request] Need Help Plz
    By TylerD in forum Tech Board
    Replies: 4
    Last Post: 01-03-2009, 08:54 AM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21