So I wrote a program to convert ONE letter or number into morse code.
That is, a user can input anything from A-Z, a-z, or 0-9 and have the morse code counterpart returned.
Now I am trying to change the character variable into a character array and do the same conversion, except I want to be able to convert the whole array of letters and numbers into morse code.
That is, I want a user to put in Hello, A123Z, etc. and be able to receive the morse code for it.
I can't get it to work. Also, so far I only have the character array go up to 10 places, but I want it to be unlimited. 10 is just my trial number until I get the project to work.
Here is the source code. What am I doing wrong??
// Morse code conversion
#include <iostream>
#include <string>
using namespace std;
int main()
{
// Declare variables
char morse[10];
string converted[10];
char repeat;
int i = 0;
// Create program loop:
do
{
// Prompt user for input:
cout << "Please enter a string of characters or numbers no more than 10 units long:\n";
cin >> morse;
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string UserSentence;
getline(cin, UserSentence); //This allows as many characters as the user wants, and spaces too.
string ConvertedToMorse [ UserSentence.length() ]; //Creating the array of strings using the amount of characters of the user sentence
for(int i = 0; i < UserSentence.length(); i += 1)
{
if(UserSentence.at(i)=='A' || UserSentence.at(i)=='a')
ConvertedToMorse[i]="* -";
elseif (UserSentence.at(i)=='B' || UserSentence.at(i)=='b')
ConvertedToMorse[i]="- * * *";
// else if (/*And so on*/)
else ConvertedToMorse[i]=" "; //Else, only put a space
}
for (int x = 0; x < UserSentence.length(); x += 1)
cout << ConvertedToMorse[x] << " ";
cout << endl;
cin.get();
}
Oh, and you increased the value of 'i' two times, notice i += 1 at the end of your for loop
// Morse code conversion
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
// Declare variables
string morse;
char repeat;
int i = 0;
int x;
cout << "Please enter a string of characters or numbers:\n";
getline(cin, morse);
x = morse.length();
string converted[x];return 0;
}