Problem Abfrage ob 1 oder 0 eingegeben wurde , C++



  • Die liebe Jessie trollt, Beweis:

    Sie hat swordsfishs code geändert das es nicht klappen kann. Ich bin raus

    bool is_valid_binary(std::string const&str)
    {
    	for(auto ch : str)
    		if(ch != '1' || ch != '0')
    		return false; 
    	return true;
    }
    

    Edit: ist ihr eingefügter code, original hat 2 &



  • @dirkski

    // Copyright (C) 2007-2014 Free Software Foundation, Inc.
    //
    // This file is part of the GNU ISO C++ Library.  This library is free
    // software; you can redistribute it and/or modify it under the
    // terms of the GNU General Public License as published by the
    // Free Software Foundation; either version 3, or (at your option)
    // any later version.
    
    // This library is distributed in the hope that it will be useful,
    // but WITHOUT ANY WARRANTY; without even the implied warranty of
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    // GNU General Public License for more details.
    
    // Under Section 7 of GPL version 3, you are granted additional
    // permissions described in the GCC Runtime Library Exception, version
    // 3.1, as published by the Free Software Foundation.
    
    // You should have received a copy of the GNU General Public License and
    // a copy of the GCC Runtime Library Exception along with this program;
    // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
    // <http://www.gnu.org/licenses/>.
    
    /** @file bits/c++0x_warning.h
     *  This is an internal header file, included by other library headers.
     *  Do not attempt to use it directly. @headername{iosfwd}
     */
    
    #ifndef _CXX0X_WARNING_H
    #define _CXX0X_WARNING_H 1
    
    #if __cplusplus < 201103L
    #error This file requires compiler and library support for the \
    ISO C++ 2011 standard. This support is currently experimental, and must be \
    enabled with the -std=c++11 or -std=gnu++11 compiler options.
    #endif
    
    #endif
    


  • @dirkski ja er soll auch nicht überprüfen ob beides drin ist sondern mind einer..

    auch mit dem & funktioniert das nicht



  • @diejessi
    Für alle die es interessiert, hab meinen Fehler gefunden... hier ist der richtige Code:

    #include <iostream>
    #include <cstdlib>
    #include <iomanip>
    
    using namespace std; 
    
    int main(int argc, char** argv) 
     {
    	string bin; 
    
    	cout << "BIN: " <<endl; 
    	cin >> bin; 
    	
    	int laenge = bin.length();
    	
    	while(laenge > 0 )
    	{
    		laenge = laenge -1;
    		if(bin[laenge] == '1' || bin[laenge] == '0')
    		{
    			cout << "r";
    				
    		}
    		else
    		{
    			cout << "f";
    			laenge = 0; 		
    		}
    	}
    	return 0;
    }
    

    hab nur eine Sache getauscht.. xD



  • Das schreit doch förmlich nach einer for-Schleife.
    laenge = 0; ~> break;.
    std::string::length() gibt keinen int zurück.
    Die Parameter für main() und das return 0; am Ende sind überflüssig.
    <string> fehlt, <cstdlib> und <iomanip> brauchst du nicht.

    //edit: Eigentlich schreit's nach einer range-based-for:

    #include <iostream>
    #include <string>
    
    int main()
    {
        std::string bin;
        std::cin >> bin;
        for (auto ch : bin) {
            if (ch == '0' || ch == '1') {
                std::cout.put('r');
            } else {
                std::cout.put('f');
                break;
            }
        }
        std::cout.put('\n');
    }
    

  • Gesperrt

    Noch eine Möglichkeit:

    #include <algorithm>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string bin;
        cin >> bin;
    
        bin.erase(remove_if(bin.begin(),bin.end(),[](char x){return x=='0'||x=='1';}),bin.end());
        cout << (bin.empty() ? "gute binärzahl!" : "falsche zeichen mit drinne!") << endl;
    }
    


  • @RBS2 sagte in Problem Abfrage ob 1 oder 0 eingegeben wurde , C++:

    bin.erase(remove_if(bin.begin(),bin.end(),[](char x){return x-'0'<2;}),bin.end());
    
    auto new_end{ remove_if(bin.begin(), bin.end(), [](char x){ return x - '0' > 1; }) };
    if (new_end != bin.end()) {
        // garbage
    }
    

    ... dann könnte man mit bin danach sogar noch etwas anfangen ^^



  • Nochwas hierzu:

    @wob sagte in Problem Abfrage ob 1 oder 0 eingegeben wurde , C++:

    Man darf auf string[length_of_string] zugreifen, das ist '\0'.

    Das ist so eine typische Information in einem Anfängerthread, die zwar nicht falsch ist, aber dennoch bestens geeignet, um Anfängern beim Lernen Knüppel zwischen die Beine zu werfen. Siehe hier:

    @diejessi sagte in Problem Abfrage ob 1 oder 0 eingegeben wurde , C++:

    @wob ich verstehe nich ganz...

    Das gilt ab C++11 und nur für Strings (!). Für fast alle anderen array-ähnlichen Datenstrukturen ist diese Information falsch und sogar gefährlich. Man lernt unbeschwerter C++ wenn man sowas nicht weiss, und auch fortgeschrittene C++-Programmierer können ein Leben lang wunderbaren und korrekten Code schreiben, ohne jemals davon gehört zu haben 😉



  • Als Wiedereinsteiger in C++ (zuvor Python) ist mir folgende Lösung in den Sinn gekommen. Hierbei habe ich mich an der Python-Funktion all orientiert, welche glücklicherweise ein Äquivalent hat.:

    #include <algorithm>
    #include <iostream>
    #include <string>
    
    
    bool is_binary_digit(char c)
    {
    	return c == '0' or c == '1';
    }
    
    bool is_binary_number(const std::string& xs)
    {
    	return std::all_of(xs.begin(), xs.end(), is_binary_digit);
    }
    
    void test(const std::string& input)
    {
    	if (is_binary_number(input)) {
    		std::cout << input << " is a valid binary number." << std::endl;
    	}
    	else {
    		std::cout << input << " is NOT a valid binary number." << std::endl;
    	}
    }
    
    int main()
    {
    	const std::string valid_input{ "01001000100" };
    	const std::string invalid_input{ "0101a01c" };
    
    	test(valid_input);
    	test(invalid_input);
    }
    

Anmelden zum Antworten