Here is my code
It is supposed to read in a file into three vectors and skips empty lines.The bool function is there to check if a line is empty or not. If it is empty, prevents the code in the main from reading it. So my question is, how do I get rid of the separate bool function and combine it in the int main? I want everything to be inside int main. Thanks.
#include <string>
#include <iostream>
#include <sstream>
#include <vector>
#include <fstream>
using namespace std;
// check if the string is empty i.e it only consists of
// whitespace characters only
bool empty(string s) {
for (int i = 0; i < s.length(); i++) {
if (!(s[i] == ' ' || s[i] == '\t' || s[i] == '\n')) return false;
}
return true;
}
int main() {
ifstream objFile;
string inputName;
string outputName;
string header;
cout << "Enter image file name: ";
cin >> inputName;
objFile.open(inputName);
string name;
vector<string> name2;
string description;
vector<string> description2;
string initialLocation;
vector<string> initialLocation2;
string line;
if (objFile) {
while (!objFile.eof()) {
line = "";
// Skip past empty lines
while (!objFile.eof() && empty(line))
getline(objFile, line);
name = line;
name2.push_back(name);
line = "";
// Skip past empty lines
while (!objFile.eof() && empty(line))
getline(objFile, line);
description = line;
description2.push_back(description);
line = "";
// Skip past empty lines
while (!objFile.eof() && empty(line))
getline(objFile, line);
initialLocation = line;
initialLocation2.push_back(initialLocation);
}
}
else {
cout << "not working" << endl;
}
for (std::vector<string>::const_iterator i = name2.begin(); i != name2.end(); ++i)
std::cout << *i << ' ';
std::cout << endl;
for (std::vector<string>::const_iterator i = description2.begin(); i != description2.end(); ++i)
std::cout << *i << ' ';
std::cout << endl;
for (std::vector<string>::const_iterator i = initialLocation2.begin(); i != initialLocation2.end(); ++i)
std::cout << *i << ' ';
std::cout << endl;
}