Good day.
I have several files in my folder: main.cpp , input.txt . In the main.cpp file, I read a line from the file, divide the line by points (I find sentences), then use one function to check the very first character of each sentence to see if it is capitalized. If not, then change to this.
Ie it should look like this:
Current input: I have no choice. glory to mankind. Glory. to. Mankind. After: I have no choice. Glory to mankind. Glory. To. Mankind. Well, of course, that I have stored in input.txt:
I have no choice. glory to mankind. Glory. to. Mankind. So, I wrote this project using CLion IDE. Everything worked. But then I decided to do the same in a separate folder and compile it in the console, although I ran into the problem that I was returning an empty string from the file .
Here is the main
#include <iostream> #include <fstream> #include <sstream> #include <vector> #include <iterator> using namespace std; string getResult(vector<string> val) { bool flag = true; for (int i = 0; i < val.size(); i++) { // Iteration string current = val[i]; for (int j = 0; j < current.size(); j++) { if (current[j] != ' ') { // If first word is uppercase if (flag && isupper(current[j]) == 0) { current[j] = toupper(current[j]); val[i] = current; } flag = false; } } flag = true; } string result; for (int i = 0; i < val.size(); i++) result += val[i] + "."; return result; } int main() { ifstream file("input.txt"); string str; string file_contents; if (!file.is_open()) { cout << "Some problems with file..."; return -1; } while (getline(file, str)) { file_contents += str; file_contents.push_back('\n'); } vector<string> array; // split istringstream is(str); string s; while (std::getline(is, s, '.')) array.push_back(s); cout <<"Current input: "<< str << endl; string result = getResult(array); cout << "After: " << result << endl; return 0; } I collect the project in the console: g++ -o prog main.cpp . ./prog : ./prog I get it
Current input: After: I just can not understand what is wrong and what is wrong.