Verbesserungsvorschläge :Taschenrechner^^?



  • also , ich weiß nicht mehr wirklich was ich noch dran machen soll 😉
    dann mal los vorschläge:ich weiß ist klein ,aber bin ja auch neu 😉

    //Übungsbeispiel Taschenrechner
    //mit den Grundlagen:
    //->if-Schleife<-
    //->do-while-Schleife<-
    //->Menü<-
    //->Variablen<-
    #include <iostream>
    
    using namespace std;
    //void Berechnung(int Ergebnis);
    int main()
    
    {
    	//cout<<"Taschenrechner"<<endl;
    	int Auswahl =0;
    	do
    	{
    	cout<<"|.:Taschenrechner-Menue:. |"<<endl;
    	cout<<"|-------------------------|"<<endl;
    	cout<<"|1. Multiplikation        |"<<endl;
    	cout<<"|2. Division              |"<<endl;
    	cout<<"|3. Addition              |"<<endl;
    	cout<<"|4. Subtraktion           |"<<endl;
    	cout<<"|5. Beenden               |"<<endl;
    	cout<<"|6. Info                  |"<<endl;
    	cout<<"|-------------------------|"<<endl;
    	cin>>Auswahl;
    	}
    	while (Auswahl !=5)    
    
    		if(Auswahl==1)
    		{
            int fZahl;//mal
    		int fFaktor;//mal
    		int fErgebnis;//mal
            cout<<"\nHallo,";
    		cout<<"Dies ist der Taschenrechner fuer Multiplikation\n";
    		cout<<"\n1.Wert: \n"<<endl;
    		cin>>fZahl;
    		cout<<"Welchen Faktor:\n"<<endl;
    		cin>>fFaktor;
    		fErgebnis = fZahl*fFaktor;
    		cout<<"\n\nDas Ergebnis ist: "<<fErgebnis<<endl; 
    		}
            else if(Auswahl ==2)
    		{
            int fZahl1;//geteilt
    		int fDivisor;//geteilt
    		int fErgebnis1;//geteilt
    
    		cout<<"\nHallo,";
    		cout<<"Dies ist der Taschenrechner fuer Division\n";
    		cout<<"\n1.Zahl: \n"<<endl;
    		cin>>fZahl1;
    		cout<<"Welchen Divisor:\n"<<endl;
    		cin>>fDivisor;
    		fErgebnis1 = fZahl1/fDivisor;
    		cout<<"\n\nDas Ergebnis ist: "<<fErgebnis1<<endl;
    		}
    		else if(Auswahl ==3)
    		{
            int fZahl12=0;//plus
    		int fZahl2=0;//plus
    		int fErgebnis2=0;//plus
    		cout<<"\nHallo,";
    		cout<<"Dies ist der Taschenrechner fuer Addition\n";
    		cout<<"\n1.Zahl: \n"<<endl;
    		cin>>fZahl12;
    		cout<<"2.Zahl:\n"<<endl;
    		cin>>fZahl2;
    		fErgebnis2 = fZahl2+fZahl12;
    		cout<<"\n\nDas Ergebnis ist: "<<fErgebnis2<<endl;
    		}
    		else if(Auswahl ==4)
    		{
            int fErgebnis3;//minus
            int fZahl123;//minus
    		int fZahl3;//minus
    		cout<<"\nHallo,";
    		cout<<"Dies ist der Taschenrechner fuer Subtraktion\n";
    		cout<<"\n1.Zahl: \n"<<endl;
    		cin>>fZahl123;
    		cout<<"2.Zahl:\n"<<endl;
    		cin>>fZahl3;
    		fErgebnis3 = fZahl123-fZahl3;
    		cout<<"\n\nDas Ergebnis ist: "<<fErgebnis3<<endl;
    		}
    		else if(Auswahl ==5)
    		{
               cout<<"Spiel wird beendet"<<endl;
    		}
    		else if(Auswahl ==6)
    		{
    		char Info[65] = "Das ist mein Taschenrechner\n\n(c)copyright by Kevin888 aka Stylemaker43";
    		cout<<"\n"<<Info<<endl;
    
    		}
    		else
    		cout<<"Falsche Eingabe!"<<endl;
    	   cout<<"\n\n\n"<<endl;
    
         }
    
       return 0;
     }
    


  • Schau dir Funktionen an.



  • Schau dir die Regeln für Quelltextformatierung an.
    Schau dir die Regeln für Variablenbenennung an.



  • ---- schrieb:

    Schau dir Funktionen an.

    und switch evtl.



  • wenn er hier schon mit guten tips erschlagen wird... setz in deinem if() die zahl nach vorne und die variable nach hinten:

    aus

    if(Auswahl==1)
    

    wird dann

    if(1==Auswahl)
    


  • Igitt, mach das nicht.
    Zweiteres ist unlesbarer, der Fehler passiert mir nie und wenn doch, dann warnt mich der Compiler.
    Diese Syntax verwenden nur einige wenige hinterbliebene C/C++-Programmierer. Weder gute C++, noch C Programmierer machen das.

    Und diese Zeile ist peinlich:

    char Info[65] = "Das ist mein Taschenrechner\n\n(c)copyright by Kevin888 aka Stylemaker43";
    


  • esthete schrieb:

    Weder gute C++, noch C Programmierer machen das.

    willst du damit sagen, dass Tavis Ormandy ein schlechter c/c++ programmierer ist? bilde mir ein in seinem code hab ich mal sowas gesehen :p



  • was mache ich falsch->

    //Übungsbeispiel Taschenrechner
    //mit den Grundlagen:
    //->if-Schleife<-
    //->do-while-Schleife<-
    //->Menü<-
    //->Variablen<-
    #include <iostream>
    
    using namespace std;
    void Plus(*pErgebnis2, *pZahl12, *pZahl2);
    int main()
    
    {
    	//cout<<"Taschenrechner"<<endl;
    	int Auswahl =0;
    	do
    	{
    	cout<<"|.:Taschenrechner-Menue:. |"<<endl;
    	cout<<"|-------------------------|"<<endl;
    	cout<<"|1. Multiplikation        |"<<endl;
    	cout<<"|2. Division              |"<<endl;
    	cout<<"|3. Addition              |"<<endl;
    	cout<<"|4. Subtraktion           |"<<endl;
    	cout<<"|5. Beenden               |"<<endl;
    	cout<<"|6. Info                  |"<<endl;
    	cout<<"|-------------------------|"<<endl;
    	cin>>Auswahl;
    	}
    	while (Auswahl !=5)    
    
    		if(Auswahl==1)
    		{
            int fZahl;//mal
    		int fFaktor;//mal
    		int fErgebnis;//mal
            cout<<"\nHallo,";
    		cout<<"Dies ist der Taschenrechner fuer Multiplikation\n";
    		cout<<"\n1.Wert: \n"<<endl;
    		cin>>fZahl;
    		cout<<"Welchen Faktor:\n"<<endl;
    		cin>>fFaktor;
    		fErgebnis = fZahl*fFaktor;
    		cout<<"\n\nDas Ergebnis ist: "<<fErgebnis<<endl; 
    		}
            else if(Auswahl ==2)
    		{
            int fZahl1;//geteilt
    		int fDivisor;//geteilt
    		int fErgebnis1;//geteilt
    
    		cout<<"\nHallo,";
    		cout<<"Dies ist der Taschenrechner fuer Division\n";
    		cout<<"\n1.Zahl: \n"<<endl;
    		cin>>fZahl1;
    		cout<<"Welchen Divisor:\n"<<endl;
    		cin>>fDivisor;
    		fErgebnis1 = fZahl1/fDivisor;
    		cout<<"\n\nDas Ergebnis ist: "<<fErgebnis1<<endl;
    		}
    		else if(Auswahl ==3)
    		{
            int Zahl12=0;//plus
    		int Zahl2=0;//plus
    		int Ergebnis2=0;//plus
            int *pZahl12 = NULL;
    		pZahl12 = &Zahl12;
            int *pErgebnis2 = NULL;
    		pErgebnis2 = &Ergebnis2;
            int *pZahl2 = NULL;
    		pZahl2 = &Zahl2;
    
    		Plus (*pErgebnis2, *pZahl12, *pZahl2);
    		}
    		else if(Auswahl ==4)
    		{
            int fErgebnis3;//minus
            int fZahl123;//minus
    		int fZahl3;//minus
    		cout<<"\nHallo,";
    		cout<<"Dies ist der Taschenrechner fuer Subtraktion\n";
    		cout<<"\n1.Zahl: \n"<<endl;
    		cin>>fZahl123;
    		cout<<"2.Zahl:\n"<<endl;
    		cin>>fZahl3;
    		fErgebnis3 = fZahl123-fZahl3;
    		cout<<"\n\nDas Ergebnis ist: "<<fErgebnis3<<endl;
    		}
    		else if(Auswahl ==5)
    		{
               cout<<"Spiel wird beendet"<<endl;
    		}
    		else if(Auswahl ==6)
    		{
    		char Info[65] = "Das ist ein Taschenrechner\n\n";
    		cout<<"\n"<<Info<<endl;
    
    		}
    		else
    		cout<<"Falsche Eingabe!"<<endl;
    	   cout<<"\n\n\n"<<endl;
    
         }
    		void Plus(*pErgebnis2, *pZahl12, *pZahl2);
    		{
    			cout<<"Das ist der Taschenrechner fuer rAddition\n";
    			cout<<"Erste Zahl: ";
    			cin>>pZahl12;
    			cout<<"Zweite Zahl: ";
    			cin>>pZahl2;
    			*pErgebis2= *pZahl12+*pZahl2;
    			cout<<"Ergebnis: "<<pErgebnis2;
    			cout<<endl;
    		}
       return 0;
     }´
    


  • __-- schrieb:

    esthete schrieb:

    Weder gute C++, noch C Programmierer machen das.

    willst du damit sagen, dass Tavis Ormandy ein schlechter c/c++ programmierer ist? bilde mir ein in seinem code hab ich mal sowas gesehen :p

    oO da hab ich mich getäuscht, grad nochmal nachgeschaut...



  • habs jez so jjaa funktionen switch case kann ich ja aber hab ich hier net gemacht vorher 😉

    switch(Auswahl)
    	{
    
    		case('1')
    		{
            int fZahl;//mal
    		int fFaktor;//mal
    		int fErgebnis;//mal
            cout<<"\nHallo,";
    		cout<<"Dies ist der Taschenrechner fuer Multiplikation\n";
    		cout<<"\n1.Wert: \n"<<endl;
    		cin>>fZahl;
    		cout<<"Welchen Faktor:\n"<<endl;
    		cin>>fFaktor;
    		fErgebnis = fZahl*fFaktor;
    		cout<<"\n\nDas Ergebnis ist: "<<fErgebnis<<endl; 
    		}break;
    
            case('2')
    		{
            int fZahl1;//geteilt
    		int fDivisor;//geteilt
    		int fErgebnis1;//geteilt
    
    		cout<<"\nHallo,";
    		cout<<"Dies ist der Taschenrechner fuer Division\n";
    		cout<<"\n1.Zahl: \n"<<endl;
    		cin>>fZahl1;
    		cout<<"Welchen Divisor:\n"<<endl;
    		cin>>fDivisor;
    		fErgebnis1 = fZahl1/fDivisor;
    		cout<<"\n\nDas Ergebnis ist: "<<fErgebnis1<<endl;
    		}break;
    
    		case('3')
    		{
            int Zahl12=0;//plus
    		int Zahl2=0;//plus
    		int Ergebnis2=0;//plus
            cout<<"\nHallo,";
    		cout<<"Dies ist der Taschenrechner fuer Addition\n";
    		cout<<"\n1.Zahl: \n"<<endl;
    		cin>>Zahl12;
    		cout<<"2.Zahl:\n"<<endl;
    		cin>>Zahl2;
    		Ergebnis2 = Zahl12+fZahl2;
    		cout<<"\n\nDas Ergebnis ist: "<<Ergebnis2<<endl;
    		}break;
    
    		case('4')
    		{
            int fErgebnis3;//minus
            int fZahl123;//minus
    		int fZahl3;//minus
    		cout<<"\nHallo,";
    		cout<<"Dies ist der Taschenrechner fuer Subtraktion\n";
    		cout<<"\n1.Zahl: \n"<<endl;
    		cin>>fZahl123;
    		cout<<"2.Zahl:\n"<<endl;
    		cin>>fZahl3;
    		fErgebnis3 = fZahl123-fZahl3;
    		cout<<"\n\nDas Ergebnis ist: "<<fErgebnis3<<endl;
    		}break;
    
    		case('5')
    		{
               cout<<"Spiel wird beendet"<<endl;
    		}break;
    
    		case('6')
    		{
    		char Info[65] = "Das ist mein Taschenrechner\n\n(c)copyright by Kevin888 aka Stylemaker43";
    		cout<<"\n"<<Info<<endl;
    		}break;
    
    		default:
    			{
    		cout<<"Falsche Eingabe!"<<endl;
    	   cout<<"\n\n\n"<<endl;
    
         }
    
       return 0;
     }
    


  • äähm soo^^:

    //Übungsbeispiel Taschenrechner
    //mit den Grundlagen:
    //->if-Schleife<-
    //->do-while-Schleife<-
    //->Menü<-
    //->Variablen<-
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
    	//cout<<"Taschenrechner"<<endl;
    	char Auswahl;
    	do
    	{
    	cout<<"|.:Taschenrechner-Menue:. |"<<endl;
    	cout<<"|-------------------------|"<<endl;
    	cout<<"|(M)ultiplikation         |"<<endl;
    	cout<<"|(D)ivision               |"<<endl;
    	cout<<"|(A)ddition               |"<<endl;
    	cout<<"|(S)ubtraktion            |"<<endl;
    	cout<<"|(B)eenden                |"<<endl;
    	cout<<"|(I)nfo                   |"<<endl;
    	cout<<"|-------------------------|"<<endl;
    	cin>>Auswahl;
    	}
    	while (Auswahl !=5);   
    
    	  switch(Auswahl)
    	{
    
    	    case('M'):
    		case('m'):
    		{
            int fZahl;//mal
    		int fFaktor;//mal
    		int fErgebnis;//mal
            cout<<"\nHallo,";
    		cout<<"Dies ist der Taschenrechner fuer Multiplikation\n";
    		cout<<"\n1.Wert: \n"<<endl;
    		cin>>fZahl;
    		cout<<"Welchen Faktor:\n"<<endl;
    		cin>>fFaktor;
    		fErgebnis = fZahl*fFaktor;
    		cout<<"\n\nDas Ergebnis ist: "<<fErgebnis<<endl; 
    		}break;
    
    		case('D'):
    		case('d'):
    		{
            int fZahl1;//geteilt
    		int fDivisor;//geteilt
    		int fErgebnis1;//geteilt
    
    		cout<<"\nHallo,";
    		cout<<"Dies ist der Taschenrechner fuer Division\n";
    		cout<<"\n1.Zahl: \n"<<endl;
    		cin>>fZahl1;
    		cout<<"Welchen Divisor:\n"<<endl;
    		cin>>fDivisor;
    		fErgebnis1 = fZahl1/fDivisor;
    		cout<<"\n\nDas Ergebnis ist: "<<fErgebnis1<<endl;
    		}break;
    
    		case('A'):
    		case('a'):
    		{
            int Zahl12=0;//plus
    		int Zahl2=0;//plus
    		int Ergebnis2=0;//plus
            cout<<"\nHallo,";
    		cout<<"Dies ist der Taschenrechner fuer Addition\n";
    		cout<<"\n1.Zahl: \n"<<endl;
    		cin>>Zahl12;
    		cout<<"2.Zahl:\n"<<endl;
    		cin>>Zahl2;
    		Ergebnis2 = Zahl12+Zahl2;
    		cout<<"\n\nDas Ergebnis ist: "<<Ergebnis2<<endl;
    		}break;
    
    		case('S'):
    		case('s'):
    		{
            int fErgebnis3;//minus
            int fZahl123;//minus
    		int fZahl3;//minus
    		cout<<"\nHallo,";
    		cout<<"Dies ist der Taschenrechner fuer Subtraktion\n";
    		cout<<"\n1.Zahl: \n"<<endl;
    		cin>>fZahl123;
    		cout<<"2.Zahl:\n"<<endl;
    		cin>>fZahl3;
    		fErgebnis3 = fZahl123-fZahl3;
    		cout<<"\n\nDas Ergebnis ist: "<<fErgebnis3<<endl;
    		}break;
    
    		case('B'):
    		case('b'):
    		{
               cout<<"Spiel wird beendet"<<endl;
    		}break;
    
    		case('I'):
    		case('i'):
    		{
    		char Info[65] = "Das ist mein Taschenrechner\n\n";
    		cout<<"\n"<<Info<<endl;
    		}break;
    
    		default:
    	{
    	cout<<"Falsche Eingabe!"<<endl;
    	cout<<"\n\n\n"<<endl;
    
    	}
    	}
    
       return 0;
     }
    


  • Die deutsche Sprache ist verbesserungwuerdig.



  • Die Idee mit switch-case finde ich fragwürdig, aber ich habe einmal den gesamten Code formatiert (und nur das, es lässt sich extrem viel verbessern):

    //Übungsbeispiel Taschenrechner
    //mit den Grundlagen:
    //->if-Schleife<-
    //->do-while-Schleife<-
    //->Menü<-
    //->Variablen<-
    #include <iostream>
    
    int main()
    {
      char Auswahl;
      do {
        std::cout <<
          "|.:Taschenrechner-Menue:. |\n"
          "|-------------------------|\n"
          "|(M)ultiplikation         |\n"
          "|(D)ivision               |\n"
          "|(A)ddition               |\n"
          "|(S)ubtraktion            |\n"
          "|(B)eenden                |\n"
          "|(I)nfo                   |\n"
          "|-------------------------|\n";
      } while (!(std::cin >> Auswahl));
    
      switch (Auswahl) {
      case 'M':
      case 'm': {
        int zahl, faktor;
        std::cout <<
          "\nHallo, dies ist der Taschenrechner fuer Multiplikation\n"
          "\n1.Wert:\n" << std::endl;
        std::cin >> zahl;
        std::cout << "Welchen Faktor:\n" << std::endl;
        std::cin >> faktor;
        int ergebnis = zahl * faktor;
        std::cout << "\n\nDas Ergebnis ist: " << ergebnis << std::endl;
        break;
      }
      case 'D':
      case 'd': {
        int zahl, divisor;
        std::cout <<
          "\nHallo, dies ist der Taschenrechner fuer Division\n"
          "\n1.Zahl:\n" << std::endl;
        std::cin >> zahl;
        std::cout << "Welchen Divisor:\n" << std::endl;
        std::cin >> divisor;
        int ergebnis = zahl / divisor;
        std::cout << "\n\nDas Ergebnis ist: " << ergebnis << std::endl;
        break;
      }
      case 'A':
      case 'a': {
        int zahl, summand;
        std::cout <<
          "\nHallo, dies ist der Taschenrechner fuer Addition\n"
          "\n1.Zahl:\n" << std::endl;
        std::cin >> zahl;
        std::cout << "2.Zahl:\n" << std::endl;
        std::cin >> summand;
        int ergebnis = zahl + summand;
        std::cout << "\n\nDas Ergebnis ist: " << ergebnis << std::endl;
        break;
      }
      case 'S':
      case 's': {
        int zahl, subtrahend;
        std::cout <<
          "\nHallo, dies ist der Taschenrechner fuer Subtraktion\n"
          "\n1.Zahl: \n" << std::endl;
        std::cin >> zahl;
        std::cout << "2.Zahl:\n" << std::endl;
        std::cin >> subtrahend;
        int ergebnis = zahl - subtrahend;
        std::cout << "\n\nDas Ergebnis ist: " << ergebnis << std::endl;
        break;
      }
      case 'B':
      case 'b':
        std::cout << "Spiel wird beendet" << std::endl;
        break;
      case 'I':
      case 'i':
        std::cout << "\nDas ist mein Taschenrechner\n\n" << std::endl;
        break;
      default:
        std::cout << "Falsche Eingabe!\n\n\n\n" << std::endl;
      }
    }
    

Anmelden zum Antworten