Okay... Also vorneweg... Mein Programm ist ziemlich chaotisch, das saeubere ich und fuege Kommentare ein wenn es endlich so laeuft wie es soll... Also bitte nichts dazu sagen (Auch, dass ich mal true/false und mal 1/0 verwende, die Files nicht wirklich ueberpruefe auf Fehler/ob sie ueberhaupt da sind, etc. )
Also... Ich hab das Program gut zum laufen gebracht, alles laeuft, es durchsucht alles, gibt alles richtig aus findet alle Woerter bei denen die Buchstaben alphabetisch ansteigend sind, also passt super...
Jetzt war heute Vorlesung und einer meiner Kommilitonen hat darauf bestanden, dass wenn wieder Program abgeben, es doch auch Woerter sein sollen, wo die Buchstaben Aufsteigend und/oder gleich sind... Sprich, nicht nur Woerter wie Ghost, Almost, etc. sondern auch Woerter wie Billowy...
Super hab ich mir gedacht, kein Stress, nimmst du einfach das "=" hinter ">" weg und dann hab ich auch das geregelt...
Das funktioniert aber jetzt ueberhaupt nicht mehr, ich krieg direkt die Rueckmeldung vom Debugger:
Unhandled exception at 0x5adeca58 (msvcr100d.dll) in Wordlist.exe: 0xC0000005: Access violation writing location 0xabababab.
Und das im File "memcpy.asm" in der Zeile:
mov [edi],al ;U - write second byte to destination
Ich weis, dass das Problem hier liegt:
Code:
bool searchWords(string word,int length)
{
for(int index = 1;index < length;index++)
{
if(word[index-1] > word[index]) //Mit >= funktioniert es so
return false;
}
return true;
}
Hier das gesamte Programm:
Code:
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cctype>
#include <string>
#include <fstream>
using namespace std;
const int MIN_LENGTH = 4;
void loadFile(fstream &,fstream &, string &,float,float);
void writeOnFile(string [],fstream &);
void alphaSortResult(string [],int);
bool searchBadList(string,string [],int);
bool searchWords(string,int);
void Printwords();
void Printresults(float,float,string);
float yield(float,float);
void findLongestWord(string,string, int);
int main()
{
int exit;
fstream list;
fstream examplewords;
string word;
string upword;
float Upword_Count = 0;
float Word_Count = 0;
list.open("Wordlist1.txt");
examplewords.open("Examplewords.txt");
loadFile(list,examplewords,word,Upword_Count,Word_Count);
list.close();
cout << "Press any key to exit..." << endl;
cin >> exit;
return 0;
}
void loadFile(fstream &list,fstream &examplewords, string &word,float Upword_Count,float Word_Count)
{
bool found = 0;
bool found_bad = 0;
int wordlength;
int badwordlength;
string *goodlist;
goodlist = new string[200];
string badword;
string badlist[30];
string longestword;
int c = 0;
int i = 0;
int wordlengthlongest = 4;
fstream results("Results.txt", ios::out);
while(examplewords)
{
examplewords >> badword;
badwordlength = badword.length();
for(int d = 0;d < badwordlength; d++)
{
badword[d] = tolower(badword[d]);
}
badlist[c] = badword;
c++;
}
if (list)
{
cout << "Processing File..." << endl;
while (list >> word)
{
found = 0;
Word_Count++;
wordlength = word.length();
if (wordlength >= MIN_LENGTH)
{
for(int count = 0;count < wordlength; count++)
{
word[count] = tolower(word[count]);
}
found = searchWords(word,wordlength);
if(found == 1)
{
found_bad = searchBadList(word,badlist,c);
if(found_bad == 0)
{
Upword_Count++;
goodlist[i] = word;
i++;
findLongestWord(longestword,word,wordlengthlongest);
}
}
else
{
found = 0;
}
}
}
}
cout << longestword << endl;
alphaSortResult(goodlist,c);
writeOnFile(goodlist,results);
results.close();
Printwords();
Printresults(Upword_Count,Word_Count,longestword);
delete [] goodlist;
goodlist = 0;
}
void writeOnFile(string goodlist[],fstream &results)
{
int index = 0;
while (index < 173)
{
results << goodlist[index] << endl;
index++;
}
}
bool searchWords(string word,int length)
{
for(int index = 1;index < length;index++)
{
if(word[index-1] > word[index])
return false;
}
return true;
}
void Printwords()
{
string upword;
fstream results("Results.txt", ios::in);
int a = 1;
while(results >> upword)
{
cout << a << ". \t" << upword << endl;
a++;
}
}
void Printresults(float Upword_Count,float Word_Count, string longestword)
{
cout << "Words processed: " << Word_Count << endl;
cout << "Upwords found: " << Upword_Count << endl;
cout << "Longest Upword: " << longestword << endl;
cout << setprecision(2);
cout << "Percent found: " << yield(Upword_Count,Word_Count) <<"%" << endl;
cout << "\n\t\t\t\tCOMMENTS\n" << endl;
cout << "All Words in the list were processed." << endl;
cout << "The statistics are displayed without taking the example words into account." << endl;
}
float yield(float upwordcount,float wordcount)
{
return (upwordcount / wordcount) * 100;
}
bool searchBadList(string word,string badlist[],int size)
{
int index = 0;
bool found = false;
while(index<size && !found)
{
if (badlist[index] == word)
{
found = true;
return found;
}
index++;
}
return false;
}
void alphaSortResult(string goodlist[],int size)
{
int startScan, minIndex;
string minValue;
for (startScan = 0; startScan < (size - 1);startScan++)
{
minIndex = startScan;
minValue = goodlist[startScan];
for (int index = startScan + 1; index < size; index++)
{
if (goodlist[index] < minValue)
{
minValue = goodlist[index];
minIndex = index;
}
}
goodlist[minIndex] = goodlist[startScan];
goodlist[startScan] = minValue;
}
}
Irgendjmd. eine Idee was ich machen kann, ohne gleich alles zu aendern?
Wie kann ich so einen Fehler verhindern?
Danke schonmal
mfg Test-Grave