atoi() Frage



  • Was passt dir an adatcas Ansatz nicht???



  • n00b42 schrieb:

    Geht es etwa gar nich mit atoi(), ich habs jetzt so geändert:

    Doch, klar. Aber wenn Du mehrere einliest, sind streams doch angemessener, oder?



  • ja sicher aber was stimmt dadran nich???

    #include <iostream>
    #include <string>
    #include <cstdio>
    #include <stdlib.h>
    
    using namespace std;
    int main ()
    {
        string text;
        string zeichen[100];
        int posi[100];
        int lustig[100];
        posi[1]=0;
        int loop;
        getline(cin,text);
    for (loop=2;loop<10;++loop)
    {
        posi[loop]=text.find(" ",posi[(loop-1)]);                    
        zeichen[loop]=text.substr(posi[(loop-1)],posi[loop]);        
        cout<<zeichen[loop]<<endl;                               
        lustig[loop]=atoi(zeichen[loop].c_str());
        cout<<lustig[loop];
    }
    return 0;
    }
    


  • n00b42 schrieb:

    ja sicher aber was stimmt dadran nich???

    #include <iostream>
    #include <string>
    #include <cstdio>
    #include <stdlib.h>
    
    using namespace std;
    int main ()
    {
        string text;
        string zeichen[100];
        int posi[100];
        int lustig[100];
        posi[1]=0;
        int loop;
        getline(cin,text);
    for (loop=2;loop<10;++loop)
    {
        posi[loop]=text.find(" ",posi[(loop-1)]);                    
        zeichen[loop]=text.substr(posi[(loop-1)],posi[loop]);        
        cout<<zeichen[loop]<<endl;                               
        lustig[loop]=atoi(zeichen[loop].c_str());
        cout<<lustig[loop];
    }
    return 0;
    }
    

    Hi,

    dein Code ist total unlesbar. Auch die Einrückung ist dir nicht wirklich gut gelungen 😃
    Außerdem ist dein Ansatz zur Lösung des Problems nicht sonderlich schön, da du es dir viel zu kompliziert machst.

    So könnte man es machen, aber das empfinde ich als total umständlich:

    #include <iostream>
    #include <string>
    
    int main()
    {
    	std::string text; // Der eigentliche Text
    
    	std::getline(std::cin, text); // einlesen
    
    	std::string temp;
    
    	for (unsigned int i = 0; i < text.length() + 1; ++i)
    	{
    		if (text[i] == ' ' || text.length() == i)
    		{
    			std::cout << atoi(temp.c_str()) << std::endl;
    			temp = "";
    		}
    		else
    		{
    			temp += text[i];
    		}
    	}
    
    	return 0;
    }
    

    Folgender Code macht das gleiche, aber viel besser:

    #include <iostream>
    #include <sstream>
    #include <string>
    
    int main()
    {
    	std::string text; // Der eigentliche Text
    
    	std::getline(std::cin, text); // einlesen
    
    	int number;
    
    	std::stringstream strm(text);
    
    	while (strm >> number)
    	{
    		std::cout << number << std::endl;
    	}
    
    	return 0;
    }
    


  • Und dazu ergänzend ein Beispiel aus der STL Doku von sgi, das den istream_iterator nutzt (http://www.sgi.com/tech/stl/istream_iterator.html) und dann man hat die Werte auch in einem vector:

    vector<int> V;
    copy(istream_iterator<int>(strm), istream_iterator<int>(), back_inserter(V));



  • langsam hab ich das Gefühl ich solte von Dev-C++ 4.9.9.2 auf was andres umsteigen.
    Kann mir da jemand einen Tipp geben?



  • n00b42 schrieb:

    langsam hab ich das Gefühl ich solte von Dev-C++ 4.9.9.2 auf was andres umsteigen.
    Kann mir da jemand einen Tipp geben?

    Code::Blocks natürlich. Oder falls Du einen brutal schnellen Rechner hast eine Java-IDE wie Netbeans oder Eclipse.



  • Und hier noch ein Beispiel ohne streams mit einem formschönen C Style Cast:

    void example ( char* s )
    {
    	while (*s)
    		printf("%ld\n", strtoul ( s, &s, 10 ) );
    }
    
    int main() 
    { 
        string text("1 12 123"); 
    	example ( (char*)text.c_str() );
        return 0; 
    }
    

Anmelden zum Antworten