#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Text to Morse Code Converter with sounds and price for C++


    Hello, I am new to C++ and I have a program to make for school that consists in a text-to-morse code converter with sound and price. I believe I almost finish it, only, I have a little problem. the for loop is giving me a headache. when plug in a word say "hello", it only outputs the number assigned on the variable of the for loop (for(int i=0; i<2; i++). Then it only outputs the letter "h" and "e".
    Please help me solve the problem. you can run it and you will see what I'm talking about.

    code:

    #include <iostream>
    #include <dos.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <windows.h>
    #include <cstring>


    using namespace std;
    void morseCode(char);
    void delay(int wait)
    {
    Sleep(wait);
    }
    void dot()
    {
    Beep(400, 100); //Beep( freq, duration)
    cout << '.';
    }
    void dash()
    {
    Beep(500, 200);
    cout << '-';
    delay(1); // Sleep(1000);
    }

    int main()

    {

    SetConsoleTitle("Morse Converter");
    string in;

    cout<< "enter a word: ";
    getline(cin, in, '\n');
    int len = in.length();
    string opcode;

    int value;
    cout << "count of letters and spaces is :" << in.length() <<endl;
    float cost;
    float price_per_letter = 0.01; // 1 cent per letter, includes that operator must read blanks.
    cost = price_per_letter * len;
    cout<< "Your text message cost $"<< cost << endl;

    for(int i=0; i<2; i++)
    {
    char letter = in[i];
    cout << "letter "<< letter;
    morseCode( letter);
    cout << endl;
    delay(1);
    }
    cin.get();
    return 0;
    }
    void morseCode(char letter)
    {
    switch(toupper(letter)) {
    case 'A':
    dot();dash();
    break;
    case'B':
    dash();dot();dot();dot();
    break;
    case 'C':
    dash(); dot(); dash();dot();
    break;
    case 'D':
    dash();dot();dot();
    break;
    case 'E':
    dot();
    break;
    case'F':
    dot();dot();dash();dot();
    break;
    case 'G':
    dash(); dash(); dot();
    break;
    case 'H':
    dot();dot();dot();dot();
    break;case 'I':
    dot();dot();
    break;
    case'J':
    dot();dash(); dash();dash();
    case 'K':
    dash(); dot(); dash();
    break;
    case 'L':
    dot();dash();dot();dot();
    break;
    case 'M':
    dash();dash();
    break;

    case 'O':
    dash(); dash(); dash();
    break;
    case 'P':
    dot();dash();dash();dot();
    break;
    case 'Q':
    dash();dash();dot();dash();
    break;
    case'R':
    dot();dash();dot();
    break;
    case 'S':
    dot();dot();dot();
    break;
    case 'T':
    dash();
    break;
    case 'U':
    dot();dot();dash();
    break;
    case'V':
    dot();dot();dot();dash();
    break;
    case 'W':
    dot(); dash(); dash();
    break;
    case 'X':
    dash();dot();dot();dash();
    break;case 'Y':
    dash();dot();dash();dash();
    break;
    case'Z':
    dash();dash();dash();dot();

    break;
    case ' ':
    break;

    default:
    cout<<"bad input"<< endl;
    }


    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,172
    Rep Power
    2222
    For one thing, your formatting is complete rubbish (I viewed the original via the Reply button), so how can you even tell what you've written?

    Also, you need to use code tags when you post a listing so that you can preserve the formatting. Which does not relieve you of the responsibility to properly format it in the first place.

    Here is how it should appear:
    Code:
    #include <iostream>
    #include <dos.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <windows.h>
    #include <cstring>
    
    
    using namespace std;
    void morseCode(char);
    
    void delay(int wait)
    {
        Sleep(wait);
    }
    
    void dot()
    {
        Beep(400, 100); //Beep( freq, duration)
        cout << '.';
    }
    
    void dash()
    {
        Beep(500, 200);
        cout << '-';
        delay(1); // Sleep(1000);
    }
    
    int main()
    {
        SetConsoleTitle("Morse Converter");
        string in;
    
        cout<< "enter a word: ";
        getline(cin, in, '\n');
        int len = in.length();
        string opcode;
    
        int value;
        cout << "count of letters and spaces is :" << in.length() <<endl;
        float cost;
        float price_per_letter = 0.01; // 1 cent per letter, includes that operator must read blanks.
        cost = price_per_letter * len;
        cout<< "Your text message cost $"<< cost << endl;
    
        for(int i=0; i<2; i++)
        {
            char letter = in[i];
            cout << "letter "<< letter;
            morseCode( letter);
            cout << endl;
            delay(1);
        }
        cin.get();
        return 0;
    }
    
    void morseCode(char letter)
    {
        switch(toupper(letter)) 
        {
            case 'A':
                dot();dash();
                break;
            case'B':
                dash();dot();dot();dot();
                break;
            case 'C':
                dash(); dot(); dash();dot();
                break;
            case 'D':
                dash();dot();dot();
                break;
            case 'E':
                dot();
                break;
            case'F':
                dot();dot();dash();dot();
                break;
            case 'G':
                dash(); dash(); dot();
                break;
            case 'H':
                dot();dot();dot();dot();
                break;
            case 'I':
                dot();dot();
                break;
            case'J':
                dot();dash(); dash();dash();
            case 'K':
                dash(); dot(); dash();
                break;
            case 'L':
                dot();dash();dot();dot();
                break;
            case 'M':
                dash();dash();
                break;
    
            case 'O':
                dash(); dash(); dash();
                break;
            case 'P':
                dot();dash();dash();dot();
                break;
            case 'Q':
                dash();dash();dot();dash();
                break;
            case'R':
                dot();dash();dot();
                break;
            case 'S':
                dot();dot();dot();
                break;
            case 'T':
                dash();
                break;
            case 'U':
                dot();dot();dash();
                break;
            case'V':
                dot();dot();dot();dash();
                break;
            case 'W':
                dot(); dash(); dash();
                break;
            case 'X':
                dash();dot();dot();dash();
                break;
            case 'Y':
                dash();dot();dash();dash();
                break;
            case'Z':
                dash();dash();dash();dot();
                break;
            case ' ':
                break;
    
            default:
                cout<<"bad input"<< endl;
        }
    }
    Please direct your attention to case 'J'. Please note the total lack of a break statement there. If you had formatted your code properly, then that would have been immediately obvious to you too.

    But that has nothing to do with the problem you posted. Consider this part of your program:
    Code:
        for(int i=0; i<2; i++)
        {
            char letter = in[i];
            cout << "letter "<< letter;
            morseCode( letter);
            cout << endl;
            delay(1);
        }
    Each iteration of that loop processes and displays one and only one input letter. Since you only run that loop twice, it will only process and display the first two letters. QED.

    Since you have already determined the length of the input string and stored it in len, may I recommend that you run that for-loop len times? That should process and display the entire input string.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0
    Like I said, I am new to C++. I do appreciate you comments and your quick response. It worked, Thank you!
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,893
    Rep Power
    481
    If your program works please post it at rosettacode.org . They don't yet have a C++ solution. Follow link:
    Here are solutions in other languages (good grief, this solution is mine).
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo