-1

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;
}
Swordfish
  • 1
  • 3
  • 17
  • 42
SBB
  • 1
  • Given that you are also using objects and functions from the standard library - which also means your code is not self-contained in `main()` - is it also your intent to write your `main()` so it doesn't use them as well? – Peter Nov 10 '18 at 07:19
  • You will want to review [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – David C. Rankin Nov 10 '18 at 08:27
  • `empty()` will return true when the string is *empty itself*... – Ruks Nov 10 '18 at 13:33

2 Answers2

2

Your usage of a redundant function empty() is resolvable just by using the Standard Library (STL):

Edit: As pointed out in comments there was a supposed to be a check if there was the only whitespace inside the string along with its length... Thanks to @Enfyve for pointing out std::all_of(s.begin(), s.end(), isspace))...

Instead of this:

if (empty(line)) { /*...*/ }

this:

if (std::all_of(line.begin(), line.end(), isspace)) { /*...*/ }

or:

if (std::find_if(line.begin(), line.end(), [](char const ch)
    { return !isspace(ch); }) == line.end()) { /*...*/ }

or:

if (std::count_if(line.begin(), line.end(), [](char const ch)
    { return !isspace(ch); }) == 0) { /*...*/ }

You have many options to do that... Don't invent the wheel again!

Ruks
  • 1,389
  • 4
  • 17
  • 1
    Also worth noting, since the custom function also treats whitespace strings as 'empty' that you can use `bool onlyWhiteSpace = std::all_of(s.begin(),s.end(),isspace);` in C++11 or newer to determine if a string only consists of whitespace characters. – Enfyve Nov 10 '18 at 07:20
  • 1
    The function named `empty()` in the OP's code also checks for presence of (some) whitespace, which differs from `std::empty()`. – Peter Nov 10 '18 at 07:21
0

how do I get rid of the separate bool function and combine it in the int main?

You "get rid" of the function by implementing what it does in main().

Also see Why is iostream::eof inside a loop condition considered wrong?. But you have been told that in your former questions already.

Please refrain from asking the same question multiple times just because you are not happy with the ansers. Edit your question the get better answers instead.

Swordfish
  • 1
  • 3
  • 17
  • 42