Hi. I'm trying to make a program that helps you to learn foreign language words faster. By asking you them. This is only the beginning part of it, but yet it crashes. I don't really know what's the problem, I'm very bad with arrays at the moment, I don't even know if I used the right codes for program like this one.
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
cout << "Welcome. You are using F-Words Helper tool which was create to help you\n learn your foreign language words!" << endl;
cout << "\nProgram written by: Rokas Paulikas in 2013-10-02" << endl;
int total, i;
cout << "\nHow much different words do you want to learn?" << endl; cin >> total;
string word[total]; // how big your array will be?
for(i = 1; i <= total; i++){
cout << "Enter word " << i << ":" << endl;
cin >> word[i];
}
cout << "You are retarded" << endl;
return 0;
}
Also don't mind the last sentence, I was just testing if it would not crash if the program executed one more line of code at the end of it. Thanks.
First of all, I'm not sure how this compiles without "total" being some kind of compile-time constant. Regardless, you're running your for loop from 1 to total, when it should run from 0 to (total-1). Think of what would happen on the last iteration of the loop if you were essentially saying cin >> word[total]; That's out of the bounds of your array.
Also you can not create arrays like that. You must declare the size of an array before compile time. Another method would be to use a dynamic array like
1 2 3 4 5 6 7 8 9
string *words = nullptr;
//get the amount of words
words = new string[size];
//when you are done remember to delete it
delete[] words;
//if you use new[] you must use delete[]