Morsecode


  • Mod

    wella_black schrieb:

    lesen ist anscheinend nicht so meine stärke... ...
    aber so wie es jetzt ist sollte es doch funktionieren

    Nein, das sollte nicht funktionieren. Vergleiche deine Lösungen noch einmal mit den sehr konkreten Vorschlägen und Erklärungen hier im Thread. Wenn du einfach ignorierst, was man dir erklärt oder vormacht, dann kann man dir nicht helfen.



  • @ wella_black

    Nimm das char k raus (wozu brauchst Du das?) und dann eine for-Schleife aus den obigen Beispielen. zB

    for(size_t i = 0; i < text.size(); ++i)
    {
      if (text.at(i) == 'a' || text.at(i) == 'A') cout << ".-";
    


  • map<char, string> M = { { 'a', ".-" },
                              { 'b', "-..." },
                              { 'z', "--.." } };
      char c = 'b';
      cout << M[c] << endl;
    


  • hab es hinbekomm und will mich bedanken bei euch. das sieht jetzt so aus

    #include <iostream>
    #include <string>
    #include <windows.h>
    using namespace std;
    int main ()
    {
    system ("color f0");
    menue:
    cin.clear();
        cout <<endl;
        cout << " Willkommen ;) " << endl;
        cout << " was m\x94 \bchten sie tun ? W\x84 \bhlen sie die: "<<endl;
        cout <<endl;
        cout << " 1 f\x81 \br Text -> Morsecode "<<endl;
        cout << " 2 f\x81 \br Morsecode -> Text "<<endl;
        cout << " 3 f\x81 \br Bling bling"<<endl;
        cout << " 8 zum Beenden " <<endl;
        char j ='j';
        cin >> j;
    
    if (j=='1') {goto TtoMO;}//Text zu Morsecode
    if (j=='2') {goto MOtoT;}//Morse zu Text
    if (j=='3') {goto bling;}//Bling bling
    if (j=='8') {goto ende;}//ende
    //-----------------------------------------------------------------------------
    TtoMO: // Text to Morse
    cin.ignore();
    cin.clear();
    {
        cout <<endl;
        cout << " Text -> Morsecode:"<<endl;
        cout << " Geben Sie ihren Text ein und Beenden Sie ihren Satz mit einem Punkt.! " <<endl;
        cout <<endl;
        string k;           // k ist die variable für den einzugebenden text.
        getline(cin,k);     // zeile einlesen
        int i;              // zählvariable
        for     (i=0 ; i < k.length(); i++ ) // die vorschleife.
    {       if (k[i]=='a'||k[i]=='A') {cout << ".- ";}
            if (k[i]=='b'||k[i]=='B') {cout << "-... ";}
            if (k[i]=='c'||k[i]=='C') {cout << "-.-. ";}
            if (k[i]=='d'||k[i]=='D') {cout << "-.. ";}
            if (k[i]=='e'||k[i]=='E') {cout << ". ";}
            if (k[i]=='f'||k[i]=='F') {cout << "..-. ";}
            if (k[i]=='g'||k[i]=='G') {cout << "--. ";}
            if (k[i]=='h'||k[i]=='H') {cout << ".... ";}
            if (k[i]=='i'||k[i]=='I') {cout << ".. ";}
            if (k[i]=='j'||k[i]=='J') {cout << ".--- ";}
            if (k[i]=='k'||k[i]=='K') {cout << "-.- ";}
            if (k[i]=='l'||k[i]=='L') {cout << ".-.. ";}
            if (k[i]=='m'||k[i]=='M') {cout << "-- ";}
            if (k[i]=='n'||k[i]=='N') {cout << "-. ";}
            if (k[i]=='o'||k[i]=='O') {cout << "--- ";}
            if (k[i]=='p'||k[i]=='P') {cout << ".--. ";}
            if (k[i]=='q'||k[i]=='Q') {cout << "--.- ";}
            if (k[i]=='r'||k[i]=='R') {cout << ".-. ";}
            if (k[i]=='s'||k[i]=='S') {cout << "... ";}
            if (k[i]=='t'||k[i]=='T') {cout << "- ";}
            if (k[i]=='u'||k[i]=='U') {cout << "..- ";}
            if (k[i]=='v'||k[i]=='V') {cout << ".-- ";}
            if (k[i]=='w'||k[i]=='W') {cout << "...- ";}
            if (k[i]=='x'||k[i]=='X') {cout << "-..- ";}
            if (k[i]=='y'||k[i]=='Y') {cout << "-.-- ";}
            if (k[i]=='z'||k[i]=='Z') {cout << "--.. ";}
            if (k[i]==' ')            {cout <<" ";}
            if (k[i]=='.')            {cout << endl;}
    }
    system("pause");
    goto menue;
    }
     // Morsecode zu Text
    MOtoT:
    cin.ignore();
    cin.clear();
    {
    
            cout<<endl;
        cout << " Morsecode -> Text "<<endl;
        cout <<" Geben sie ihren Morsecode ein !"<<endl;
        cout <<" 1 f\x81 \br nochmal "<<endl;
        cout <<" 2 f\x81 \br Menue "<<endl;
        cout <<" 3 f\x81 \br Ende "<<endl;
        cout <<endl;
        string l;
        while (cin>>l)
        {
            if (l==".-") {cout << "a";}
            if (l=="-...") {cout << "b";}
            if (l=="-.-.") {cout << "c";}
            if (l=="-..") {cout << "d";}
            if (l==".") {cout << "e";}
            if (l=="..-.") {cout << "f";}
            if (l=="--.") {cout << "g";}
            if (l=="....") {cout << "h";}
            if (l=="..") {cout << "i";}
            if (l==".---") {cout << "j";}
            if (l=="-.-") {cout << "k";}
            if (l==".-..") {cout << "l";}
            if (l=="--") {cout << "m";}
            if (l=="-.") {cout << "n";}
            if (l=="---") {cout << "o";}
            if (l==".--.") {cout << "p";}
            if (l=="--.-") {cout << "q";}
            if (l==".-.") {cout << "r";}
            if (l=="...") {cout << "s";}
            if (l=="-") {cout << "t";}
            if (l=="..-") {cout << "u";}
            if (l=="...-") {cout << "v";}
            if (l==".--")  {cout << "w";}
            if (l=="-..-") {cout << "x";}
            if (l=="-.--") {cout << "y";}
            if (l=="--..") {cout << "z";}
            if (l==" ") {cout << " ";}
            if (l=="1") { goto MOtoT;}
            if (l=="2") {goto menue;}
            if (l=="3") {goto ende;}
        }
        system ("pause");
        goto menue;
    }
     // Bling bling
    bling:
    cin.ignore();
    cin.clear();
    {
      int k=1;
        cout<<endl;
        cout << "Bling bling geben sie den morsecode ein "<<endl;
        cout <<endl;
        string m;           // k ist die variable für den einzugebenden text.
    
        getline(cin,m);     // zeile einlesen
                     // zählvariable
        for   (int p=0;p < m.length();p++ )
        {
            if (m[p] =='.')
            {
                system ("color 0f");
                Sleep (k*1000);
                system ("color f0");
                Sleep(k*1000);
            }
            if (m[p]=='-')
            {
                system ("color 0f");
                Sleep (k*3000);
                system ("color f0");
                Sleep(k*1000);
            }
            if (m[p]==' ')
            {
                system("color f0");
                Sleep  (k*3000);
            }
        }
    system ("pause");
    goto menue;
    }
    ende: // ende
    system ("pause");
    return 0;
    }
    

    Danke 😉



  • Es mag ja sein das dein Programm so funktioniert.

    Aber;
    Spaghetti-Code in C++ sollte nicht sein. Teile den Quelltext in Funktionen auf.
    Kennst du auch noch andere Schleifen als mit "goto-Label" dann nutze die.
    Die Quelltest-Formatierung kann auch noch übersichtlicher werden.



  • und die Kommentierung:

    string m;           // k ist die variable für den einzugebenden text.
    

Anmelden zum Antworten