What is wrong with my for loop in trying to store words from a file into an array?

Here is my code. All of my functions are fine except for "Function: populateArray" where I have to store the words from a file into an array (the array has a limit of 1000). What is wrong with my code that I keep getting a compiling error & what do I need to do to fix it?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<iostream>
#include<fstream>
using namespace std;

// Function Prototypes
void readFilename(ifstream&, string&);
void countNumberLines(ifstream&, int&);
void countNumberChars(ifstream&, int&);
void populateArray(ifstream&, string[]);

int main()
{
  // Variables
  ifstream inFile;
  string filename;
  int countLines;
  int countChars;
  char ch;
  string words[1000];

  // Function Calls
  readFilename(inFile, filename);
  countNumberLines(inFile, countLines);
  countNumberChars(inFile, countChars);
  populateArray(inFile, words);

  return 0;
}

// Function: readFilename
void readFilename(ifstream &inFile, string &filename)
{
  // Reads in file name.
  cout << "Please enter the file name you wish to open." << endl;
  getline(cin, filename);
  inFile.open(filename.c_str());

  // Displays error message if file is not found.
  if (!inFile)
    {
      cout << "This file did not open properly and the program will now terminate.\nPlease make sure the spelling of the file is correct." << endl;
    }
}

// Function: countCharsLines
void countCharsLines(ifstream &inFile, int &countLines, int &\
countChars, char &ch)
{
  string line;
  countLines = 0;
  countChars = 0;

  while(!inFile.eof())
    {
      getline(inFile,line);
      countLines++;
    }

  inFile.clear();
  inFile.seekg(0, ios::beg);

  while(!inFile.eof())
    {
      ch = inFile.get();
      countChars++;
    }

  inFile.close();
}

// Function: populateArray
void populateArray(ifstream &inFile, string words[])
{
  for(int i=0;i<=1000;i++)
    {
      inFile >> words[i];
    }
  inFile.close();
}


And here is the compiling error that I'm getting. Not sure what I'm doing wrong.

Undefined first referenced
symbol in file
countNumberChars(std::basic_ifstream<char, std::char_traits<char> >&,

int&)/var/tmp//ccTJqJoK.o
countNumberLines(std::basic_ifstream<char, std::char_traits<char> >&,

int&)/var/tmp//ccTJqJoK.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status
You declared function

void countNumberChars(ifstream&, int&);

but did not define it though you are calling it in the code

countNumberChars(inFile, countChars);
Hi !
Line 7, 9 have function declarations but no function definitions :/ I think u mean to delete these declarations (or at least comment out for now) because u have a merged version on line 46.
In contrast line 46 has the definition of a function but no declaration. There's also a syntac error at the end of that line.

In main(), lines 23, 24 are calling those same functions from line 7 and 9 which don't have a definition...Like 7 and 9, u'll want to comment these out in favor of calling the function whose definition is on line 46.

Remember that each function declaration must have an exactly matching definition. U can't have 1 without the other or u'll get those compiler errors.

char &ch doesn't need to be passed into the function on line 46 either since its purpose is complete when the function is completed. Just declare it locally.

Hope this helps !
the purpose of making functions is to make things easier and simpler not to complicate them.. i can see that if you perform 'read file' and 'count characters/lines' in the same function then it would be better..

Last edited on
Yeh, I said that too in my reply to a previous thread about this same code where there were 3 separate functions to read file, count chars and count lines. Now there's (almost) 2.
1 step at a time ~
Topic archived. No new replies allowed.