Thread: need help quickly

  1. #1
    ver
    Guest

    Unhappy need help quickly

    This program should cdo two things:
    1: Convert a english phrase to morse code
    2 convert a morse phrase to english

    The english phrase should be taken and each letter seperated.
    The position of the letter in the english array should be located and the correspomding position should be found in the morse array. The program should then print the morse code equivalent of the letters entered with spaces in between.


    #include <stdio.h>
    #include <conio.h>
    #include <string.h>

    void encode();
    void decode();

    char english[]={"ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 "};

    char *morse[]={".-", "-...", "-.-.","-..", ".", "..-.", "--.", "....", "..",
    ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-",
    ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--",
    "--..", ".----", "..---", "...--", "....-", ".....", "-....",
    "--...", "---..", "----.", "----", " "};

    main()
    { int choice;

    do{
    printf("\n Choose one of the following options");
    printf("\n 1: Convert english to morse code");
    printf("\n 2: Convert morse code to english ");
    scanf("%d", &choice);
    }while(choice<1 || choice>2);

    if(choice==1)
    encode();
    if(choice=2)
    decode();

    getch();
    return 0;
    }

    void encode()
    {
    char message[100]=" "; char code[200]="";
    int c;
    int z, x;

    printf("\n Enter the phrase you wish to convert to morse: ");
    scanf("%s", message);

    c = strlen(message); //strlen finds the length of a string excluding
    //the null token
    for(z = 0; z <= c; z++) //the problem starts here
    {
    for(x = 0; english [x]!= message[z] && x < 37; x++); //while theposition english x
    strcat(code, morse[x]); //is not equal to the position in morse x and x<37
    strcat(code, " ");

    }
    printf("Code is %s", code);

    getch();

    }

    void decode();
    {
    char string[200] = " ", eng[100] = " ";
    char tokenptr;
    int p;


    printf("\n Enter the code you wish to convert to morse.");
    printf("\n Use one blank space between each morse coded letter and");
    printf("\n three blank spaces between each morse coded word");
    scanf("%s", string);

    tokenptr = strtok(string, " ");

    while(tokenptr != NULL)
    {

    printf("%s\n", tokenPtr);
    tokenPtr = strtok(NULL, " "); //the call to string should continue breaking up the string
    } //from the location in string saved by the last token


    getch();
    }

    All your help will be appreciated!!!!!

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Post using code tags and don't ask for urgent help.

    If you have a specific problem with your code, state what it is.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    >for(z = 0; z <= c; z++) //the problem starts here

    You are doing one loop too many.

    change <= to <
    Code:
    for(z = 0; z < c; z++)

  4. #4
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    More problems...
    >>printf("%s\n", tokenPtr);
    first of all, it's tokenptr, second it is a char, not a string (array of chars)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quickly learn C# for a C++ expert
    By BigDaddyDrew in forum C# Programming
    Replies: 9
    Last Post: 06-06-2004, 04:38 PM
  2. How can I initiate my growth of beard more quickly?
    By Zewu in forum A Brief History of Cprogramming.com
    Replies: 38
    Last Post: 07-21-2003, 05:15 PM
  3. console.. character.. screen.. quickly!
    By CodeMonkey in forum C++ Programming
    Replies: 2
    Last Post: 06-29-2003, 06:00 PM
  4. I need excuses for school dance! -- quickly!
    By Leeman_s in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 01-21-2003, 04:42 AM
  5. Reading from a file quickly
    By PJYelton in forum C++ Programming
    Replies: 5
    Last Post: 10-11-2002, 01:12 PM