Morse code translator

If you could tell me everything I'm doing wrong and tell me why it doesn't output the morse for each letter... That would be great.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;


//string abc[] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
char abc[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string morse[] = {".- ", "-... ", "-.-. ", "-.. ", ". ", "..-. ", "--. ", ".... ", ".. ", ".--- ", "-.- ", ".-.. ", "-- ", "-. ", "--- ", ".--. ", "--.- ", ".-. ", "... ", "- ", "..- ", "...- ", ".-- ", "-..- ", "-.-- ", "--.. "};
//char morse[] = {". - , -... , -.-. , -.. , . , ..-. , --. , .... , .. , .--- , -.- , .-.. , -- , -. , --- , .--. , --.- , .-. , ... , - , ..- , ...- , .-- , -..- , -.-- , --.. , --.."};
int main() // Initialize main function
{
    for(int i = 0; i <= 25; i++){ // Output all of the letters and their corresponding morse combinations. 
        cout << morse[i] << endl;
        cout << abc[i] << endl;

    }
    cout << "\n"; // Output a newline
    char test[] = "0"; 
    // Give test a value just in case the user doesn't, plus I can't seem to assign an empty table char
    
    cout << "Enter a letter or word" << endl; 
    // Prompt user
    
    cin >> test; 
    // Pass the input value to the test table (probably the problem)
    
    cout << "Total length of input: " << strlen(test) << endl;
    // Tell the user the length of the input
    
    for(int z = 0; z <= strlen(test); z++)
    {
        for(int x = 0; x <= strlen(abc); x++){
            if(abc[x] == test[z]){
                cout << morse[x];
            }
        }
    }
    // Search through the test character table and check if there is a letter and morse combination to match.
}
Last edited on
- 'test' has memory reserved only for a single character. To fix, declare it as string test; . Use test.size() instead of strlen(test).
- in the for loop, use < instead of <=
- use toupper: if(abc[x] == toupper(test[z])){
Awesome, thanks so much! One more question, how do I turn the input into all caps?
Wait... To-upper... Thanks.
Topic archived. No new replies allowed.