0

EDIT: This is a homework question I'm working on. I can't use any for/while loops anywhere in my code. The point of the program is to have the user enter a word, then it will search a dictionary for anagrams.

I'm trying to use recursion everywhere in my code rather than using a while loop or for loops. I'm stumped on how to change the following code into recursion:

while (!infile.eof())
{
    string line;
    getline(infile, line);
    dictionary.push_back(line); //using a vector to store a dictionary
}
Kelevera
  • 19
  • 1
  • 2
  • Please let us know some details about the problem you are trying to solve using recursion. Reading a file in C++ tends to be a linear thing. – Tim Biegeleisen Apr 11 '16 at 04:57
  • I wouldn't do it like that, even if I *wasn't* reading it recursively. [Read this to find out why](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – WhozCraig Apr 11 '16 at 04:58
  • 1
    Why are you trying to use recursion everywhere in your code? – Harry Apr 11 '16 at 04:58
  • Recursion can make code look nice but often will have a higher memory overhead. be careful that you're doing it for the right reasons. – Bug Maker Apr 11 '16 at 05:01
  • It's for homework. The project specifies that I can't use for/while loops anywhere in my code – Kelevera Apr 11 '16 at 05:08
  • 2
    To read a file recursively, read the first line of the file; and then, read the file recursively. – Jeremy Friesner Apr 11 '16 at 05:13

3 Answers3

2

I honestly haven't a clue why you would want to do this, but:

void read_file_recursively(std::istream& inp, std::vector<std::string>& v)
{
    std::string s;
    if (std::getline(inp, s))
    {
        v.emplace_back(std::move(s));
        read_file_recursively(inp, v);
    }
}
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • is there a way to do this with an array instead of a vector and without any additional non local variables? – Fence_rider Dec 02 '17 at 17:55
  • @Fence_rider That question is more nebulous than the OP's original post. I have no idea what you actually mean. The recursive nature of the function suggests the arguments are passed in by reference, and they have to come from *somewhere*, so they're not local, and thus disqualified by your precondition. And using an array is feasible so long as you know the top-end magnitude and avoid out-of-range indexing (such a top-end would likely be an argument as well, and also thus also passed by the caller). – WhozCraig Dec 03 '17 at 01:24
  • @WhozCraig just as op says, its for a homework assignment. my teacher happens to be more strict. anyways, i have to do this with only an array passed by value and an ifstream passed by reference with the exception of an array_size constant. with my precondition, i just meant that i cant use any variable that is not passed to the function or declared within said function. – Fence_rider Dec 03 '17 at 09:02
  • @Fence_rider That sounds like a different problem, and perhaps warrants its own question. It *sounds* like a ascending pointer-to-first-element and a *descending* size is in order, but that would be just a guess. – WhozCraig Dec 05 '17 at 15:52
1

something like

void readLine(/*some parameters here*/) {
    string line;
    getline(infile, line);
    dictionary.push_back(line)
    if (!infile.eof())
        readLine(/*some parameters here*/);
}

Needless to say, it is a weird way to read a file.

Andrei R.
  • 2,374
  • 1
  • 13
  • 27
0

I really do not understand you idea ещ abandon loops. But if you want ... I suggest you see two following functions:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void printFileFromBegin(ifstream &f)
{
    string s;
    getline(f, s);
    cout << s << endl;
    if (!f.eof())
        printFileFromBegin(f);
}

void printFileFromEnd(ifstream &f)
{
    string s;
    getline(f, s);
    if (!f.eof())
        printFileFromEnd(f);
    cout << s << endl;
}

Example of calling the last one:

    f.open(fmane);
    if (f.is_open())
    {
        printFileFromEnd(f);
        f.close();
    }

leads to printing lines from your file from the last to the first.

VolAnd
  • 6,367
  • 3
  • 25
  • 43