don't know what's wrong, compiles and runs but breaks
Jun 15, 2014 at 4:17am UTC
I'm trying to count a given text files words and letters, and sort the words alphabetically. It compiles but after I enter in the file name the program breaks.
I try to use the debugger but while using it I still can't seem to find out what's happening. Due to my lack of knowledge.
Can you tell me what I'm doing wrong?
Thanks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <cctype>
using namespace std;
long long int char_count = 0; // global final letter count
long long int word_count = 1; // global word count, final count.
string String_cleanup(string dirty_string)
{
string dirt_chars = "?!.,-=\"/\\()1234567890_*&^%$#@<>;:[]{}|" ;
string clean_string;
while (dirty_string.find_first_of(dirt_chars) != string::npos) {
int found_pos = dirty_string.find_first_of(dirt_chars);
dirty_string[found_pos] = ' ' ;
int space_pos = dirty_string.find_first_of(' ' );
dirty_string.erase(dirty_string.begin() + space_pos);
}
clean_string = dirty_string;
if (clean_string != "" ) //if the word still exists
return clean_string;
}
int Letter_count(string string_to_count)
{
unsigned long long int letter_count = 0;
unsigned long long int check_pos;
for (check_pos = 0; check_pos < string_to_count.size(); check_pos++)
letter_count = check_pos;
if (letter_count > 0)
return letter_count;
}
string Char_lower(string up_string)
{
string low_string;
char c;
unsigned int i;
for (i = 0; i < up_string.size(); i++){
c = up_string[i];
if (isupper(c))
c = tolower(c);
}
low_string = up_string;
if (low_string != "" )
return up_string;
}
int main()
{
string word = "" ;
string file_name = "" ;
getline (cin, file_name);
cout << "\n" ;
ifstream subject_text(file_name);
if (!order_file.is_open()){
cout << "problem with ordered output file.\n" ;
return 1;
}
if (!subject_text.is_open()){
cout << "couldnt open subject text file stream.\n" ;
return 2;
}
vector<string> string_vector;
string temp;
string temp2;
int temp3;
while (subject_text >> word)
{
temp = String_cleanup(word);
temp2 = Char_lower(temp);
temp3 = Letter_count(temp2);
char_count += temp3;
string_vector.push_back(word);
word_count++;
}
sort(string_vector.begin(), string_vector.end());
cout << "\n\nAlphabetically sorted.\n\n" ;
unsigned int r = 0;
for (r == 0; r < string_vector.size(); r++) {
cout << string_vector[r] << "\n" ;
}
cout << "\n\t\t" << word_count << " strings in " << file_name << "\n" ;
cout << "\n\t\t" << char_count << " characters in " << file_name << "\n" ;
subject_text.clear();
subject_text.close();
return 0;
}
Jun 15, 2014 at 7:49am UTC
You have several functions that promise to return values, but don't always return values. That's a problem.
Topic archived. No new replies allowed.