0

I'm following these guidelines for a little project in school but am unclear on how to implement this into a separate function? Any suggestions?

I don't want anyone to write code for me, but a little hint would be helpful! :D My below code does exactly what I think these instructions are asking, but just not in a separate function.

I think I declared the prototype correctly but man am I really stumped! Also, what exactly is the "command line"?

get_word: The first function should be called "get_word". It takes two arguments. Every call to this function will fill one of the arguments with a word from the file. You should then output every word you receive and a count of the total number of words to a file specified by the user on the command line. This function doesn't return anything.

#include <iostream>
#include <fstream>

//void get_word(std::ifstream, std::string);

int main()
{
  std::ifstream word("io_file.txt");

  std::string str1;
  int counter = 0;

  while(word >> str1)
    {
      std::cout << str1 << '\n';
      ++counter;
    }
  std::cout << "Word count: " << counter << '\n';
}
  • 1
    Move the code that does the input in your main into your get_word function. You will also need to use reference parameters. –  Mar 05 '17 at 22:13
  • Well if I understand correctly, the only line that actually does input is "while(word >> str1)". I'm a little confused about the command line part. For example, I pass two files in the command line to the program before it starts, one being an input file and another an output file, but I'm already reading from a file within the program! How can I read from two files? – InertRaindrop Mar 05 '17 at 22:39
  • "Well if I understand correctly, the only line that actually does input is "while(word >> str1)"." - Yes.The number of files is not an issue here. –  Mar 05 '17 at 22:44

1 Answers1

0

"get_word". It takes two arguments. Every call to this function will fill one of the arguments with a word from the file.

As you supposed, you will need two arguments, one file and one string. But because you must fill the string passed as argument with a word from the file, you have to declare the string argument as reference. What it means ? By default, arguments are passed as copy, for example

void plusTwo(int a) { a += 2 }

The code inside the function declaration will modify the copy of the variable passed as parameter, so :

int b = 0;
plusTwo(b);
//b still equals 0

So you have to mark the argument with an & in the declaration which means that you passed the argument as reference (no copy). So for example :

void plusTwo(int &a) { a += 2 }

The code inside the function declaration will modify the copy of the variable passed as parameter, so :

int b = 0;
plusTwo(b);
//b equals now 2

So the correct prototype is :

void get_word(std::ifstream, std::string&);

Then, the command line is the window where are displayed the text with "cout". So in order to get the file path, you have to do :

std::string filePath;
cin >> filePath;

filePath will get what is entered by user.

Julien Vernay
  • 295
  • 3
  • 13
  • `void get_word(std::ifstream, std::string&);` -> `void get_word(std::ifstream &, std::string&);` Cannot copy an `ifstream`, [or any stream for that matter](http://stackoverflow.com/questions/6010864/why-copying-stringstream-is-not-allowed), so you can't pass it by value. – user4581301 Mar 05 '17 at 22:29
  • Why can't a stream be copied if you don't mind me asking? – InertRaindrop Mar 05 '17 at 22:36
  • @Inert Because it doesn't make sense to do so - there is really only one stream. In the same way it wouldn't make any sense for a bank account object to be copied - there is only one bank account with a specific account number. Preventing the copy is done by deleting the copy constructor. –  Mar 05 '17 at 22:43
  • Ahh, that makes more sense! Thanks for the reply! – InertRaindrop Mar 05 '17 at 22:47