Calling a void function with user input (morse code)

I am confused on how to call the void function & make it prompt the user to input a string/output result in morse code. I am not sure I set up the actual void function correctly. I bolded the main parts. The unbolded are just other menu options I'll use later. Thank you (This compiler automatically includes all needed #includes).



#include <iostream>

using namespace std;

void cha (char c){
c=toupper(c);
if (c == 'A')
{
cout << ".-" ;
}
else if (c == 'B')
{
cout << "-..." ;
}
else if (c == 'C')
{
cout << "-.-." ;
}
else if (c == 'D')
{
cout << "-..";
}
else if (c == 'E')
{
cout << ".";
}
else if (c == 'F')
{
cout << "..-.";
}
else if (c == 'G')
{
cout << "--.";
}
else if (c == 'H')
{
cout << "....";
}
else if (c == 'I')
{
cout << "..";
}
else if (c == 'J')
{
cout << ".---";
}
else if (c == 'K')
{
cout << "-.-";
}
else if (c == 'L')
{
cout << ".-..";
}
else if (c == 'M')
{
cout << "--";
}
else if (c == 'N')
{
cout << "-.";
}
else if (c == 'O')
{
cout << "---";
}
else if (c == 'P')
{
cout << ".--.";
}
else if (c == 'Q')
{
cout << "--.-";
}
else if (c == 'R')
{
cout << ".-.";
}
else if (c == 'S')
{
cout << "...";
}
else if (c == 'T')
{
cout << "-";
}
else if (c == 'U')
{
cout << "..-";
}
else if (c == 'V')
{
cout << "...-";
}
else if (c == 'W')
{
cout << ".--";
}
else if (c == 'X')
{
cout << "-..-";
}
else if (c == 'Y')
{
cout << "-.--";
}
else if (c == 'Z')
{
cout << "--..";
}
else if (c == '1')
{
cout << ".----";
}
else if (c == '2')
{
cout << "..---";
}
else if (c == '3')
{
cout << "...--";
}
else if (c == '4')
{
cout << "....-";
}
else if (c == '5')
{
cout << ".....";
}
else if (c == '6')
{
cout << "-....";
}
else if (c == '7')
{
cout << "--...";
}
else if (c == '8')
{
cout << "---..";
}
else if (c == '9')
{
cout << "----.";
}
else if (c == '0')
{
cout << "-----";
}
else
{
cout << "#" ;
}
}

𝐯𝐨𝐢𝐝 𝐬𝐌𝐨𝐫𝐬𝐞 (𝐬𝐭𝐫𝐢𝐧𝐠 𝐬) {

𝐟𝐨𝐫 (𝐮𝐧𝐬𝐢𝐠𝐧𝐞𝐝 𝐢𝐧𝐭 𝐢=𝟎; 𝐢<𝐬.𝐥𝐞𝐧𝐠𝐭𝐡(); 𝐢++) {
𝐜𝐡𝐚(𝐬[𝐢]);
𝐬𝐌𝐨𝐫𝐬𝐞 = ' ';
}




}

int main()
{
string s, mSentence;

char A, I, N, P, S, c, menuChoice;

c=toupper(c);

cout << "Menu" << endl;

cout << "A - Alphabet" << endl;

cout << "I - Initials" << endl;

cout << "N - Numbers" << endl;

cout << "P - Punctuations" << endl;

cout << "S - User Sentence" << endl;

cout << "Q - Quit" << endl;

cout << "Enter command: ";
cin >> menuChoice;

if (menuChoice == A) {
cout << "Here are the letters of the alphabet (A-Z) in morse code: ";
}

else if (menuChoice == I) {
cout << "Here are the team member initials in morse code: ";
}

else if (menuChoice == N) {
cout << "Here are the numbers (0123456789) in morse code: ";
}

else if (menuChoice == P) {
cout << "Here are the punctuation marks (.,:?'-/\"@) in morse code: ";
}

𝐞𝐥𝐬𝐞 𝐢𝐟 (𝐦𝐞𝐧𝐮𝐂𝐡𝐨𝐢𝐜𝐞 == 𝐒) {

𝐜𝐨𝐮𝐭 << "𝐏𝐥𝐞𝐚𝐬𝐞 𝐞𝐧𝐭𝐞𝐫 𝐚 𝐬𝐞𝐧𝐭𝐞𝐧𝐜𝐞: ";

𝐠𝐞𝐭𝐥𝐢𝐧𝐞 (𝐜𝐢𝐧, 𝐬);

𝐜𝐨𝐮𝐭 << "𝐇𝐞𝐫𝐞 𝐢𝐬 𝐲𝐨𝐮𝐫 𝐬𝐞𝐧𝐭𝐞𝐧𝐜𝐞 𝐢𝐧 𝐦𝐨𝐫𝐬𝐞 𝐜𝐨𝐝𝐞: ";

𝐬 = 𝐬𝐌𝐨𝐫𝐬𝐞;

𝐜𝐨𝐮𝐭 << 𝐬𝐌𝐨𝐫𝐬𝐞;

}

else {
cout << "Program done!";
return 0;
}

}
Last edited on
void functions just do not return a value. that is all void means.
some void functions use a reference parameter to hold any results. Some do not have results to return (print functions, for example).

the difference:
void foo(int x);
int bar (int y);
..
z = bar(3); //bar returns values.
bar (4); //but you can ignore the return value and throw it away in c++
foo(4); //call a void function.
z = foo(4); //this is not meaningful, foo does not return a value.

in every other way it behaves exactly like any other function; if you want to read from user, use cin, if you want to write, use cout, etc. And it is parameter driven, as are non void functions, but be sure to pass in what it needs.
Topic archived. No new replies allowed.