Witam!
Po raz kolejny zwrazam sie do Was z prosba o pomoc, bo w sumie po to tu jestescie. Moj problem wyglada nastepujaco:
- Gdy zliczam ilosc wyrazow, to wyskakuje mi za kazdym razem 0;
- Gdy chce posortowac elementy, to pozostaje puste miejsce.
Kod:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cstring>
#include <iomanip>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(void)
{
string fileName;
int c;
int array_size = 1024;
char *array = new char[array_size];
int poss = 0,
itype = 1,
count = 0;
// open the file
cout << "Podaj nazwe pliku, bez rozszerzenia.\n"
"Rozszerzenie z jakiego korzysta program to *.txt " << endl;
cout << "Nazwa pliku: ";
cin >> fileName;
fileName = fileName + ".txt";
ifstream inFile;
inFile.open(fileName.c_str());
vector<string> names;
if (inFile.is_open()){
while (!inFile.eof() && poss < array_size){
//reading one character from file to array
inFile.get(array[poss]);
++poss;
}
//placing character array terminating character
array[poss-1] = '\0';
system("cls");
cout << "Plik zostal otwarty prawidlowo, co dalej chcesz z nim zrobic?\n";
while(itype != 0){
cout << "[1] Pokaz zawartosc pliku\n";
cout << "[2] Ile znakow jest w pliku?\n";
cout << "[3] Ile jest wyrazow w pliku?\n";
cout << "[4] Posortuj wyrazy alfabetycznie (z powtorzeniami)\n";
cout << "[0] Wyjdz\n";
cin >> itype;
switch(itype){
// show text
case 1:{
system("cls");
for (int i = 0; array[i] != '\0'; ++i){
cout << array[i];
}
cout << endl << endl << endl << endl;
break;
}
// count of characters
case 2:{
system("cls");
cout << string(array).length();
cout << endl << endl << endl << endl;
break;
}
// count of words
case 3:{
system("cls");
for (int i = 0; array[i] != '\0'; ++i){
++count;
}
cout << "Ilosc wyrazow w pliku: " << count << endl;
cout << endl << endl << endl << endl;
break;
}
// sort
case 4:{
system("cls");
string word;
while (inFile.good()) {
getline(inFile, word);
names.push_back(word);
}
sort(names.begin(), names.end());
// Loop to print names
for (size_t i=0; i < names.size(); i++)
cout << names[i] << "\n";
break;
}
// count of how many times word shows
default:{
system("cls");
break;
}
}
}; //while
system("cls");
cout << "Dziekujemy za skorzystanie z programu. Zapraszamy ponownie.\n";
} // end of if - file is open
// if can't open the file
else{
system("cls");
cout << "Nie mozna otworzyc pliku. Sprawdz nazwe i wroc ponownie.\n";
}
inFile.close();
return 0;
}
Jezeli mam cos poprawic w kodzie i mogloby to usprawnic dzialanie to prosze o jakies dokladniejsze wskazowki, poniewaz jest to na zaliczenie i mam czas tylko do czwartku. Dziekuje ;)