Fehler in Priemzahlenprogramm
-
thebaboon schrieb:
ich möchte das gerne mit eminer methode lösen (ausnutzen vom gleichsetzen von integer und float)
Das ist nicht so gut, wegen der Rundungsfehler bei floats.
Und eine Zahl ist noch nicht eine Pröhmzähl, weil sie sich einmal nicht ohne Rest durch eine Zahl teilen lässt.
-
#include<iostream> using namespace std; int main() { int priemzahl = 1; int oft; int test; int schleifenzahl; float teilzahl; float testfloat; cout << "Wieviele Primzahlen sollen Angezeigt werden?\n\a"; cin >> oft; int i; // in einer schleife deklariert man eine variable immer wieder neu... ! for(int wieder = 0; wieder < oft; ++wieder) { ++priemzahl; schleifenzahl = 0; teilzahl = 0; for(i = 1; teilzahl < priemzahl; ++i) { ++teilzahl; testfloat /= teilzahl; test = testfloat; if( test != testfloat ) { cout << priem zahl <<" | "; teilzahl = priemzahl; i = 0; } } } cin.get(); cin.get(); }so sieht das shcon mal etwas besser aus...
(keine bzw nur minimalste änderungen...)
-
#include<iostream> using namespace std; int main() { long priemzahl = 1; long oft; long test; float teilzahl; float testfloat; cout << "Anzeige aller Priemzahlen im Zahlenraum von 1 bis:\n\a"; cin >> oft; long i; // in einer schleife deklariert man eine variable immer wieder neu... ! cout<<"\n1 | "; for(long wieder = 0; wieder < oft; ++wieder) { ++priemzahl; teilzahl = 1; int fehler; fehler = 0; int durchlauf; durchlauf = 0; //cout<<"\n\n------------------------------<<>>------------------------------\n"; for(i = 0; fehler != 1; ++i) { ++durchlauf; ++teilzahl; //cout<<"<--><--><--><--><-->\n"; //cout<<durchlauf<<"<--- durchlauf\n"; //cout<<teilzahl<<"<---teilzahl\n"; //cout<<priemzahl<<"<---priemzahl\n"; testfloat = priemzahl/teilzahl; //cout<<testfloat<<"<--- Testfloat\n"; test = testfloat; //cout<<test<<"<--- Test\n"; //cout<<fehler<<"<--- Fehler\n\n"; if( priemzahl == teilzahl && priemzahl <= oft) { cout << priemzahl <<" | "; fehler = 1; } if( test == testfloat ) { fehler = 1; } } //cout<<"\n\n------------------------------<<>>------------------------------\n"; } cin.get(); cin.get(); }jetzt funzt es...
abber wie kann ich die gesammte ausgabe in eine textdatei speichern?
-
such mal nach fstream

-
Beim Aufruf des Programms aus der Konsole:
programm-name > datei-name.txtOder du benutzt Filestreams (std::fstream).
Gruß
Don06
-
fstream datei; datei.open("MeineTextDatei.txt", ios::out); datei<<"BLA BLA BLUBB" datei.close();Ps.
Mein Primzahlen Programmint main() { Anfang: int i; int a; cout<<"Von wo an sollen die Primzahlen Berechnet werden? "; cin>>i; cout<<"\n"; cout<<"Bis wohin sollen die primzahlen berechnet werden (max.4294967295)? "; cin>>a; cout<<"\n"; for(i;i<a;i=++i) { bool isprim=true; for(int j=2;j<i;++j) { if(i%j==0) { isprim=false; break;} } if(isprim) { cout<<"Primzahl: "<<i<<"\t";} }
-
danke...
abber wenn ichs jetzt so mach:
#include<iostream> using namespace std; int main() { long priemzahl = 1; long oft; long test; float teilzahl; float testfloat; cout << "Anzeige aller Priemzahlen im Zahlenraum von 1 bis:\n\a"; cin >> oft; long i; // in einer schleife deklariert man eine variable immer wieder neu... ! cout<<"\n1 | "; for(long wieder = 0; wieder < oft; ++wieder) { ++priemzahl; teilzahl = 1; int fehler; fehler = 0; int durchlauf; durchlauf = 0; //cout<<"\n\n------------------------------<<>>------------------------------\n"; for(i = 0; fehler != 1; ++i) { ++durchlauf; ++teilzahl; //cout<<"<--><--><--><--><-->\n"; //cout<<durchlauf<<"<--- durchlauf\n"; //cout<<teilzahl<<"<---teilzahl\n"; //cout<<priemzahl<<"<---priemzahl\n"; testfloat = priemzahl/teilzahl; //cout<<testfloat<<"<--- Testfloat\n"; test = testfloat; //cout<<test<<"<--- Test\n"; //cout<<fehler<<"<--- Fehler\n\n"; if( priemzahl == teilzahl && priemzahl <= oft) { fstream datei; datei.open("priemzahlen.txt", ios::out); datei<<priemzahl<<" | "; datei.close(); cout << priemzahl <<" | "; fehler = 1; } if( test == testfloat ) { fehler = 1; } } //cout<<"\n\n------------------------------<<>>------------------------------\n"; } cin.get(); cin.get(); }dann kommt ne fehler meldung "aggregate `std::fstream datei' has incomplete type and cannot be defined "
-
#include <fstream> vergessen sry
-
jetzt kommt:
#include<iostream> #include <fstream> using namespace std; int main() { long priemzahl = 1; long oft; long test; float teilzahl; float testfloat; cout << "Anzeige aller Priemzahlen im Zahlenraum von 1 bis:\n\a"; cin >> oft; long i; // in einer schleife deklariert man eine variable immer wieder neu... ! cout<<"\n1 | "; for(long wieder = 0; wieder < oft; ++wieder) { ++priemzahl; teilzahl = 1; int fehler; fehler = 0; int durchlauf; durchlauf = 0; //cout<<"\n\n------------------------------<<>>------------------------------\n"; for(i = 0; fehler != 1; ++i) { ++durchlauf; ++teilzahl; //cout<<"<--><--><--><--><-->\n"; //cout<<durchlauf<<"<--- durchlauf\n"; //cout<<teilzahl<<"<---teilzahl\n"; //cout<<priemzahl<<"<---priemzahl\n"; testfloat = priemzahl/teilzahl; //cout<<testfloat<<"<--- Testfloat\n"; test = testfloat; //cout<<test<<"<--- Test\n"; //cout<<fehler<<"<--- Fehler\n\n"; if( priemzahl == teilzahl && priemzahl <= oft) { fstream datei; datei.open("priemzahlen.txt", ios::out); datei<<priemzahl<<" | "; datei.close(); cout << priemzahl <<" | "; fehler = 1; } if( test == testfloat ) { fehler = 1; } } //cout<<"\n\n------------------------------<<>>------------------------------\n"; } cin.get(); cin.get(); }Permission denied
ld returned 1 exit status
-
Hast du Zugriff auf die Datei?
-
was meinst du mit zugriff?
-
Ist sie schreibgeschützt?
-
SirForce schrieb:
Ist sie schreibgeschützt?
Exakt. Und Willkommen, falls du der bist, für den ich dich halte.

-
naja...sie existiert ja noch garnicht, weil ich das programm ja nochnicht ausführen konnte
-
Wie siehts mit ofstream aus?
-
tut mir leid...ich programmiere seit 2 tagen in c

könntest du mir kurz einmal sagen was offstream ist?
-
#include<iostream> using namespace std; int main() { int priemzahl = 1; int oft; int test; int schleifenzahl; float teilzahl; float testfloat; cout << "Wieviele Primzahlen sollen Angezeigt werden?\n\a"; cin >> oft; int i; // in einer schleife deklariert man eine variable immer wieder neu... ! for(int wieder = 0; wieder < oft; ++wieder) { ++priemzahl; schleifenzahl = 0; teilzahl = 0; for(i = 1; teilzahl < priemzahl; ++i) { ++teilzahl; testfloat /= teilzahl; test = testfloat; if( test != testfloat ) { fstream datei; datei.open("priemzahlen.txt", ios::out); datei<<priemzahl<<" | "; datei.close(); cout << priemzahl <<" | "; teilzahl = priemzahl; i = 0; } } } cin.get(); cin.get(); }so siehts jetzt aus
Fehlermeldung;:
32 C:\Dokumente und Einstellungen\Admin\Eigene Dateien\Geschäft\Unkommerziell\cplusplus\priemzahlentester2.cpp aggregate `std::fstream datei' has incomplete type and cannot be defined
-
#include <fstream>