Hello All,
I wrote this simple program that reads text files but I theres one problem. When it's done and the user gets prompted if he wants to read again from another text file or exit, when he selects the option where he wants to read from another text file it just crashes.. Any idea why ? I'm running this in xcode 4.2 on my macbook pro 64 bit.
function run() :
void run(){
cout << "\t\t**FILE READING PROGRAM**\n\n"<<endl;
cout << "Welcome, This program takes a text file as input and tells you the number of occurences of each letter."<<endl;
execute:
string filename;
cout << "\n\n\n\nEnter filename to read (file must be in desktop) : ";
getline(cin, filename);
string path = "/Users/TheUser/Desktop/";
path.append(filename);
const char * c = path.c_str();
ifstream file;
file.open(c);
if(file.is_open()){
string current;
char alpha[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
int len2 = sizeof(alpha);
int al[len2];
for(int y=0;y<len2;y++){
al[y] = 0;
}
while(!file.eof()){
file >> current;
int len = current.length();
for(int i=0;i<len;i++){
for(int x=0;x<len2;x++){
if(current[i] == alpha[x]){
al[x]++;
}
}
}
}
cout << "CHARACTER\t\t\tOCCURENCES"<<endl<<endl;
for(int i=0;i<len2;i++){
cout << alpha[i] << "\t\t\t\t\t" << al[i]<<endl<<endl;
}
file.close();
prompt();
}else{
cout << "\n\n\nERROR: File type not supported or file does not exist. (NOTE: Make sure the file is in desktop.)"<<endl<<endl;
cout << "NOTE : If you are trying to read from a word document, copy your content and paste it in a text document (.txt file), this means that you need to copy and paste the content into notepad(on Windows) or TextEdit (on MAC) and save it to your desktop." <<endl<<endl;
goto execute;
}
}
and now the prompt function :
void prompt(){
int choice;
cout << "\n\nWould you like to read another text file ? \n1-Yes\n2-No\n"<<endl;
cin >> choice;
if(choice == 1){
run();
}else if(choice == 2){
cout << "Exiting ...";
exit(0);
}else{
cout << "\n\nPlease pick a valid option (either 1 or 2)";
prompt();
}
}
finally the main function :
int main(){
run();
return 0;
}
It works fine and everything but just when I prompt the user if he wants to read another text file it gives me an error like :
Any help is greatly appreciated , Thanks in advance ! :)