Frage wiederholen?



  • Hallo zusammen.

    Ich habe folgendes Problem. Und zawr möchte ich ein Programm schreiben beidem man auf die erste Frage mit Ja oder Nein antworten muss. Ich würde die Frage gerne 10 mal wiederholen lassen. (for schleife??)

    #include <iostream>
    using namespace std;
    
    char antwort = ' ';
    
    int main()
    
    { 
    	cout << "Herzlich willkommen ";
    	cout << "\nWillst du nochmal begruesst werden? (j/n)";
    	cout << "\nAntwort: ";
    	cin >> antwort;
    
    	if (antwort == 'j')
    	{
    		cout << "\nHerzlich willkommen"; 
    	return 0;
    	}	
    	else (antwort == 'n');
    	{
    		cout << "\nDann nicht ;)\n";
    	}
    }
    

    das alles unter int main() würde ich gerne 10 mal wiederholen. am besten mit einer schleife.
    und warum ist das Feld ' ' hinter char leer? könnte ich nicht einfach char ja, nein; anwenden?

    ich hoffe ich versteht was ich meine. vielen dank für eure hilfe.



  • Addicted schrieb:

    das alles unter int main() würde ich gerne 10 mal wiederholen. am besten mit einer schleife.

    und wo ist das problem?

    Addicted schrieb:

    und warum ist das Feld ' ' hinter char leer? könnte ich nicht einfach char ja, nein; anwenden?

    weil du das so programmiert hast. das leere feld ist ein leerzeichen. völlig wurst was dort zu programmstart drin steht.



  • #include <string>
    #include <iostream>
    
    int main()
    {
    	using namespace std;
    
    	cout << "Herzlich willkommen ";
    
    	for( int i=0; i < 10; ++i)
    	{
    		cout << "\nWillst du nochmal begruesst werden? (j/n)";
        	cout << "\nAntwort: "; 
    		string antwort;
    		cin >> antwort;
    
    		if( antwort == "ja" || antwort == "j" || antwort == "J" || antwort == "JA" )
    		{
    			cout << "Herzlich willkommen ";
    		}
    		else
    		{
    			break;
    		}
    	}
    }
    


  • rofltofl schrieb:

    #include <string>
    #include <iostream>
    
    int main()
    {
    	using namespace std;
    
    	cout << "Herzlich willkommen ";
    
    	for( int i=0; i < 10; ++i)
    	{
    		cout << "\nWillst du nochmal begruesst werden? (j/n)";
        	cout << "\nAntwort: "; 
    		string antwort;
    		cin >> antwort;
    
    		if( antwort == "ja" || antwort == "j" || antwort == "J" || antwort == "JA" )
    		{
    			cout << "Herzlich willkommen ";
    		}
    		else
    		{
    			break;
    		}
    	}
    }
    

    🙄

    #include <string>
    #include <iostream>
    
    int main()
    {
        using namespace std;
    	setlocale(LC_ALL, "german");
    
        cout << "Hi!" << endl;
    
        for( int i=0; i==9?i=0:0,i<10; i++ )
        {
            cout << "\nWillst du nochmal begruesst werden? (j/n)";
            cout << "\nAntwort: ";
            string antwort;
            cin >> antwort;
    
            if( antwort == "j" )
            {
                cout << "Äääääääätsch!!!" << endl;
            }
            else if ( antwort == "n" )
            {
                cout << "Halllooooo " << "hahahaha!" << endl;
            }
        }
    	return 4711;
    }
    


  • edit:

    #include <string>
    #include <iostream>
    
    int main()
    {
        using namespace std;
    	setlocale(LC_ALL, "german");
    
        cout << "Hi!" << endl;
    
        for( int i=0; i==9?i=0:0,i<10; i++ )
        {
            cout << "\nWillst du nochmal begruesst werden? (j/n)";
            cout << "\nAntwort: ";
            string antwort;
            cin >> antwort;
    
            if( antwort == "j" )
    			cout << "Äääääääätsch!!!" << endl;
            else if ( antwort == "n" )
    			cout << "Halllooooo " << "hahahaha!" << endl;
            else
    			cout << "Zu doof zum Tippen der Herr?!" << endl;
        }
    	return 4711;
    }
    

    🙄



  • lam0r detect0r(active) schrieb:

    edit:

    #include <string>
    #include <iostream>
    
    int main()
    {
        using namespace std;
    	setlocale(LC_ALL, "german");
    
        cout << "Hi!" << endl;
    
        for( int i=0; i==9?i=0:0,i<10; i++ )
        {
            cout << "\nWillst du nochmal begruesst werden? (j/n)";
            cout << "\nAntwort: ";
            string antwort;
            cin >> antwort;
    
            if( antwort == "j" )
    			cout << "Äääääääätsch!!!" << endl;
            else if ( antwort == "n" )
    			cout << "Halllooooo " << "hahahaha!" << endl;
            else
    			cout << "Zu doof zum Tippen der Herr?!" << endl;
        }
    	return 4711;
    }
    

    🙄

    Sinn? 😮



  • cvcv schrieb:

    Sinn? 😮

    💡 ja 💡

    🙂



  • Wo sind die Postingwehrmänner des Forums...



  • danke das hilft mir schonmal. aber kannst du mir verraten warum man hier string braucht und iostream allein nicht mehr ausreichend ist?



  • Addicted schrieb:

    danke das hilft mir schonmal. aber kannst du mir verraten warum man hier string braucht und iostream allein nicht mehr ausreichend ist?

    Weil antwort nun eine Variable vom Typ string ist.
    char kann ja nur 1 Zeichen speichern - wenn dir das reicht ist es OK antwort wieder zu einem char zu machen und bei den Vergleichen statt mit "j" mit 'j' zu vergleichen (wie du es ja urspruenglich hattest).

    string ermoeglicht dir aber mehrere Zeichen zu speichern. So dass der User zB auch "ja" oder "nein" schreiben kann.


Anmelden zum Antworten