What am I doing wrong with my arrays?

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
80
81
82
83
#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;
  int index;
  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: countNumberLines
void countNumberLines(ifstream &inFile, int &countLines)
{
  // Variables
  string line;
  countLines = 0;

  while(!inFile.eof())
    {
      getline(inFile,line);
      countLines++;
    }
  inFile.close();
}
// Function: countNumberChars
void countNumberChars(ifstream &inFile, int &countChars)
{
  // Variables
  string character;
  countChars = 0;

  while(!inFile.eof())
    {
      getline(inFile,character);
      countChars++;
    }
  inFile.close();
}
// Function: populateArray
void populateArray(ifstream &inFile, string &words[])
{
  int index = 0;

  while(getline(inFile,words))
    {
      index++;
    }
  inFile.close();
}


These are the errors that I keep getting & I have no idea how to fix them (please note the line numbers listed here probably won't match up exactly with the code pasted above).

p1.cpp: In function `int main()':
p1.cpp:32: error: expected primary-expression before ']' token
p1.cpp: At global scope:
p1.cpp:80: error: declaration of `words' as array of references
p1.cpp: In function `void populateArray(std::ifstream&)':
p1.cpp:83: error: `words' was not declared in this scope
Arrays are passed by reference by default so you shouldn't need to do & when passing them to functions.

Make sure your function prototypes match with the functions and show that it's accepting an array. For example change it to this (which includes the []).
void populateArray(ifstream&, string[]);

When passing arrays into a function you only need to include the name of the array, not the [].
populateArray(inFile, words);

getline will pass a line from the file into an individual string, so you are not able to do this.
while(getline(inFile,words))

You will want to create some sort of counter so you can pass it into 1 string in the array at a time. Hopefully that cleared some things up.
Yes, thank you. You definitely helped clear things up a bit but I am still having some trouble. Okay I just changed my code to this:

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
80
81
#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[])
{
  int index = 0;

  while(getline(inFile,words))
    {
      index++;
    }
  inFile.close();
}


And now it's giving me these compiling error messages:

p1.cpp: In function `void populateArray(std::ifstream&, std::string*)':
p1.cpp:81: error: no matching function for call to `getline(std::basic_ifstream<char, std::char_traits<char> >&, std::string*&)'

And line 81 would be:
 
  while(getline(inFile,words))

Last edited on
Topic archived. No new replies allowed.