C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 6th, 2013, 05:24 PM
davis123 davis123 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 3 davis123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 50 m 31 sec
Reputation 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;
}


}

Reply With Quote
  #2  
Old January 6th, 2013, 07:42 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,130 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 19 h 43 m 21 sec
Reputation Power: 1949
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.

Reply With Quote
  #3  
Old January 6th, 2013, 08:03 PM
davis123 davis123 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 3 davis123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 50 m 31 sec
Reputation Power: 0
Like I said, I am new to C++. I do appreciate you comments and your quick response. It worked, Thank you!

Reply With Quote
  #4  
Old January 7th, 2013, 09:15 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is online now
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,362 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 10 h 14 m 33 sec
Reputation Power: 383
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Text to Morse Code Converter with sounds and price for C++

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap