Hello xCriminaL,
To start with it is always a good idea to provide complete code that can be compiled and run. This does not mean the entire program, but at least enough to show the problem and what leads up to it.
What you need to look at first is:
for (int i = 0; i <= groesse; i++)
. Now I can only guess that "groesse" has a value of 20, but this may not be the case. The problem comes from using (<=). This may be useful in an if statement or while condition, but should be used sparingly in a for loop and not the norm.
Looking at the for loop a little differently:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
f.open("vokabeln.txt", ios::out);
// <--- How do you know it is open and usable? And how is "f" defined?
for (int idx = 0; idx <= groesse; idx++)
{
if (firstTry == false)
{
f << '\n';
}
else
{
firstTry = false;
}
f << sammlung[idx].spanisch; // here comes the warning
f << '\n';
f << sammlung[idx].deutsch;
f << '\n';
}
f.close();
|
"i", "j" and "k" may be acceptable names for loop iterators, but sometimes a good name is better. When you look at lines 15 and 17 it makes more sense.
Since the array is indexed from (0)zero to 19 for a 20 element array starting at "idx" at (0) and going to (<=) means that it is doing 1 extra loop than it needs to, so when "idx" becomes 20 this is beyond the end of the array thus causing the error.
When using a for loop try to set it up to use the (<) only. You will save your-self many headaches.
After that as
seeplus shows you can use the insertion operator(<<) to chain several different things into 1 "cout" statement. There is no need for a separate "cout" statement for every little bit. And tha same is true for writing to a file.
Depending on how you code is written, as a whole, the:
f.close();
may not be needed as the dtor of the class will close the file when the function looses scope.
Andy