Witam.
Stworzyłem klasę words
Która docelowo ma przechowywać wyrazy z pliku.
Chciałbym napisałem która pyta o plik i ładuje wyrazy z pliku do tablicy obiektów klasy words.
Pojawił mi się problem. (Ta tablica obiektów jest jeszcze nie zdefiniowana w funkcji) Jak sobie z tym poradzić?
Mój kod:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int n_words=0;//number of words
fstream plik;
void ask4file(){
string file_name;
do {
cout<<"Please enter filename\n";
getline (cin,file_name);
if(!file_name.size()){//open def file
plik.open( "code.txt", ios::in | ios::binary );
}else{
plik.open( file_name, ios::in | ios::binary );
}if(plik.good()==0){
cout << "\nNo such file\n" << std::endl;
}
}while (plik.good() == false);
while (plik >> tab[n_words].content){//copy word to array
n_words++;
}
n_words--;
}
class words{
public:
int times;
string content;
};
int main(){
words tab[15];
ask4file();
plik.close();
cout<<tab[1].content;
return 0;
}