Morsecode



  • hab ich. ich bekomme dann 53 fehler angezeigt



  • Da dein Feld morse ein char*[] ist musst du hier eine andere Vergleichsoperation verwenden. Hier hilft dir strcmp. Besser ist es aber, du verwendest gleich ein Array auf string.

    if (strcmp(eingabe,morse[i]) == 0)
            cout << alpha[i];
    

    Vorausgesetzt, dass eingabe den Morsecode für ein Zeichen enthält.



  • hab zurzeit auch dieses morseprojekt auf. hab da auch schon paar zeilen zusammen aber bei mir hängt es mit dem logischen verständniss bzw. schon beim grundwissen.
    [code="cpp"]
    mein text sieht so aus:
    #include <iostream>
    #include <cstdlib>
    #include <windows.h>
    #include <conio.h>
    #include <string.h>
    using namespace std;
    int main ()
    {
    system ("color f0");

    cout << "a oder b ";
    char k='k';
    cin>>k;
    string text;
    cin >>text;
    getline(cin,text);
    for(k=0;k<text;k++)
    {
    if (k=='a'||k=='A') {cout << ".- ";}
    if (k=='b'||k=='B') {cout << "-... ";}
    if (k=='c'||k=='C') {cout << "-.-. ";}
    if (k=='d'||k=='D') {cout << "-.. ";}
    if (k=='e'||k=='E') cout << ". "<<endl;
    if (k=='f'||k=='F') cout << "..-. "<<endl;
    if (k=='g'||k=='G') cout << "--."<<endl;
    if (k=='h'||k=='H') cout << "...."<<endl;
    if (k=='i'||k=='I') cout << ".."<<endl;
    if (k=='j'||k=='J') cout << ".---"<<endl;
    if (k=='k'||k=='K') cout << "-.-"<<endl;
    if (k=='l'||k=='L') cout << ".-.."<<endl;

    verurteilt mich bitte nicht gleich deswegen ....
    mein genaues problem liegt derzeit bei dem for()
    der fehler heißt "no match for 'operand<' in 'k<text"



  • Wenn du c++ programmieren möchtest, warum die Header aus C? Versuche ohne diese auszukommen. Wenn du sie doch brauchst, überlege gibt es keinen eleganteren C++ Weg.

    wella_black schrieb:

    #include <iostream>
    #include <cstdlib>		// es kann ja sein das du diesen und die folgenden Header brauchst, wenn nicht weg damit
    #include <windows.h>
    #include <conio.h>
    #include <string.h>
    using namespace std;
    
    int main ()
    {
    	system ("color f0");
    
    	cout << "a oder b ";	
    	char k='k';
    	cin>>k;
    	string text;		// hier sagst du text soll ein String sein
    	cin >>text;
    	getline(cin,text);
    	for(k=0;k<text;k++)	// Was soll text hier sein???
    	{
    		if (k=='a'||k=='A') {cout << ".- ";}
    		if (k=='b'||k=='B') {cout << "-... ";}
    

    Für bunten Quelltext hast du den "Schlußcode" vergessen. Oder den ganzen Quelltext markieren und dann auf den C++ Button klicken.



  • mit dem "text" in for ()
    hatte ich gehofft das es k solange übersetzt solange es kleiner ist als das textende.
    ich weiß einfach nicht wie ich in dem for () sage wie lange es übersetzten soll.


  • Mod

    wella_black schrieb:

    mit dem "text" in for ()
    hatte ich gehofft das es k solange übersetzt solange es kleiner ist als das textende.
    ich weiß einfach nicht wie ich in dem for () sage wie lange es übersetzten soll.

    Entweder zählst du Indizes:

    for(size_t k = 0; k < text.size(); ++k)
    {
      verarbeite(text[k]);
    }
    

    Oder du nutzt Iteratoren:

    for (std::string::const_iterator it=text.begin(); it!=text.end(); ++it)
    {
      verarbeite(*it);
    }
    

    Oder am allereinfachsten:

    for(auto k : text)
    {
      verarbeite(k);
    }
    

    Oder eine von 1000 anderen Möglichkeiten. Warum dein jetziger Ansatz Quatsch ist, ist dir klar? Du vergleichst Buchstaben mit Zeichenketten und deine Buchstaben haben nicht einmal etwas mit der Zeichenkette zu tun.



  • ja, das irgendwas daran nicht stimmen kann weiß ich.
    sizeof (text) müsste ja so viel heißen wie größe von text.

    also compilern macht es erstmal. . . aber nicht übersetzen.
    hab auch die bibliotheken rausgenommen die ich nicht mehr brauch. . .
    aber irgendwo muss ja immernoch ein riesen fehler sein...

    #include <iostream>
    #include <cstdlib>
    #include <string>
    using namespace std;
    int main ()
    {
      system ("color f0");
        cout << "geben sie ihren text ein !  ";
        char k='k';
        cin>>k;
        string text;
        cin >>text;
        getline(cin,text);
        for(k=0;k<sizeof(text);k++)
        {
            if (k=='a'||k=='A') cout << ".- ";
    


  • wella_black schrieb:

    cout << "geben sie ihren text ein !  ";
        char k='k';
        cin>>k;         // du willst hier ein Zeichen eingeben?
        string text;
        cin >>text;     // und hier einen Text
        getline(cin,text);  // und hier noch was
        for(k=0;k<sizeof(text);k++)
        {
            if (k=='a'||k=='A') cout << ".- ";
    

    Vielleicht solltest du dich auf eine Eingabe hier erst mal beschränken. Wähle aber die für diese Aufgabe richtige.


  • Mod

    wella_black schrieb:

    ja, das irgendwas daran nicht stimmen kann weiß ich.
    sizeof (text) müsste ja so viel heißen wie größe von text.

    Liest du überhaupt mit? Wer hat denn irgendwas von sizeof gesagt? sizeof hat hier überhaupt gar nichts zu suchen, das hat eine ganz andere Bedeutung.



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

    #include <iostream>
    #include <cstdlib>
    #include <string>
    using namespace std;
    int main ()
    {
      system ("color f0");
        cout << "geben sie ihren text ein !  " <<endl;
        char k='k';  // hier muss ich doch k deklarieren ?! 
        string text;
        getline(cin,text); // hier liest es alles ein 
        //cout << text; // hier kann es auch wieder alles ausgeben 
        for(k=0;k<text.length()-1;k++) // und das ist die bedingung
            if (k=='a'||k=='A') cout << ".-";
    

    compiler sagt auch nix aber übersetzen will es nicht -.-


  • 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