morse code convertor

This is a discussion on morse code convertor within the C++ Programming forums, part of the General Programming Boards category; I'm having troubles getting this converter to work. The getline doesn't seem to work. It skips it and goes right ...

  1. #1
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640

    morse code convertor

    I'm having troubles getting this converter to work. The getline doesn't seem to work. It skips it and goes right to the end for some reason.

    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    char *morse[36] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....",
                        "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.",
                        "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-",
                        "-.--", "--..",
                        
                        "-----", ".----", "..---", "...--", "....-", ".....",
                        "-....", "--...", "---..", "----." };
    int main()
    {
        int choice = 0;
        while ((choice != 1) && (choice != 2))
        {
            cout << "Enter a choice:" << endl;
            cout << "1. Text-to-Morse" << endl;
            cout << "2. Morse-to-Text" << endl;
            cin >> choice;
        }
        if (choice == 1)
        {
            cout << "Enter a sentence (MAX = 80 chars):" << endl;
            char sentence[81];
            cin.get(sentence, 81);
            for (int i=0; i<=80; i++)
            {
                int mov = 0;
                sentence[i] = toupper(sentence[i]);
                if ((sentence[i] <= 90) && (sentence[i] >= 65))
                    mov = 65;
                if ((sentence[i] <= 57) && (sentence[i] >= 22))
                    mov = 48;
                if (sentence[i] == 32)
                {
                    cout << "   ";
                }
                else { cout << morse[sentence[i]-mov] << " "; }
            }
        }
        if (choice == 2)
        {
            cout << "Enter a sentence in morse (MAX = 80 chars):" << endl;
            char sentence[81];
            cin.getline(sentence, 81);
            char delims[] = " ";
            char *result = strtok(sentence, delims);
            while (result != NULL)
            {
                for (int i=0; i<=35; i++)
                {
                    if (strcmp(result, morse[i]) == 0)
                    {
                        if (i >=26)
                            cout << (char)(i+22);
                        else
                            cout << (char)(i+65);
                    }
                }
                cout << " ";
                result = strtok(NULL, delims);
            }
        }
        cin >> choice;
        return 0;
    }
    Last edited by neandrake; 04-29-2004 at 11:51 AM.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,993
    I think that the newline character is being entered into:
    cin.get(sentence, 81);

    a solution might be to use:

    Code:
    while ((choice != 1) && (choice != 2))
    {
        cout << "Enter a choice:" << endl;
        cout << "1. Text-to-Morse" << endl;
        cout << "2. Morse-to-Text" << endl;
        cin >> choice;
        cin.get();
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Hey, thanks a bunch, that fixed it. Well, I used cin.ignore() instead, but thanks for pointing that problem out to me. I was amazed that after fixing that problem, the program works (seeing as I wasn't able to test it before). I was just gonna post another problem about displaying the characters in Morse to Text, but I just solved it, there needs to be parenthesis around the i+48 and i+65. Will edit that in now.

    Thanks again
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do I morse code converting program
    By panfilero in forum C Programming
    Replies: 17
    Last Post: 10-29-2005, 09:16 PM
  2. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 03:28 AM
  3. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  4. About the Morse code Converter
    By Amber_liam in forum C Programming
    Replies: 17
    Last Post: 05-29-2002, 08:35 AM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

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